我只想在用户输入时在TextInput
上显示错误提示。
例如,如果输入长度小于8,我想在用户在Password is too short
上键入时显示TextInput
错误。
答案 0 :(得分:1)
这是文本输入布局
xml布局文件中的
<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/textView_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:inputType="textPassword" />
</android.support.design.widget.TextInputLayout>
你的活动应该是这样的。
passwordTIL = (TextInputLayout) findViewById(R.id.input_layout_password);
passwordET = (EditText) findViewById(R.id.textVIew_password);
passwordET.addTextChangedListener(new SigninTextWatcher(passwordET)
//you can use this for username too or to check if the email format is correct or not.
private class SigninTextWatcher implements TextWatcher {
private View view;
private SigninTextWatcher(View view) {
this.view = view;
}
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable editable) {
switch (view.getId()) {
case R.id.textView_password:
validatePassword();
break;
}
}
}
private boolean validatePassword() {
if (passwordET.getText().toString().trim().isEmpty()) {
passwordTIL.setError("Empty error message");
requestFocus(passwordET);
return false;
} else if(passwordET.getText().toString().length() < 6){
passwordTIL.setError("Short password error message");
requestFocus(passwordET);
return false;
}else {
passwordTIL.setErrorEnabled(false);
}
return true;
}
您也可以使用validatePassword()函数来启用/禁用登录按钮
答案 1 :(得分:0)
您可以使用附加到编辑文本的textChangedListener。 见简单示例:
Field1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
if(s.length() < 6)
// show message too short
}}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
});
答案 2 :(得分:0)
使用
et_password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.length()<6){
et_password.setError("The password must contain 6 characters");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
答案 3 :(得分:0)
将addTextChangedListener()添加到您的editText。
EditText password = (EditText) findViewById(R.id.password_et);
password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if((charSequence.toString()).length < 6){
Toast.makeText(this, "Password length less than 6", Toast.LENGTH_SHORT).show();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});