安卓系统:当在号码/电话上设置inputType时,检查EditText是否为空



我在android中有一个EditText,供用户输入年龄。它被设置为inputType=电话。我想知道是否有办法检查此EditText是否为null。

我已经研究过这个问题:检查EditText是否为空。但它没有解决inputType=phone的情况。

这些,我已经检查过了,不起作用:

(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.

谢谢你的帮助。

您可以使用类似的TextUtils类进行检查

TextUtils.isEmpty(ed_text);

或者你可以这样检查:

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
}

第一种方法

使用TextUtil库

if(TextUtils.isEmpty(editText.getText().toString()) 
{
    Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
    return;
}

第二种方法

private boolean isEmpty(EditText etText) 
{
        return etText.getText().toString().trim().length() == 0;
}

添加Kotlin getter函数

val EditText.empty get() = text.isEmpty() // it == ""
// and/or
val EditText.blank get() = text.isBlank() // it.trim() == ""

有了这些,您只需使用if (edittext.empty) ...if (edittext.blank) ...

如果你不想扩展这个功能,原来的Kotlin是:

edittext.text.isBlank()
// or
edittext.text.isEmpty()
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
}

我用这个方法做同样的工作:

public boolean checkIsNull(EditText... editTexts){
for (EditText editText: editTexts){
  if(editText.getText().length() == 0){
    return true;
  }
}
return false;
}

只需执行以下

String s = (EditText) findViewByID(R.id.age)).getText().toString();
TextUtils.isEmpty(s);

我发现如果用户进入空间,这些测试就会失败,所以我测试空值是否缺少提示

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
}

editText.length()通常适用于我试试这个

if((EditText) findViewByID(R.id.age)).length()==0){
   //do whatever when the field is null`
}

Java:检查EditText是否为空

1) 查找EditText

 ourEditText = view.findViewById(R.id.edit_text);

2) 获取EditText的字符串值

 String ourEditTextString = ourEditText.getText().toString();

3) 删除所有空格

  • 只允许用户输入空格
String ourEditTextNoSpaces = ourEditTextString.replaceAll(" ","");

3) 检查是否为空

boolean isOurEditTextEmpty = ourEditTextNoSpaces.isEmpty();

试试这个。这是我得到的唯一解决方案

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;
}

最新更新