我在android中有5个EditTexts。我想知道我是否可以检查所有5个EditTexts是否为空。有什么办法可以做到吗?


当前回答

尝试使用If ELSE If条件。您可以轻松地验证editText字段。

 if(TextUtils.isEmpty(username)) {
                userNameView.setError("User Name Is Essential");
                return;
         } else  if(TextUtils.isEmpty(phone)) {
                phoneView.setError("Please Enter Your Phone Number");
                return;
            }

其他回答

我使用TextUtils来做这个:

if (TextUtils.isEmpty(UsernameInfo.getText())) {
    validationError = true;
    validationErrorMessage.append(getResources().getString(R.string.error_blank_username));
}

以下几点对我来说很有用:

if(searchText.getText().toString().equals("")) 
    Log.d("MY_LOG", "Empty");

首先,我从EditText检索文本,然后将其转换为字符串,最后使用.equals方法将其与“”进行比较。

我曾经做过类似的事情;

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

你可以使用TextUtils。isEmpty就像我的例子! 祝你好运

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");

}

对所有五个编辑文本都这样做吗