【问题标题】:Can't use custom font in password hint Android Studio无法在密码提示 Android Studio 中使用自定义字体
【发布时间】:2019-12-26 15:14:10
【问题描述】:

我正在尝试在 Android Studio 的密码提示字段中使用自定义字体。

在 XML 中将“编辑文本”设置为 inputType="textPassword" 后,字体将变为与我选择的字体不同的字体。所有其他编辑文本都具有正确的自定义字体。

我已尝试在 java 代码中遵循 this 解决方案(将 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());

我也尝试过this 解决方案,但没有成功

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

对尝试什么或这些解决方案中可能缺少什么有任何想法?

PS:我使用的是 montserrat_regular 字体

【问题讨论】:

    标签: android fonts passwords


    【解决方案1】:

    它的发生是因为您将字体的 id 传递给 password.setTypeface(R.font.my_font) 不是 TypeFace 对象。您需要在 setTypeFace() 方法中传递 TypeFace 对象,因此要从您的字体资源 ID 中创建 TypeFace 对象,只需添加代码

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

    我认为这应该可行。

    【讨论】:

      猜你喜欢
      • 2011-03-25
      • 2014-02-03
      • 2014-08-18
      • 1970-01-01
      • 2016-02-20
      • 2015-10-15
      • 1970-01-01
      • 2013-06-20
      • 1970-01-01
      相关资源
      最近更新 更多