跨越Android的一个特定的词 [英] Span a particular word in android
本文介绍了跨越Android的一个特定的词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hello All
在我的Android项目中,我想显示一个特定的词说,在串TRIAL这是我的Trial.This审判只是为了好玩。
在android系统如何才能做到这一点。
In my android project i would like to display a particular word say TRIAL in string This is for my Trial.This Trial is just for fun. How can achieve this in android.
我试着用下面code,将仅格式化从15到30的字符串。
I tried using below code which will format only the string from 15 to 30.
TextView TV = (TextView)findViewById(R.id.mytextview01);
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);
TV.setText(WordtoSpan);
请让我知道如果我能做到这一点的机器人。
Please let me know if i could do this in android.
请转发您的宝贵建议。
在此先感谢:)
推荐答案
在镰刀的回答扩展位,具有全部更换:
Expanding a bit on Scythe's answer, replace them all with:
public class Junk extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textView1);
String tvText = "This is for my Trial.This Trial is just for fun. Just a Trial";
tvText+="A Trial. Only a Trial. It's a Trial";
String lookFor = "Trial";
tv.setText(formatString(tvText, lookFor));
}
private Spannable formatString(String targetStr, String lookFor) {
Spannable spanString = null;
spanString = new SpannableString(targetStr);
int fromIndex, startSpan, endSpan;
fromIndex = 0;
while (fromIndex < (targetStr.length() - 1)) {
startSpan = targetStr.indexOf(lookFor, fromIndex);
if (startSpan == -1)
break;
endSpan = startSpan + lookFor.length();
spanString.setSpan(new ForegroundColorSpan(Color.BLUE), startSpan,
endSpan, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
fromIndex = endSpan;
}
return spanString;
}
}
这篇关于跨越Android的一个特定的词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文