Android:在 Number/Phone 上设置 inputType 时检查 EditText 是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20349522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: Check if EditText is Empty when inputType is set on Number/Phone
提问by user3061111
I have an EditText in android for users to input their AGE. It is set an inputType=phone. I would like to know if there is a way to check if this EditText is null.
我在 android 中有一个 EditText 供用户输入他们的年龄。它被设置为 inputType=phone。我想知道是否有办法检查此 EditText 是否为 null。
I've already looked at this question: Check if EditText is empty.but it does not address the case where inputType=phone.
我已经看过这个问题:检查 EditText 是否为空。但它没有解决 inputType=phone 的情况。
These, I've checked already and do not work:
这些,我已经检查过并且不起作用:
(EditText) findViewByID(R.id.age)).getText().toString() == null
(EditText) findViewByID(R.id.age)).getText().toString() == ""
(EditText) findViewByID(R.id.age)).getText().toString().matches("")
(EditText) findViewByID(R.id.age)).getText().toString().equals("")
(EditText) findViewByID(R.id.age)).getText().toString().equals(null)
(EditText) findViewByID(R.id.age)).getText().toString().trim().length() == 0
(EditText) findViewByID(R.id.age)).getText().toString().trim().equals("")
and isEmpty do not check for blank space.
Thank you for your help.
感谢您的帮助。
回答by Hariharan
You can check using the TextUtils class like
您可以使用 TextUtils 类进行检查,例如
TextUtils.isEmpty(ed_text);
or you can check like this:
或者你可以这样检查:
EditText ed = (EditText) findViewById(R.id.age);
String ed_text = ed.getText().toString().trim();
if(ed_text.isEmpty() || ed_text.length() == 0 || ed_text.equals("") || ed_text == null)
{
//EditText is empty
}
else
{
//EditText is not empty
}
回答by Jawad Zeb
First Method
第一种方法
Use TextUtil library
使用TextUtil 库
if(TextUtils.isEmpty(editText.getText().toString())
{
Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
return;
}
Second Method
第二种方法
private boolean isEmpty(EditText etText)
{
return etText.getText().toString().trim().length() == 0;
}
回答by J.Done
I use this method for same works:
我将这种方法用于相同的作品:
public boolean checkIsNull(EditText... editTexts){
for (EditText editText: editTexts){
if(editText.getText().length() == 0){
return true;
}
}
return false;
}
回答by AZIM MOHAMAD
Simply do the following
只需执行以下操作
String s = (EditText) findViewByID(R.id.age)).getText().toString();
TextUtils.isEmpty(s);
回答by Samer Kasseb
EditText textAge;
textAge = (EditText)findViewByID(R.id.age);
if (TextUtils.isEmpty(textAge))
{
Toast.makeText(this, "Age Edit text is Empty", Toast.LENGTH_SHORT).show();
//or type here the code you want
}
回答by Sunil
I found that these tests fail if a user enters a space so I test for a missing hint for empty value
我发现如果用户输入空格,这些测试就会失败,所以我测试是否缺少空值提示
EditText username = (EditText) findViewById(R.id.editTextUserName);
EditText password = (EditText) findViewById(R.id.editTextPassword);
// these hint strings reflect the hints attached to the resources
if (username.getHint().equals("Enter your username") || password.getHint().equals("Enter Your Password")){
// enter your code here
} else {
// alls well
}
回答by Bobby Sebastian
editText.length() usually works for me try this one
editText.length() 通常对我有用 试试这个
if((EditText) findViewByID(R.id.age)).length()==0){
//do whatever when the field is null`
}
回答by Gibolt
Add Kotlin getter functions
添加 Kotlin getter 函数
val EditText.empty get() = text.isEmpty() // it == ""
// and/or
val EditText.blank get() = text.isBlank() // it.trim() == ""
With these, you can just use if (edittext.empty) ...
or if (edittext.blank) ...
有了这些,您可以使用if (edittext.empty) ...
或if (edittext.blank) ...
If you don't want to extend this functionality, the original Kotlin is:
如果你不想扩展这个功能,原来的 Kotlin 是:
edittext.text.isBlank()
// or
edittext.text.isEmpty()
回答by Kafi Junior
Try this. This is the only solution I got
尝试这个。这是我得到的唯一解决方案
String phone;
try{
phone=(EditText) findViewByID(R.id.age)).getText().toString();
}
catch(NumberFormatException e){
Toast.makeText(MainActivity.this, "plz enterphone Number ", Toast.LENGTH_SHORT).show();
return;
}