简体   繁体   English

无法在密码提示中使用自定义字体Android Studio

[英]Can't use custom font in password hint Android Studio

I am trying to use a custom font in a password hint field in Android Studio. 我正在尝试在Android Studio的密码提示字段中使用自定义字体。

After I set the Edit Text as inputType="textPassword" in XML, the font changes to a different from the one I've chosen. 在将XML中的inputType="textPassword"编辑文本inputType="textPassword"设置为inputType="textPassword"之后,字体更改为与我选择的字体不同的字体。 All the other Edit Texts have the correct custom font. 所有其他编辑文本均具有正确的自定义字体。

I've tried following this solution in java code (replaced Typeface.DEFAULT with R.font.my_font ) but nothing happened 我已经尝试在Java代码中遵循解决方案(将Typeface.DEFAULT替换为R.font.my_font ),但是没有任何反应

    EditText password = (EditText) findViewById(R.id.register_password_text);
    password.setTypeface(R.font.my_font);
    password.setTransformationMethod(new PasswordTransformationMethod());

I've also tried this solution with no success 我也尝试过这种解决方案,但没有成功

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

Any ideas on what to try or what might be missing in these solutions? 关于尝试什么或这些解决方案可能缺少什么的任何想法?

PS: I'm using montserrat_regular font PS:我正在使用montserrat_regular字体

Its happening because you are passing the id of the font in password.setTypeface(R.font.my_font) not the TypeFace Object. 发生这种情况的原因是您在password.setTypeface(R.font.my_font)中而不是TypeFace对象中传递了字体的ID。 You need to pass TypeFace object in setTypeFace() method , so to make TypeFace object from your font resource Id just add the code 您需要在setTypeFace()方法中传递TypeFace对象,因此要从字体资源ID中创建TypeFace对象,只需添加代码

Typeface typeface = ResourcesCompat.getFont(this, R.font.my_font);
password.setTypeface(typeface);

I think this should work. 我认为这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM