简体   繁体   English

如何使每个文本视图的单词可点击?

[英]how to make each word of a textview clickable?

Guys this is the sample text: "I want this [ option1 / option2 ] clickable." 伙计们,这是示例文本:“我希望此[option1 / option2]可点击。”

What i what to happen is when user click an option( or a word) i will save the clicked word to a variable. 我将要发生的事情是,当用户单击一个选项(或一个单词)时,我会将单击的单词保存到变量中。 How to make that options clickable? 如何使该选项可点击?

Thanks for any help.. 谢谢你的帮助..

This is a blog having example same as you need It helped me a lot. 这是一个博客,其中有您需要的示例,它对我有很大帮助。 Link To Blog 链接到博客

Code from Blog how can you do this. 来自Blog的代码如何执行此操作。

In Your Main Activity set Links This way. 在您的主要活动中,设置“这种方式的链接”。

mTextSample = (TextView) findViewById(R.id.text1);
        text = "<html>Visit my blog <a href=\"http://sherifandroid.blogspot.com\">mysite</a> or run the <a href=\"sherif-activity://myactivity?author=sherif&nick=king\">myactivity</a> callback</html>";  
        mTextSample.setText(Html.fromHtml(text));
        mTextSample.setMovementMethod(LinkMovementMethod.getInstance());

To get the Parameters : 获取参数:

TextView mTextSample = (TextView) findViewById(R.id.text);
        mTextSample.setText(getIntent().getData().getQueryParameter("author"));
        mTextSample = (TextView) findViewById(R.id.text1);
        mTextSample.setText(getIntent().getData().getQueryParameter("nick"));

In Manifest Declare your second Activity as : 在清单中,将您的第二个活动声明为:

<activity android:label="@string/app_name" android:name="sherif.android.linkify.example.TestActivity"> 
   <intent-filter> <data android:scheme="sherif-activity" android:host="myactivity"/> 
   <action android:name="android.intent.action.VIEW"/> 
   <category android:name="android.intent.category.DEFAULT"/> 
   <category android:name="android.intent.category.BROWSABLE"/> 
   </intent-filter> 
</activity>

can't do that easily without checking inside the text view in an onTouch event which part of the textview is touched (involves checking x,y position of touch inside textview ) 如果不在onTouch事件中检查文本视图的哪一部分被触摸(在文本视图中检查触摸的x,y位置),就无法轻松地做到这一点

to do what you want to do easily use 2 textboxes and set 2 different onClickListener 要轻松地完成您想做的事情,请使用2个文本框并设置2个不同的onClickListener

check this out for more detail http://developer.android.com/guide/topics/ui/ui-events.html on how to set clicklisteners 有关更多信息,请参见http://developer.android.com/guide/topics/ui/ui-events.html,以了解更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM