【问题标题】:How to prompt error on TextInput [closed]如何在 TextInput 上提示错误 [关闭]
【发布时间】:2017-07-17 18:25:19
【问题描述】:

我只想在用户输入时在TextInput 上显示错误提示。 例如,如果输入长度小于 8,我想在用户输入 TextInput 时显示 Password is too short 错误。

【问题讨论】:

    标签: java android


    【解决方案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() 函数来启用/禁用登录按钮

    【讨论】:

      【解决方案2】:

      您可以使用附加到您的编辑文本的 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) {
      
        });
      

      【讨论】:

        【解决方案3】:

        使用

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

        【讨论】:

        • 投反对票的能解释一下原因吗?
        【解决方案4】:

        将 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) {
        
                }
        });
        

        【讨论】:

          猜你喜欢
          • 2016-04-07
          • 2018-07-29
          • 1970-01-01
          • 2017-06-12
          • 2012-11-01
          • 1970-01-01
          • 2016-10-19
          • 1970-01-01
          • 2017-10-31
          相关资源
          最近更新 更多