[英]android textview clickable words
所以我有这个活动B和textview 。 要显示到textview中的字符串来自我以前的Activity A (一个包含多个记录的结果查询/通过Bundle传递)
我的问题是你如何使每个记录可点击? 请注意,在我的活动B中,我只有一个textview来查看活动A中的字符串。 我希望每个单词都可以单击,然后它将转到相应的活动。
仅供参考,这是精选的
更新:
Result.class
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.symptomsresult);
result = (TextView) findViewById (R.id.tvSymptomResult);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("results");
result.setText(value);
}
else{
}
}
Symptoms.class
String[] symptoms = alSymptoms.toArray(new String[alSymptoms.size()]);
String c = dbHelper.getData(symptoms);
final int result = 1;
Bundle extras = new Bundle();
Intent i = new Intent(this, SymptomsResult.class);
extras.putString("results", c);
i.putExtras(extras);
startActivityForResult(i, result);
DBHelper.class
public String getData(String[] symptoms) {
String where = KEY_SYMPTOMS + "= ?";
String orStr = " OR "; /* Maybe " AND "? */
StringBuilder builder = new StringBuilder(where);
for(int i = 1; i < symptoms.length; i++)
builder.append(orStr).append(where);
String search = "";
Cursor c = myDataBase.query(true, DB_TABLE, new String[]
{KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
c.moveToFirst();
while (c.isAfterLast() == false) {
search += c.getString(0) + ", ";
c.moveToNext();
}
return search;
}
任何帮助表示赞赏。 谢谢!
您可以使用HTML href
作为文本的值,然后添加此行以使其显示为Web链接
txtView.setMovementMethod(LinkMovementMethod.getInstance());
您可以通过使用自定义scheme
建立链接来打开您的活动
示例代码:
txtView.setText(Html.fromHtml(String.format("<a href='%s://%s'>%s</a>",
schema,action,text)));
对于单击此链接时要打开的活动,您必须在manifest
添加以下内容
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="android.intent.action.VIEW" />
<data
android:host = "YOUR_ACTION"
anroid:scheme="YOUR_SCHEME" />
</intent-filter>
这里的诀窍是您要创建自定义URL而不是host
您可以设置一个可以设置为启动不同活动的操作。 请注意, action/host
和scheme
可以是任意字符串,只要它们与您在创建链接时使用的值匹配即可
因此,当您使用此类链接单击该文本时,它将调用与scheme/host
匹配的活动
我正在使用这个解决方案,与你的情况非常相似,而且它运行得很好。
您可以使用Linkify类和Spannables执行此操作 。 但我相信ListView是一个更好的选择,项目更容易点击,因为它们更大,这种方法有更多的文档。
您仍然可以使用我在getData()
编写的代码,而不是将Cursor转换为String,将Cursor传递给SimpleCursorAdapter。
那么如何将它们放在列表视图中呢?
像这样修改getData()
:
public Cursor getData(String[] symptoms) {
String where = KEY_SYMPTOMS + "= ?";
String orStr = " OR "; /* Maybe " AND "? */
StringBuilder builder = new StringBuilder(where);
for(int i = 1; i < symptoms.length; i++)
builder.append(orStr).append(where);
return myDataBase.query(true, DB_TABLE, new String[]
{"rowid as _id", KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
}
然后将此Cursor传递给CursorAdapter并将其绑定到ListView:
String[] fromColumns = {DBAdapter.KEY_CONDITIONS};
int[] toViews = {android.R.id.text1}; // The TextView in simple_list_item_1
setListAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, getData(symptoms),
fromColumns, toViews, 0));
(我在这里做了一些假设,但这是如何将Cursor绑定到ListActivity。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.