我在登录时有TextInputLayout,当我点击登录按钮时,如果用户名是空的,那么当密码为空时显示与密码相同的错误然后显示错误但是如果用户名为空且密码非空,那时错误未显示那我怎么能解决这个问题呢?我的代码如下!!!
@Override
public void onClick(View v) {
Intent mIntent;
switch (v.getId()) {
case R.id.txtSignIn:
hideKeyboard(mActivity, v);
if (etUserName.getText().toString().equals("")) {
etUserName.requestFocus();
etUserName.setCursorVisible(true);
tilPassword.setErrorEnabled(false);
tilUserName.setErrorEnabled(true);
tilUserName.setError(getString(R.string.username_required));
} else if (etPassword.getText().toString().equals("")) {
etPassword.requestFocus();
etPassword.setCursorVisible(true);
tilUserName.setErrorEnabled(false);
tilPassword.setErrorEnabled(true);
tilPassword.setError(getString(R.string.password_required));
} else {
tilUserName.setErrorEnabled(false);
tilPassword.setErrorEnabled(false);
showToast(mActivity, getString(R.string.successfully_login), 0);
mIntent = new Intent(SignIn.this, DashBoard.class);
startActivity(mIntent);
}
break;
case R.id.txtSignUp:
mIntent = new Intent(mActivity, SignUp.class);
startActivity(mIntent);
break;
case R.id.txtForgotPassword:
mIntent = new Intent(mActivity, ForgotPassword.class);
startActivity(mIntent);
break;
}
try {
} catch (Exception e) {
LOGD(getClass().getSimpleName(), e.getMessage());
LOGE(getClass().getSimpleName(), e.getMessage());
}
}
答案 0 :(得分:0)
使用isEmpty()方法,因为null和"" < - 如果EditText
为空,则无法导出。
如下所示,屏幕上会弹出错误,密码或用户名字段为空
if (etUserName.isEmpty() && etPassword.isEmpty()) {
<your code if nothing is entered>
}
如需其他帮助,请查看以下内容: Check if EditText is empty.