Android:当我在EditText上应用密码时更改字体

waw*_*los 47 android typeface android-edittext

当用户开始在EditText Android中编写内容时,我使用FloatLabel库(https://github.com/weddingparty/AndroidFloatLabel)添加一些动画.

我的问题是,当我将密码类型应用于我的EditText时,字体似乎被更改.我想保持正常的字体.(见图1)

在此输入图像描述

但是当我添加以下行来应用密码类型时,提示的字体似乎被改变了!

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

在此输入图像描述

And*_*ter 128

以下可能会为您解决.

pass.setTypeface(user.getTypeface());
Run Code Online (Sandbox Code Playgroud)

基本上它只是传递Typeface你的用户名字段并将其作为Typeface密码字段传递.


我在Dialogs API指南中找到了对此的解释.

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

换句话说,可以用XML完成的修复如下:

<EditText
    android:id="@+id/password"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif"
    android:hint="@string/password"/>
Run Code Online (Sandbox Code Playgroud)

  • 什么是“用户”? (2认同)

Cün*_*eyt 6

For some reason I didn't have to set the transformation method so this may be a better solution. In MyActivity:

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