在创建文本视图在android的可点击的链接 [英] Create clickable link in text view in android

查看:147
本文介绍了在创建文本视图在android的可点击的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些哈希标签在的TextView 这是开始以#

I have some hash tags in a TextView which are starts with '#'

例如:#1#2次Hello世界#three

Example:"#one#two Hello World #three".

我想这些哈希标签可点击单独打开一个活动,让这项活动该文本。

I wants these hash tags clickable separately and open an activity and getting this text in that activity.

因此​​,这些哈希工作为纽带,开辟活动。还标记不是固定的,可能是任何文本的装置。同时改变哈希标签的颜色为红色和标签其余颜色为黑色

So these hash are working as a link and open up an activity. Also the tags are not fixed means that may be any text. Also change the color of hash tags to red and color of rest of tags will be black

示例: #one #two 的Hello World #three

Example: #one#two Hello World #three

推荐答案

修改以下根据您的要求。使用 SpannableString

Modify the below according to your requirement. Use a SpannableString

String s ="#one #Two Hello World #three";
String split[] = s.split("#");
TextView_tv = (TextView) findViewById( R.id.tv );
for(int i=1;i<split.length;i++)
{
  SpannableString ss1=  new SpannableString("#"+split[i]);     
  ss1.setSpan(new  MyClickableSpan(""+i), 0, 1,  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  ss1.setSpan(newForegroundColorSpan(Color.RED),0,1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  _tv.append(ss1);
   _tv.append(" ");   
}
_tv.setMovementMethod(LinkMovementMethod.getInstance());

class MyClickableSpan extends ClickableSpan{    
String clicked;
public MyClickableSpan(String string) {

    super();
    clicked =string;
    }

    public void onClick(View tv) {
        if(clicked.equals("1"))
        {
             Toast.makeText(getApplicationContext(), "One",1000).show();
        }
        else if(clicked.equals("2"))
        {
            Toast.makeText(getApplicationContext(), "Two",1000).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), "Three",1000).show();       
        }

   }

    @Override
    public void updateDrawState(TextPaint ds) {
       ds.setUnderlineText(false); // set to false to remove underline
    }
    } 
   }

快于模拟器

在每个哈希点击显示敬酒之一,二,三。相反,吐司开始一个新的活动。

On each hash click displays toast one, two and three. Instead of toast start a new activity.

编辑:

如果你想字符串点击

 ss1.setSpan(new  MyClickableSpan(""+i,split[i]), 0, 1,  Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

然后

 String clicked;
 String astring;
 public MyClickableSpan(String check,String actualstring) {
super();
clicked =check;
astring =actualstring; // pass this to next activity using intent
}

然后

  public void onClick(View tv) {
        if(clicked.equals("1"))
        {
             Toast.makeText(getApplicationContext(), astring,1000).show();
        }
        else if(clicked.equals("2"))
        {
            Toast.makeText(getApplicationContext(), astring,1000).show();
        }
        else
        {
            Toast.makeText(getApplicationContext(), astring,1000).show();       
        }

   }

这篇关于在创建文本视图在android的可点击的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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