在TextInputLayout中,更改单个字母或字母的提示颜色

时间:2016-09-23 08:51:22

标签: android android-edittext android-design-library android-textinputlayout

我已在我的Activity中以编程方式定义了TextInputLayout。我在TextInputLayout中添加了EditText,以编程方式添加。

现在,我必须设置该edittext的提示颜色。在这里,我只需要改变提示的单个字母的颜色。

Example - Hint is like, Firstname *. Here  i want to change the color of "*" to Red and remaining hint as black in color.

我已经尝试过Html和Spannable String,但两者都不能用于TextInputLayout。

对于Spannable我做了

SpannableString redSpannable = new SpannableString(red);
redSpannable.setSpan(new ForegroundColorSpan(Color.RED), 0, red.length(), 0);
Spanned hint = Html.fromHtml(string + redSpannable);
etDefault[j].setHint(hint);

对于HTML,

I used "font-color"

如果有人知道,请分享您的意见。

3 个答案:

答案 0 :(得分:9)

根据Android Issue Tracker

  

将提示设置为editText会考虑范围,但标签会这样做   没有漂浮在焦点上。

editText.setHint(hint);
  

将提示设置为textInputLayout会忽略范围

textInputLayout.setHint(hint);
  

TextInputLayout使用内部类CollapsingTextHelper来绘制   提示,不支持跨度。

正如他们所说,

  

他们已将此缺陷传递给开发团队并且将会   更新此问题,并提供更多信息。

答案 1 :(得分:0)

我建议你只使用SpannableStringBuilder而不使用Html.fromHtml

SpannableStringBuilder sb = new SpannableStringBuilder("Hint with first letter black");
CharacterStyle cs= new ForegroundColorSpan(ContextCompat.getColor(this, android.R.color.black));
sb.setSpan(cs, 0, 1, 0);
((TextView) findViewById(R.id.description_messages)).setHint(sb);

在这段代码中,第一个字母变成黑色。如果您希望它也是大胆的,请使用:

CharacterStyle cs = new StyleSpan(android.graphics.Typeface.BOLD);

这只是提示EditText。没有浮动文字效果。

答案 2 :(得分:0)

在TextInputLayout中,更改单个字母或字母

的提示颜色
public static readonly DependencyProperty ItemsSourceProperty =
    DependencyProperty.Register("ItemsSource", 
                                typeof(IEnumerable),
                                typeof(MyUserControl),
                                new PropertyMetadata(null, (s, e) => ((MyUserControl)s).OnItemsSourceChanged(e.NewValue as IEnumerable)));

private void OnItemsSourceChanged(IEnumerable newItemsSource) {
    listBox.ItemsSource = newItemsSource;
}
相关问题