Android中的密码提示字体

hpi*_*que 251 passwords fonts android android-edittext

当EditText处于密码模式时,似乎提示以不同的字体显示(courrier?).我怎么能避免这个?我希望提示以与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" />
Run Code Online (Sandbox Code Playgroud)

man*_*sha 387

更改xml中的字体也不适用于我的提示文本.我找到了两种不同的解决方案,其中第二种解决方案对我有更好的行为:

1)android:inputType="textPassword"从你的xml文件中删除,而不是在java中设置它:

EditText password = (EditText) findViewById(R.id.password_text);
password.setTransformationMethod(new PasswordTransformationMethod());
Run Code Online (Sandbox Code Playgroud)

使用这种方法,提示字体看起来不错,但是当您在该编辑字段中键入时,在转换为密码点之前,您不会看到纯文本中的每个字符.此外,在全屏输入时,不会出现点,而是明文中的passoword.

2)留android:inputType="textPassword"在你的xml中.在Java中,ALSO设置了字体和passwordMethod:

EditText password = (EditText) findViewById(R.id.register_password_text);
password.setTypeface(Typeface.DEFAULT);
password.setTransformationMethod(new PasswordTransformationMethod());
Run Code Online (Sandbox Code Playgroud)

这种方法给了我想要的提示字体,并通过密码点给出了我想要的行为.

希望有所帮助!

  • 是`password.setTransformationMethod(new PasswordTransformationMethod());`有必要吗?似乎对我没有好处. (15认同)
  • 请注意,使用第一个解决方案是一个坏主意 - 启用了自动建议的用户将开始在其他应用程序中看到他们的密码,建议他们,因为EditText未被声明为`inputType ="textPassword"` (14认同)
  • 更好的是,请在本页的答案中查看这个非常简单的解决方案:http://stackoverflow.com/a/18073897 (10认同)
  • 太棒了,这是一些奇怪的行为,你不会指望默认! (3认同)

ays*_*nje 192

我在Dialogs Guide中找到了这个有用的提示

提示:默认情况下,当您将EditText元素设置为使用"textPassword"输入类型时,字体系列将设置为等宽字体,因此您应将其字体系列更改为"sans-serif",以便两个文本字段都使用匹配的字体样式.


例如

android:fontFamily="sans-serif"
Run Code Online (Sandbox Code Playgroud)

  • 这很好,除了它只适用于16级及以上的API级别 (29认同)
  • 这应该是公认的答案.它更简单,它来自文档. (20认同)
  • 请注意,虽然它仅适用于16级及更高级别的API,但xml属性不会导致旧设备崩溃,只会忽略它们. (6认同)

Jam*_*ken 32

这就是我为解决这个问题所做的工作.出于某种原因,我没有必要设置转换方法,所以这可能是一个更好的解决方案:

在我的xml中:

<EditText
    android:id="@+id/password_edit_field"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="Password"
    android:inputType="textPassword" />
Run Code Online (Sandbox Code Playgroud)

在我的Activity:

EditText password = (EditText) findViewById( R.id.password_edit_field );
password.setTypeface( Typeface.DEFAULT );
Run Code Online (Sandbox Code Playgroud)

  • 提醒:在按代码更改InputType之后调用setTypeface().如果InputType包含xx_VARIATION_PASSWORD,则不需要setTransformationMethod; (2认同)

rjr*_*rjr 21

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

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);
Run Code Online (Sandbox Code Playgroud)

而且我没有在XML中设置android:password ="true".


Joe*_*Joe 5

提供的答案manisha确实有效,但与默认状态相比,它使密码字段处于非标准状态.也就是说,默认字体也适用于密码字段,包括点替换和在用点替换之前出现的预览字符(以及当它是"可见密码"字段时).

要解决此问题并使其1)看起来和行为完全类似于默认textPassword输入类型,而且2)允许提示文本以默认(非等宽)字体显示,您需要TextWatcher在字段上有一个可以切换的字段fontface在它们之间来回正确,Typeface.DEFAULTTypeface.MONOSPACE根据它是否为空.我创建了一个可用于实现此目的的辅助类:

import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

/**
 * This class watches the text input in a password field in order to toggle the field's font so that the hint text
 * appears in a normal font and the password appears as monospace.
 *
 * <p />
 * Works around an issue with the Hint typeface.
 *
 * @author jhansche
 * @see <a
 * href="http://stackoverflow.com/questions/3406534/password-hint-font-in-android">http://stackoverflow.com/questions/3406534/password-hint-font-in-android</a>
 */
public class PasswordFontfaceWatcher implements TextWatcher {
    private static final int TEXT_VARIATION_PASSWORD =
            (EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_PASSWORD);
    private TextView mView;

    /**
     * Register a new watcher for this {@code TextView} to alter the fontface based on the field's contents.
     *
     * <p />
     * This is only necessary for a textPassword field that has a non-empty hint text. A view not meeting these
     * conditions will incur no side effects.
     *
     * @param view
     */
    public static void register(TextView view) {
        final CharSequence hint = view.getHint();
        final int inputType = view.getInputType();
        final boolean isPassword = ((inputType & (EditorInfo.TYPE_MASK_CLASS | EditorInfo.TYPE_MASK_VARIATION))
                == TEXT_VARIATION_PASSWORD);

        if (isPassword && hint != null && !"".equals(hint)) {
            PasswordFontfaceWatcher obj = new PasswordFontfaceWatcher(view);
            view.addTextChangedListener(obj);

            if (view.length() > 0) {
                obj.setMonospaceFont();
            } else {
                obj.setDefaultFont();
            }
        }
    }

    public PasswordFontfaceWatcher(TextView view) {
        mView = view;
    }

    public void onTextChanged(final CharSequence s, final int start, final int before, final int count) {
        // Not needed
    }

    public void beforeTextChanged(final CharSequence s, final int start, final int count, final int after) {
        if (s.length() == 0 && after > 0) {
            // Input field went from empty to non-empty
            setMonospaceFont();
        }
    }

    public void afterTextChanged(final Editable s) {
        if (s.length() == 0) {
            // Input field went from non-empty to empty
            setDefaultFont();
        }
    }

    public void setDefaultFont() {
        mView.setTypeface(Typeface.DEFAULT);
    }

    public void setMonospaceFont() {
        mView.setTypeface(Typeface.MONOSPACE);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后要使用它,您需要做的就是调用register(View)静态方法.其他所有内容都是自动的(如果视图不需要,可以跳过变通方法!):

    final EditText txtPassword = (EditText) view.findViewById(R.id.txt_password);
    PasswordFontfaceWatcher.register(txtPassword);
Run Code Online (Sandbox Code Playgroud)


Pha*_*inh 5

解决此问题的方法有很多,但每种方法各有利弊。这是我的测试

通过以下方式启用输入密码后,我只会在某些设备(答案末尾列出)中遇到此字体问题

edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
Run Code Online (Sandbox Code Playgroud)

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

我尝试过的东西

1)setTransformationMethod改用inputType

edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
Run Code Online (Sandbox Code Playgroud)
  • 字体效果很好
  • 键盘显示效果不佳(仅显示文本,不显示数字)

2)使用 Typeface.DEFAULT

setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
setTypeface(Typeface.DEFAULT);
Run Code Online (Sandbox Code Playgroud)
  • 键盘显示效果不错
  • 字体可能无法正常工作。示例sans-serif-lightView我的应用程序中所有字体的默认字体=>之后setTypeface(Typeface.DEFAULT),该EditText字体在某些设备中仍然看起来有所不同

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

  • 对于某些设备,它将崩溃,请在此处检查我的答案/sf/answers/3669483961/。而且字体仍然看起来不同

我的解决方案

setInputType重新使用字体之前先对其进行缓存

Typeface cache = edtPassword.getTypeface();
edtPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edtPassword.setTypeface(cache);
Run Code Online (Sandbox Code Playgroud)

测试
某些设备字体字体问题

  • 小米A2(8.0.1)
  • 像素XL(8.1.0)
  • 索尼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)