How to check if an editText box(s) is empty or not

2019-07-06 20:23发布

This question already has an answer here:

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条回答
你好瞎i
2楼-- · 2019-07-06 20:44

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.

查看更多
登录 后发表回答