如何使用Java在android中检查editText框是否为空 [英] How to check if an editText box(s) is empty or not in android using Java
问题描述
在我的应用程序中,每个屏幕上大约有5个editText框.我想检查它们是否为空,并且如果所有文本框都不为空,则有一个意图可以将您带到下一个屏幕.我想知道要写什么代码.
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.
推荐答案
当您要调用该意图时,只需将此代码添加到您的java文件中即可.
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
}
在这里,只需将your_editTextBox1_id
替换为在xml
文件中设置的ID
.
Here, just replace the your_editTextBox1_id
with the ID
you set in your xml
file.
如果要在单击Button
时进行检查,只需在按钮的OnClick
方法中添加此代码即可.
If you want to check it when a Button
is clicked, just add this code in the OnClick
method of the button.
这篇关于如何使用Java在android中检查editText框是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!