当EditText处于密码模式时,提示似乎以不同的字体显示(courier ?)我该如何避免这种情况?我想提示出现在相同的字体,当EditText不是在密码模式。

我当前的xml:

<EditText 
android:hint="@string/edt_password_hint"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:password="true"
android:singleLine="true" />

当前回答

解决这个问题有很多方法,但每种方法都有优缺点。下面是我的测试

我只面对这个字体问题在某些设备(列表在我的答案)时启用输入密码由

edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

如果我使用android:inputType="textPassword",这个问题不会发生

我曾经尝试过

1)使用setTransformationMethod代替inputType

edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

字体效果会很好 键盘显示不太好(只能显示文本,不能在文本上方显示数字)

2)使用字体。默认的

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);

键盘显示良好, 字体可能工作不太好。示例sans-serif-light是我的应用程序中所有视图的默认字体= setTypeface(Typeface.DEFAULT)后面的>,EditText字体在某些设备中仍然看起来不同

3)使用android:fontFamily="sans-serif"

对于一些设备,它会崩溃,检查我的答案在这里https://stackoverflow.com/a/52421199/5381331。字体看起来也不一样

我的解决方案

在setInputType之前缓存字体,然后重用它

Typeface cache = edtPassword.getTypeface();
edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edtPassword.setTypeface(cache);

测试 一些设备面临字体问题

小米A2 (8.0.1) Pixel XL (8.1.0) Sony Xperia Z5 Au (SOV32) (6.0) 箭NX (F-04G) (6.0.1) 京瓷(S2) (7.0)

一些设备不面临字体问题

三星S4 (SC-04E) (5.0.1) 三星Galaxy Node 5 (5.1.1) 三星S7 Edge (SM-G935F) (7.0)

其他回答

其他答案是大多数情况下的正确解决方案。

但是,如果使用自定义EditText子类,比如在默认情况下应用自定义字体,就会出现一个微妙的问题。如果你在子类的构造函数中设置了自定义字体,如果你设置inputType="textPassword",它仍然会被系统覆盖。

在这种情况下,移动你的样式到onAttachedToWindow后你的超级。onAttachedToWindow电话。

示例实现:

package net.petosky.android.ui;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * An EditText that applies a custom font.
 *
 * @author cory@petosky.net
 */
public class EditTextWithCustomFont extends EditText {

    private static Typeface customTypeface;

    public EditTextWithCustomFont(Context context) {
        super(context);
    }

    public EditTextWithCustomFont(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EditTextWithCustomFont(
            Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * Load and store the custom typeface for this app.
     *
     * You should have a font file in: project-root/assets/fonts/
     */
    private static Typeface getTypeface(Context context) {
        if (customTypeface == null) {
            customTypeface = Typeface.createFromAsset(
                    context.getAssets(), "fonts/my_font.ttf");
        }
        return customTypeface;
    }

    /**
     * Set a custom font for our EditText.
     *
     * We do this in onAttachedToWindow instead of the constructor to support
     * password input types. Internally in TextView, setting the password
     * input type overwrites the specified typeface with the system default
     * monospace.
     */
    @Override protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        // Our fonts aren't present in developer tools, like live UI
        // preview in AndroidStudio.
        if (!isInEditMode()) {
            setTypeface(getTypeface(getContext()));
        }
    }
}

setTransformationMethod方法打破了android:imeOption,并允许回车输入密码字段。相反,我这样做:

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);

我没有在XML中设置android:password="true"。

解决这个问题有很多方法,但每种方法都有优缺点。下面是我的测试

我只面对这个字体问题在某些设备(列表在我的答案)时启用输入密码由

edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

如果我使用android:inputType="textPassword",这个问题不会发生

我曾经尝试过

1)使用setTransformationMethod代替inputType

edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());

字体效果会很好 键盘显示不太好(只能显示文本,不能在文本上方显示数字)

2)使用字体。默认的

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);

键盘显示良好, 字体可能工作不太好。示例sans-serif-light是我的应用程序中所有视图的默认字体= setTypeface(Typeface.DEFAULT)后面的>,EditText字体在某些设备中仍然看起来不同

3)使用android:fontFamily="sans-serif"

对于一些设备,它会崩溃,检查我的答案在这里https://stackoverflow.com/a/52421199/5381331。字体看起来也不一样

我的解决方案

在setInputType之前缓存字体,然后重用它

Typeface cache = edtPassword.getTypeface();
edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edtPassword.setTypeface(cache);

测试 一些设备面临字体问题

小米A2 (8.0.1) Pixel XL (8.1.0) Sony Xperia Z5 Au (SOV32) (6.0) 箭NX (F-04G) (6.0.1) 京瓷(S2) (7.0)

一些设备不面临字体问题

三星S4 (SC-04E) (5.0.1) 三星Galaxy Node 5 (5.1.1) 三星S7 Edge (SM-G935F) (7.0)

我使用这个解决方案来切换字体取决于提示可见性。它与Joe的答案类似,但扩展了EditText:

public class PasswordEditText extends android.support.v7.widget.AppCompatEditText {

    public PasswordEditText(Context context) {
        super(context);
    }

    public PasswordEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        if (text.length() > 0) setTypeface(Typeface.MONOSPACE);
        else setTypeface(Typeface.DEFAULT);
    }

}

我最近在EditText的一个扩展中添加了打开/关闭单空格开关的功能,这可能会对一些人有所帮助。它不使用android:fontFamily,所以兼容<16。