如何使textview的每个单词可点击



伙计们,这是示例文本:"我想要这个[option1/option2]可点击。"

当用户点击一个选项(或一个单词),我将保存点击的单词到一个变量。如何使这些选项可点击?

这是一个您需要的示例相同的博客这对我帮助很大。链接到博客

来自博客的代码

在你的主活动中设置链接。

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());

获取参数:

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"));

在Manifest中声明你的第二个活动为:

<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>

如果不检查onTouch事件中textview的哪个部分被触摸(包括检查textview中触摸的x,y位置)

做你想做的事很容易使用2个文本框和设置2个不同的onClickListener

关于如何设置clicklistener
的更多细节请查看http://developer.android.com/guide/topics/ui/ui-events.html

最新更新