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


当前回答

使用TextUtils。isEmpty(“文本”);对于单行代码

其他回答

我使用TextUtils来做这个:

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

其他答案是正确的,但要用一种简短的方式

if(editText.getText().toString().isEmpty()) {
     // editText is empty
} else {
     // editText is not empty
}

我曾经做过类似的事情;

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

试试这个

TextUtils.isEmpty(editText.getText());

如果editText为空,试试另一种简单的方法:

String star = editText.getText().toString();

if (star.equalsIgnoreCase("")) {
  Toast.makeText(getApplicationContext(), "Please Set start no", Toast.LENGTH_LONG).show();
}