点击设置字符串 - 的TextView(Android版) [英] Setting clickable string - Textview (Android)
本文介绍了点击设置字符串 - 的TextView(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的code是这样的,
My code looks like this,
String content = "Hi this is @Naveen. I'll meet @Peter in the evening.. Would you like to join @Sam??";
TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
contentTextView.setText(content);
在在TextView中设置的文字,我想补充的点击事件@Naveen,@Peter和@Sam。当用户点击这些文本我想开一个新的意图..这可能吗?任何指针将是非常有帮助的。
Before setting the text in the textview, I would like to add click event for @Naveen, @Peter and @Sam.. When the user taps on these texts I want to open a new intent.. Is that possible? Any pointers would be quite helpful.
推荐答案
您可以尝试使用的 Linkify 自定义模式。
You can try to use Linkify with custom patterns.
但是,如果不适合你的需求,你可以试试这个:
However, if that doesn't suit your needs you can try this:
SpannableString ss = new SpannableString("Hi this is @Naveen. I'll meet @Peter in the evening.. Would you like to join @Sam??");
ClickableSpan clickableSpanNaveen = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for naveen
}
};
ClickableSpan clickableSpanPeter = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for peter
}
};
ClickableSpan clickableSpanSam = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Do Stuff for sam
}
};
ss.setSpan(clickableSpanNaveen, 11, 17, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanPeter, 29, 35, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(clickableSpanSam, 76, 79, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView contentTextView=(TextView)userHeader.findViewById(R.id.contentTextView);
contentTextView.setText(ss);
contentTextView.setMovementMethod(LinkMovementMethod.getInstance());
这篇关于点击设置字符串 - 的TextView(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文