Android: Check if EditText is Empty when inputType

2020-02-21 08:33发布

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.

I've already looked at this question: Check if EditText is empty. but it does not address the case where 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.

9条回答
仙女界的扛把子
2楼-- · 2020-02-21 08:40

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
}

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-21 08:42

First Method

Use TextUtil library

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;
}
查看更多
你好瞎i
4楼-- · 2020-02-21 08:50

You can check using the TextUtils class like

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
}
查看更多
爷的心禁止访问
5楼-- · 2020-02-21 08:51

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
}
查看更多
三岁会撩人
6楼-- · 2020-02-21 08:52

Add Kotlin getter functions

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 you don't want to extend this functionality, the original Kotlin is:

edittext.text.isBlank()
// or
edittext.text.isEmpty()
查看更多
不美不萌又怎样
7楼-- · 2020-02-21 08:53

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;
}
查看更多
登录 后发表回答