当字符限制超过时,如何更改编辑文本字体颜色?

时间:2016-03-03 05:05:42

标签: android android-edittext

[当字符数限制超出此图像时,我想更改编辑文字颜色。]

[[1]:http://i.stack.imgur.com/fHTCA.png]

  • 如何在文本更改侦听器上发挥此效果???

3 个答案:

答案 0 :(得分:0)

我建议关注此链接,我相信会告诉您如何。

Android: Different colours for different characters in EditText

答案 1 :(得分:0)

您可以使用Spannable

    EditText tv = (EditText )findViewById(R.id.tv);

    Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
    wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 5, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv.setText(wordtoSpan);

或html格式Html.fromHtml

    String text = "This is <font color='red'>red</font>. This is <font color='blue'>blue</font>.";
    tv.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);

答案 2 :(得分:0)

试试这个我认为这会对你有所帮助:

if(edit_text.length> limit){edit_text.settextcolor("your color code");}