How to check if an editText box(s) is empty or not in android using Java [duplicate]

筅森魡賤 提交于 2019-12-23 17:31:45

问题


In my app, I have around 5 editText boxes in every screen.I want to check if they are empty or not and if all the text boxes are not empty, then there is an intent which takes you to the next screen. I want to know what code to write.


回答1:


Just add this code in your java file when you want to call the intent.

EditText editTextBox1 = (EditText) findViewById(R.id.your_editTextBox1_id);
EditText editTextBox2 = (EditText) findViewById(R.id.your_editTextBox2_id);
EditText editTextBox3 = (EditText) findViewById(R.id.your_editTextBox3_id);
EditText editTextBox4 = (EditText) findViewById(R.id.your_editTextBox4_id);
EditText editTextBox5 = (EditText) findViewById(R.id.your_editTextBox5_id);

if((editTextBox1.getText().length() != 0) && (editTextBox2.getText().length() != 0) && (editTextBox3.getText().length() != 0) && (editTextBox4.getText().length() != 0) && (editTextBox5.getText().length() != 0)){
    //Start Activity
}
else{
    //else show an error or anything you want
}

Here, just replace the your_editTextBox1_id with the ID you set in your xml file.

If you want to check it when a Button is clicked, just add this code in the OnClick method of the button.



来源:https://stackoverflow.com/questions/38408960/how-to-check-if-an-edittext-boxs-is-empty-or-not-in-android-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!