设置文本颜色的Android EDITTEXT特定文本 [英] Set text color for a specific text in Android Edittext
本文介绍了设置文本颜色的Android EDITTEXT特定文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果用户键入一个字符串,它包含@我想改变red.i与textwatcher试图文字的颜色,而是得到了堆栈溢出error.I要改变只有当@为开头的颜色。在code以下
给出 topic.addTextChangedListener(新TextWatcher(){ 公共无效之前onTextChanged(CharSequence中,诠释开始,诠释,
诠释计数){ } 公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
INT后){ } 公共无效afterTextChanged(编辑S){ 如果(s.toString()。匹配((@ \\\\ W +))){
topic.setText(Html.fromHtml(s.toString()的replaceAll(
(@ \\\\ W +),&下;字体颜色='#FFFF0000'> $ 1所述; /字体>中)));
}
}
});
解决方案
如果(s.toString()。匹配((@ \\\\ W +)))
{
topic.setTextColor(Color.parseColor(#FFFF0000));
}
If user types a String and it contains @ i want to change the color of the text to red.i have tried with textwatcher but got stack overflow error.I want to change the color only if the @ is at the beginning.The code is given below
topic.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void afterTextChanged(Editable s) {
if (s.toString().matches("(@\\w+)")) {
topic.setText(Html.fromHtml(s.toString().replaceAll(
"(@\\w+)", "<font color='#ffff0000'>$1</font>")));
}
}
});
解决方案
if (s.toString().matches("(@\\w+)"))
{
topic.setTextColor(Color.parseColor("#ffff0000"));
}
这篇关于设置文本颜色的Android EDITTEXT特定文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文