可点击的话里面的TextView的Andr​​oid [英] Clickable words inside of TextView Android

查看:277
本文介绍了可点击的话里面的TextView的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含主题标签的前一个TextView。 #first #second #third 。我的问题是如何检测被点击,所以我可以执行一些动作这包括hashtag - 例如。使这个词的祝酒辞。
这可能使用的TextView小部件?我应该使用一些其他的小部件istead?

I have a textview that contains hashtags ex. #first #second #third. My question is how can I detect which hashtag is clicked so I can perform some action - eg. make toast of the word. Is this possible using TextView widget? Should I use some other widget istead?

更新

我发现我的解决方案使用这例如的。希望这将帮助其他人的未来!

I found my solution using this example. Hope it will help others in the future!

推荐答案

您可以使用spannable字符串来实现这一点:

You can use spannable string to achieve this:

SpannableString ss = new SpannableString("Your string");
String[] words = ss.split(" ");
for(final String word : words){
   if(word.startsWith("#")){
     ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        //use word here to make a decision 
    }
    };
    ss.setSpan(clickableSpan, ss.indexOf(word), ss.indexOf(word) + word.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
}


TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());

这篇关于可点击的话里面的TextView的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆