Check if EditText is empty. [closed]

2018-12-31 17:26发布

I have 5 EditTexts in android for users to input. I would like to know if I could have a method for checking all the 5 EditTexts if they are null. Is there any way to do this??

30条回答
看风景的人
2楼-- · 2018-12-31 18:12

Other answers are correct but do it in a short way like

if(editText.getText().toString().isEmpty()) {
     // editText is empty
} else {
     // editText is not empty
}
查看更多
君临天下
3楼-- · 2018-12-31 18:12
private boolean hasContent(EditText et) {
       return (et.getText().toString().trim().length() > 0);
}
查看更多
春风洒进眼中
4楼-- · 2018-12-31 18:12
EditText txtUserID = (EditText) findViewById(R.id.txtUserID);

String UserID = txtUserID.getText().toString();

if (UserID.equals("")) 

{
    Log.d("value","null");
}

You will see the message in LogCat....

查看更多
琉璃瓶的回忆
5楼-- · 2018-12-31 18:13

try this :

EditText txtUserName = (EditText) findViewById(R.id.txtUsername);
String strUserName = usernameEditText.getText().toString();
if (strUserName.trim().equals("")) {
    Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
    return;
}

or use the TextUtils class like this :

if(TextUtils.isEmpty(strUserName)) {
    Toast.makeText(this, "plz enter your name ", Toast.LENGTH_SHORT).show();
    return;
}
查看更多
牵手、夕阳
6楼-- · 2018-12-31 18:14
private boolean isEmpty(EditText etText) {
    if (etText.getText().toString().trim().length() > 0) 
        return false;

    return true;
}

OR As Per audrius

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

If function return false means edittext is not empty and return true means edittext is empty...

查看更多
大哥的爱人
7楼-- · 2018-12-31 18:14

Try this

TextUtils.isEmpty(editText.getText());
查看更多
登录 后发表回答