繁体   English   中英

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

[英]EditText getText() always returns empty string

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

我正在处理表单,表单内容分为不同的页面。 在继续进行表格的下一页之前,您需要填写所有必填的EditText。 因此,当单击“下一步”按钮时,我想检查表单编辑文本是否为空输入。 如果Edittexts保留为空,则会在它们周围绘制边框,您将无法继续。

我的问题是,即使我填写了一些Edittext,它们仍然始终被视为空白。 所以我填写它们,单击“下一步”按钮,然后在每个周围画一个边框,无论是否为空。

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

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)始终为true。 从此处调用方法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()中,我遍历视图子级并将其放入我的Border方法中。 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);
}

我是否需要先“添加” Edittext或其他内容? 老实说,这是一个小组项目,所以OnCreate()中看到的所有内容都是由小组成员创建的,我真的不知道发生了什么事-

我希望你能帮帮我 !

先感谢您!

尝试这个..

在onCreate之外声明(范围仍在类内)

private EditText phone;

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

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

并在方法checkInput中将其用作

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

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

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

或将所有布局文件手动组合为一个布局,然后在onCreate中将ContentView设置为布局文件

您的问题是您的布局(糟糕的小组成员;))。

您正在使用四个时间setContentView因此仅保留最后一个。

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

使用将这些布局连接起来的布局,这应该会有所帮助。

官方文件

void setContentView(int layoutResID)

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

通过以下代码检查它:

edittext.getText().toString().equalsIgnoreCase("")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM