Android:当我在 EditText 上应用密码类型时字体会改变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24117178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:48:22  来源:igfitidea点击:

Android : Typeface is changed when i apply password Type on EditText

androidandroid-edittexttypeface

提问by wawanopoulos

I use FloatLabellibrary (https://github.com/weddingparty/AndroidFloatLabel) to add a little animation when user begin to write something in an EditText Android.

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

My problem is that the typeface seems to be changed when i apply the password type to my EditText. I would like to keep the same typeface as normal. (see picture 1)

我的问题是当我将密码类型应用于我的 EditText 时,字体似乎发生了变化。我想保持与往常一样的字体。(见图1)

enter image description here

enter image description here

But when i add the following line to apply password type, the typeface of hint seems to be changed !

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

pass.getEditText().setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

enter image description here

enter image description here

回答by Andrew Schuster

The following might solve it for you.

以下可能会为您解决。

pass.setTypeface(user.getTypeface());

Essentially it just passes the Typefaceof your username field and passes it as the Typefacefor your password field.

本质上,它只是传递Typeface您的用户名字段的 并将其作为Typeface您的密码字段传递。



I found an explanation of this in the Dialogs API Guide.

我在Dialogs API Guide 中找到了对此的解释。

Tip:By default, when you set an EditText element to use the "textPassword"input type, the font family is set to monospace, so you should change its font family to "sans-serif"so that both text fields use a matching font style.

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

In otherwords, a fix that can be done in XML would be as follows:

换句话说,可以在 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"/>

回答by Cüneyt

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());

回答by smallfatter

you can use android:hit, and setHitColor in your activity, it will not affect the normal input

您可以在您的活动中使用 android:hit 和 setHitColor,它不会影响正常输入

回答by BBdev

It may be late to answer this question but i have came across this problem. So thought to answer this. I have solved it by Implementing a CustomTextWatcher.

回答这个问题可能为时已晚,但我遇到了这个问题。所以想到回答这个。我已经解决了Implementing a CustomTextWatcher

Here is the snippet

这是片段

private class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;

    public CustomTextWatcher(EditText e) { 
        mEditText = e;
        mEditText.setTypeface(Typeface.DEFAULT);
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        mEditText.setTypeface(Typeface.DEFAULT);
    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        mEditText.setTypeface(Typeface.MONOSPACE);
    }

    public void afterTextChanged(Editable s) { 
        if(s.length() <= 0){
            mEditText.setTypeface(Typeface.DEFAULT);
        } else {
            mEditText.setTypeface(Typeface.MONOSPACE);
        }


    }
}

Here is how you can use it in your application

以下是如何在您的应用程序中使用它

EditText pin = (EditText) findViewById(R.id.pin);
pin.addTextChangedListener(new CustomTextWatcher(pin));

回答by Gowtham. R

Set the inputType as "text" in the xml,

在xml中将inputType设置为“text”,

<EditText
    android:id="@+id/edtPassword"
    android:inputType="textPassword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/password"
    android:inputType="text"/>

Then make the EditText in the activity as

然后将活动中的 EditText 设为

mEdtConfirmPassword = findViewById(R.id.edtPassword);
mEdtConfirmPassword.setTransformationMethod(new PasswordTransformationMethod());