Java 如何在TextInput上提示错误
我只想在用户键入时在Java 如何在TextInput上提示错误,java,android,Java,Android,我只想在用户键入时在TextInput上显示一个错误提示。 例如,如果输入长度小于8,我想在用户键入TextInput时显示密码太短错误。您可以使用附加到编辑文本的textChangedListener。 请参见简单示例: Field1.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { if(s.length() < 6)
TextInput
上显示一个错误提示。
例如,如果输入长度小于8,我想在用户键入
TextInput时显示密码太短
错误。您可以使用附加到编辑文本的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) {
});
Field1.addTextChangedListener(新的TextWatcher(){
@凌驾
公共无效后文本已更改(可编辑){
如果(s.长度()<6)
//显示消息太短
}}
@凌驾
更改前的公共无效(字符序列、整数开始、,
整数计数,整数后){
}
@凌驾
public void onTextChanged(字符序列,int start,
前整数,整数计数){
});
使用
et_password.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i1,int i2){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i1,int i2){
如果(charSequence.length()将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) {
}
});
EditText密码=(EditText)findViewById(R.id.password\u et);
password.addTextChangedListener(新的TextWatcher(){
@凌驾
更改前的公共无效(CharSequence CharSequence,int i,int i2,int i3){
}
@凌驾
public void onTextChanged(CharSequence CharSequence,int i,int i2,int i3){
if((charSequence.toString()).length<6){
Toast.makeText(这是“密码长度小于6”,Toast.length_SHORT).show();
}
}
@凌驾
public void PostTextChanged(可编辑){
}
});
这是文本输入布局
在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;
}
passwordTIL=(TextInputLayout)findViewById(R.id.input\u layout\u password);
passwordET=(EditText)findViewById(R.id.textVIew\u password);
passwordET.addTextChangedListener(新签名ExtWatcher(passwordET)
//您也可以将其用作用户名,或者检查电子邮件格式是否正确。
私有类SignentExtWatcher实现TextWatcher{
私人视野;
私人登录ExtWatcher(查看){
this.view=视图;
}
更改前的公共无效(CharSequence CharSequence,int i,int i1,int i2){
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
}
public void PostTextChanged(可编辑){
开关(view.getId()){
案例R.id.textView\u密码:
验证密码();
打破
}
}
}
私有布尔validatePassword(){
if(passwordET.getText().toString().trim().isEmpty()){
passwordTIL.setError(“空错误消息”);
请求焦点(passwordET);
返回false;
}else if(passwordET.getText().toString().length()<6){
passwordTIL.setError(“短密码错误消息”);
请求焦点(passwordET);
返回false;
}否则{
passwordTIL.setErrorEnabled(false);
}
返回true;
}
您也可以使用validatePassword()函数启用/禁用登录按钮downvoter能否解释原因?
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;
}