设置editText密码可见性和特殊字符过滤

此方法中的过滤规则可以根据需求自定义,此方法还可以用于其它类型输入检测

private static boolean checkLegalCharacters(String name, boolean isPasswd) {

        Pattern p1 = Pattern.compile("[0-9]*");//数字
        Pattern p2 = Pattern.compile("[a-zA-Z]");//字母
        Pattern p3 = Pattern.compile("[\u4e00-\u9fa5]");//中文
        String specialCharacters = "`~!@#$%^&*()-_=+,.;':|><?";
//        Pattern p4 = Pattern.compile( "`~!@#$%^&*()-_=+,.;':|><?");//特殊字符
        String n = "";
        for (int i = 0; i < name.length(); i++) {
            n = name.substring(i, i + 1);
            Matcher m1 = p1.matcher(n);
            Matcher m2 = p2.matcher(n);
            if (isPasswd) {
//                Matcher m4 = p4.matcher(n);
//                LogHelper.i("test","!m4.matches()=="+m4.matches()+"==="+name+"==="+n);
                if (!m1.matches() && !m2.matches() && !specialCharacters.contains(n)) {
//                if (!m1.matches() && !m2.matches() && !m4.matches()) {
                    return false;
                }
            } else {
                Matcher m3 = p3.matcher(n);
                if (!m1.matches() && !m2.matches() && !m3.matches()) {
                    return false;
                }
            }
        }
        return true;

    }


**************************************

  /**
     * @Name:setPasswordVisible
     * @Description:TODO
     * @param:@param editText
     * @param:@param passwdHin true隐藏密码
     * @return:void
     */
    public static void setPasswordVisible(EditText editText, boolean passwdHin) {
        if (!passwdHin) {
            // 显示密码
//            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            // 隐藏密码
//            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
             editText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
//        editText.postInvalidate();
        editText.setSelection(editText.getText().length());

        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(18), new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                for (int i = start; i < end; i++) {
                    String s = Character.toString(source.charAt(i));
                    if (!checkLegalCharacters(s, true)) {
                        return "";
                    }
                }
                return null;
            }
        }});
    }
 

Android中,你可以通过设置EditText组件的`maxLength`属来限制密码输入框(通常使用`android:password="true"`使其显示星号而非实际字符)的字符数。以下是具体的步骤: 1. 首先,在XML布局文件中创建EditText,并添加`android:maxLength`属,例如限制最大16位字符: ```xml <EditText android:id="@+id/password_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" <!-- 设置密码输入模式 --> android:maxLength="16" <!-- 设置最大字符数为16 --> android:singleLine="true" <!-- 确保用户无法换行输入 --> /> ``` 2. 如果你想在程序运行时动态改变最大长度,可以在Activity或Fragment中找到EditText控件并修改它的`maxLength`值。 ```java EditText passwordInput = findViewById(R.id.password_input); passwordInput.setFilters(new InputFilter[]{new InputFilter.LengthFilter(16)}); // 设置最大长度为16 ``` 或者 ```java int maxLength = 16; passwordInput.setMaxLines(1); passwordInput.setTextWatcher(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() > maxLength) { passwordInput.setSelection(maxLength); } } @Override public void afterTextChanged(Editable s) {} }); ``` 在这个例子中,如果用户试图输入超过16个字符,文本将不会超出限制,并且光标会自动移动到限制后的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值