如何添加点击进入听众在Android中/ Java的TextView的字符串? [英] How to add click listeners into a string in Android/Java Textview?

查看:242
本文介绍了如何添加点击进入听众在Android中/ Java的TextView的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么即时试图完成的东西,是标准的大部分Twitter的应用程序,在一个TextView有可能是一个@提及或#井号标签$ P $文本字符串pceding一个字,他们居然能添加一个点击监听到一个启动另一个活动的话,没有人知道这是如何实现的?下面我附上一个例子照片呈现我想要实现的,任何帮助将走很长的路感谢


解决方案

看一看的 Linkify 类。它可以让你对你TextViews中添加链接,对于给定的正前pression。

以下href=\"http://burakdede.com/2012/07/06/how-linkify-your-text-in-android/\">这文章已经从

  TextView的多行文字=(的TextView)findViewById(R.id.mytext);
模式userMatcher = Pattern.compile(\\ B @ [^:\\ S] +);
字符串userViewURL =用户://;
Linkify.addLinks(MTEXT,userMatcher,userViewURL);


  

模式被用于创建给出前reguler pression新格局就像上面的例子其中捕捉任何文字就像在给定的文本@username,那么你必须定义你的用户://方案,这也必须定义在活动,这将赶上点击和最后一Linkify.addLinks让所有的人一起工作。让我们看一下对于意图过滤Android.manifest文件。


 <活动机器人:名字=。DirectMessageActivity>
    &所述;意图滤光器>
        <类机器人:名字=android.intent.category.DEFAULT/>
        <作用机器人:名字=android.intent.action.VIEW/>
        <数据机器人:计划=用户/>
    &所述; /意图滤光器>
< /活性GT;


  

当你点击这个@username是将捕获的点击和处理字符串点击活动。是我们没有提到关于什么是发送到DirectMessageActivity当用户点击@用户名,您可能已经猜到用户名的字符串将被传递到DirectMessageActivity。你可以得到这个字符串是这样的。


 乌里数据= getIntent.getData();
如果(数据!= NULL){
    串的uri = data.toString();
    用户名= uri.substring(uri.indexOf(@)+ 1);
}

What im trying to accomplish is something that is standard in most twitter apps, in a textview there may be an "@" mention or a "#" hashtag preceding a word in a text string and they are actually able to add a click listener onto that word that launches another activity, does anyone know how this is achieved? below I have attached an example photo showing what i'm trying to achieve, any help would go a long way thanks

解决方案

Take a look at the Linkify class. It allows you to add Links within you TextViews, for a given regular expression.

The following has been extracted from this article:

TextView mText = (TextView) findViewById(R.id.mytext);
Pattern userMatcher = Pattern.compile("\B@[^:\s]+");
String userViewURL = "user://";
Linkify.addLinks(mText, userMatcher, userViewURL);

Pattern is used to create new pattern from given reguler expression like example above which catch any text like @username in the given text , then you have to define your user:// scheme this also have to be defined in activity which will catch the clicks and last one Linkify.addLinks make all of them work together. Lets look at Android.manifest file for intent filter.

<activity android:name=".DirectMessageActivity" > 
    <intent-filter>
        <category android:name="android.intent.category.DEFAULT"/> 
        <action android:name="android.intent.action.VIEW" /> 
        <data android:scheme="user" /> 
    </intent-filter> 
</activity>

When you click @username this is the activity that will catch the click and process the clicked string. Yes we didnt mention about what is send to DirectMessageActivity when user click @username , as you might guess "username" string will be passed to DirectMessageActivity. You can get this string like this.

Uri data = getIntent.getData();
if(data != null){
    String uri = data.toString();
    username = uri.substring(uri.indexOf("@")+1);
}

这篇关于如何添加点击进入听众在Android中/ Java的TextView的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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