EditText getText()始终返回空字符串

时间:2016-11-22 10:48:43

标签: java android string android-edittext is-empty

我是Android编程的新手,我认为我最大的问题是我无法真正理解所有这些膨胀的东西。你能帮我解决这个问题吗?

我正在处理表单,表单内容分成不同的页面。在进入表单的下一页之前,您需要填写所有必填的EditTexts。那么当" next"单击按钮,我想检查我的表单edittexts为空输入。如果Edittexts保留为空,则会在它们周围绘制边框,您无法继续。

我的问题是,即使我填写了一些Edittexts,它们仍然被认为是空的。所以我填写它们,点击" next"按钮,并在每个按钮周围绘制边框,无论是否为空。

基本上这是导致问题的方法:

public void Border(View view){

    if (view instanceof EditText) {

        EditText edt = (EditText) view;


        if (edt.getText().toString().trim().length() == 0) {
        edt.setBackgroundResource(R.drawable.border);
        }
    }
}

所以我想检查(如果给定的视图是EditText)EditText是否为空。 如果是的话,我想在它周围画一个边框。 显然,如果(edt.getText()。toString()。trim()。length()== 0)总是如此。 方法Border()从这里调用:

public boolean CheckInput(View v) {
    EditText phone = (EditText) findViewById(R.id.phonenr_et);
    EditText email = (EditText) findViewById(R.id.email_et);
    CheckBox box = (CheckBox) findViewById(R.id.agreement_checkbox);

    if (v.getId() != R.id.job_hunting_btn) {

        LinearLayout layout = (LinearLayout) findViewById(R.id.job_hunt_lay);

        if (layout.getChildCount() != 0) {
            for (int i = 0; i < layout.getChildCount(); i++) {
                View view = layout.getChildAt(i);
                Border(view);  // <-- Border is called
            }
        }
    }
        ...
        }

在CheckInput()中,我循环浏览视图Child并将它们放入我的Border Method中。这里调用CheckInput():

    public void OnNextButtonClick(View v) {
    if (CheckInput(v)) {
        ViewList.get(CurrentViewId).setVisibility(View.GONE);
        if (v.getId() == R.id.job_hiring_btn)
            CurrentViewId = ArbeitgeberViewId;
        else {

            CurrentViewId++;
            if (CurrentViewId == ArbeitgeberViewId)
                CurrentViewId = LastViewId;
        }
        ViewList.get(CurrentViewId).setVisibility(View.VISIBLE);
    }
}

在这里,我想确保您首先检查输入,然后才能继续下一部分表单。

这是我的OnCreate()方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setContentView(R.layout.content_main);
    setContentView(R.layout.job_hiring_data1);
    setContentView(R.layout.job_hiring_data2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    ViewList.add(findViewById(R.id.content_main_lo));


    ViewList.add(findViewById(R.id.job_hiring_data1_lo));
    ViewList.add(findViewById(R.id.job_hiring_data2_lo));
    ViewList.add(findViewById(R.id.job_hunt_lay));


    ArbeitgeberViewId = ViewList.size();


    ViewList.add(findViewById(R.id.offered_job_data1_lo));


    LastViewId = ViewList.size();
    ViewList.add(findViewById(R.id.formular_finished_lo));

    ImprintViewId = ViewList.size();
    ViewList.add(findViewById(R.id.imprint_lo));


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

我需要&#34;膨胀&#34; Edittext首先还是什么?老实说,这是一个小组项目,所以在OnCreate()中看到的一切都是由一个小组成员创建的,我真的不知道那里发生了什么 -

我希望你能帮助我!

提前谢谢!

3 个答案:

答案 0 :(得分:0)

试试这个..

在onCreate外面声明(范围仍然在类中)

private EditText phone;

然后在onCreate中初始化(确保在初始化之前使用setContentView设置相应的布局)

 phone = (EditText) findViewById(R.id.phonenr_et);

并在方法checkInput中使用它作为

phone.getText().equals("");

尝试在布局文件中包含其他布局作为以下xml代码,然后将setContentView包含在onCreate中的单个布局文件中。

<include layout="@layout/content_main" />

或手动将所有布局文件合并到一个布局中,然后将setContentView合并到onCreate中的布局文件

答案 1 :(得分:0)

你的问题是你的布局(坏组成员;))。

您正在使用四次setContentView,因此只保留最后一次。

setContentView(R.layout.activity_main);
setContentView(R.layout.content_main);
setContentView(R.layout.job_hiring_data1);
setContentView(R.layout.job_hiring_data2);

使用可以连接这些布局的布局,这应该会有所帮助。

Official doc

  

void setContentView(int layoutResID)

     

从布局资源设置活动内容。资源将被夸大,将所有顶级视图添加到活动中。

答案 2 :(得分:-1)

通过以下代码检查:

insert into #step values(2,19,20);
insert into #step values(1,2,10);
insert into #step values(19,5,100);
insert into #step values(5,16,200);
insert into #step values(4,5,50);
insert into #step values(16,4,500); -- add this to check we dont go further once we reach 16

declare @start int = 2;
declare @end int = 16;

with cte(nrTo, val) as
(
    select nrTo, value
    from #step
    where nrFrom = @start

    union all

    select #step.nrTo, value
    from cte
    inner join #step on #step.nrFrom = cte.nrTo
    and #step.nrFrom != @end 
) 
select sum(val)
from cte
相关问题