Android的验证的EditText [英] Android EditText validation
本文介绍了Android的验证的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个的EditText
例如 editText1 和 editText2
我正在输入从的EditText
为名称1 和名2
现在我要验证该的EditText
不点击OK按钮之前空。
结果为此,我使用:
Now I want to validate that the EditText
are not empty before clicking on ok button.
For this I used:
if(editText1==NULL && editText2==NULL) {
textView.setText("please enter Names");
} else {
other code;
}
但它没有验证,并与空的EditText
它显示的点击OK按钮的结果。
结果我自己也尝试了这种方式:
But its not validating, and with empty EditText
it is showing result on clicking ok button.
I have also tried in this way:
if(name1==NULL && name2==NULL) {
textView.setText("please enter Names");
} else {
other code;
}
该怎么办?
推荐答案
请使用:
if(name1.compareTo("") == 0 || name2.compareTo("") == 0) {
// Your piece of code for example
Toast toast=Toast.makeText(getApplicationContext(), "ENTER NAMES", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
} else {
// Your code
}
这肯定会工作是肯定的。
This will work definitely for sure.
这篇关于Android的验证的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文