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:14
if(TextUtils.isEmpty(textA.getText())){
            showToast(it's Null");
        }

you can use TextUtils.isEmpty like my Example ! Good luck

查看更多
低头抚发
3楼-- · 2018-12-31 18:14

You can use setOnFocusChangeListener , it will check when focus change

        txt_membername.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View arg0, boolean arg1) {
            if (arg1) {
                //do something
            } else {
                if (txt_membername.getText().toString().length() == 0) {
                    txt_membername
                            .setError("Member name is not empty, Plz!");
                }
            }
        }
    });
查看更多
流年柔荑漫光年
4楼-- · 2018-12-31 18:16

I did something like this once;

EditText usernameEditText = (EditText) findViewById(R.id.editUsername);
sUsername = usernameEditText.getText().toString();
if (sUsername.matches("")) {
    Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
    return;
}
查看更多
像晚风撩人
5楼-- · 2018-12-31 18:16

You can use length() from EditText.

public boolean isEditTextEmpty(EditText mInput){
   return mInput.length() == 0;
}
查看更多
刘海飞了
6楼-- · 2018-12-31 18:19

You can also check all the EditText Strings in one If condition: like this

if (mString.matches("") || fString.matches("") || gender==null || docString.matches("") || dString.matches("")) {
                Toast.makeText(WriteActivity.this,"Data Incomplete", Toast.LENGTH_SHORT).show();
            }
查看更多
只靠听说
7楼-- · 2018-12-31 18:20
EditText edt=(EditText)findViewById(R.id.Edt);

String data=edt.getText().toString();

if(data=="" || data==null){

 Log.e("edit text is null?","yes");

}

else {

 Log.e("edit text is null?","no");

}

do like this for all five edit text

查看更多
登录 后发表回答