首页 > 代码库 > Edittext password属性为true导致hint全角问题

Edittext password属性为true导致hint全角问题

hint字体受EditText的text字体影响,即受android:typeface的影响。android:typeface有三个取值:“sans”,“serif”,“monospace”,看源码:

setInputType中:

  if (isPassword) {
            setTransformationMethod(PasswordTransformationMethod.getInstance());
            setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
        } else if (isVisiblePassword) {
            if (mTransformation == PasswordTransformationMethod.getInstance()) {
                forceUpdate = true;
            }
            setTypefaceFromAttrs(null /* fontFamily */, MONOSPACE, 0);
        } else if (wasPassword || wasVisiblePassword) {
            // not in password mode, clean up typeface and transformation
            setTypefaceFromAttrs(null /* fontFamily */, -1, -1);
            if (mTransformation == PasswordTransformationMethod.getInstance()) {
                forceUpdate = true;
            }
        }

构造函数中:

  if (password || passwordInputType || webPasswordInputType || numberPasswordInputType) {
            setTransformationMethod(PasswordTransformationMethod.getInstance());
            typefaceIndex = MONOSPACE;
        } else if (mEditor != null &&
                (mEditor.mInputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION))
                == (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD)) {
            typefaceIndex = MONOSPACE;
        }
可以看到当为密码栏时字体是
MONOSPACE

默认Sans,所以会导致字体不一致,找到问题的根源那么解决办法就有了,设成默认的就好:

passEdit.setTypeface(Typeface.SANS_SERIF);


Edittext password属性为true导致hint全角问题