用户输入密码android studio时尝试使文本出现

时间:2017-03-17 09:16:39

标签: java android-studio

我希望在用户输入密钥时显示文本视图,这是我的代码

TextView mustContain, minChar, alphaNumeric;

这些是必须根据特定条件出现的3种不同的TextViews

只要用户输入少于8个字符,就会出现

minChar, 如果用户键入密码而没有大写或小写字符,则应显示mustContain 只要用户没有输入至少一个数字

,就会出现alphaNumeric
 public class AccountDetails extends AppCompatActivity implements TextWatcher

     @Override
        public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {

            }

        @Override
        public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

            final String validatePassword = password.getText().toString();

            if (validatePassword.length()<8 || validatePassword.length()>24){
                minChar.setText("minimum 8 characters, up to 24 characters");
                minChar.setTextColor(Integer.parseInt("#ff0000"));
            } else if (validatePassword.contains("^(?=.*?[A-Z])(?=.*?[a-z]).{8,}$")){

                mustContain.setText("at least one upper-case and lower-case");
                mustContain.setTextColor(Integer.parseInt("#ff0000"));

            } else if (validatePassword.contains("(?=.*?[0-9])")){

                alphaNumeric.setText("contain alphanumeric");
                alphaNumeric.setTextColor(Integer.parseInt("#ff0000"));
            } else {
                minChar.setText("");
                mustContain.setText("");
                alphaNumeric.setText("");
            }

        }

    @Override
    public void afterTextChanged(Editable editable) {
        final String validatePassword = password.getText().toString();

        if (validatePassword.length()<8 || validatePassword.length()>24){
            minChar.setText("minimum 8 characters, up to 24 characters");
            minChar.setTextColor(Integer.parseInt("#ff0000"));
        } else if (validatePassword.contains("^(?=.*?[A-Z])(?=.*?[a-z]).{8,}$")){


            mustContain.setText("at least one upper-case and lower-case");
            mustContain.setTextColor(Integer.parseInt("#ff0000"));

        } else if (validatePassword.contains("(?=.*?[0-9])")){

            alphaNumeric.setText("contain alphanumeric");
            alphaNumeric.setTextColor(Integer.parseInt("#ff0000"));
        } else {
            minChar.setText("");
            mustContain.setText("");
            alphaNumeric.setText("");
        }
    }


    }

1 个答案:

答案 0 :(得分:1)

只需使用setVisibility()函数即可。将textViews的可见性初始化为false,并在完成编码时将可见性设置为true。例如:

minChar.setVisibility(View.INVISIBLE);
if (validatePassword.length()<8 || validatePassword.length()>24){
            minChar.setText("minimum 8 characters, up to 24 characters");
            minChar.setTextColor(Integer.parseInt("#ff0000"));
            minChar.setVisibility(View.VISIBLE);
}