Setting typeface of hint in TextEdit

2020-02-11 03:25发布

Is it possible to set typeface of hint element of EditText view without changing the typeface of the EditText itself? Thanks.

11条回答
smile是对你的礼貌
2楼-- · 2020-02-11 03:27

Don't know if you just need to have them be different, or if you wanted to specifically set a typeface.

If you just need to style your hint differently than your text, here's how you can do it in Java on a per-field basis

  1. Wrap your hint text in HTML style tags
  2. Pass the marked-up String to Html.fromHtml(String htmlString)
  3. Pass the result of fromHtml() to setHint();

    myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));
    
查看更多
Melony?
3楼-- · 2020-02-11 03:28
mEmailView.setTypeface(Typeface.createFromAsset(getAssets(),
    getString(R.string.app_font)));

((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
     getAssets(), getString(R.string.app_font)));
查看更多
戒情不戒烟
4楼-- · 2020-02-11 03:32

Did you check android:typeface?

This works fine both for the hint and text for the EditText.

查看更多
The star\"
5楼-- · 2020-02-11 03:35

Seems that there is no way to control typeface of hint and typeface of text separately.

查看更多
对你真心纯属浪费
6楼-- · 2020-02-11 03:35

In case somebody still needs a solution for this:

 editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                editText.typeface = ResourcesCompat.getFont(it, if (TextUtils.isEmpty(s.toString())) R.font.franklingothic_book else R.font.franklingothic_demi)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
        })
查看更多
贼婆χ
7楼-- · 2020-02-11 03:35

the best way to handle this problem is to add a TextWatcher to your EditText and dynamically change the typeface when there's no input in the EditText

查看更多
登录 后发表回答