android textview可点击的单词

时间:2013-03-01 19:56:52

标签: android textview

所以我有活动B textview 。要显示到textview中的字符串来自我之前的活动A (结果查询包含多个记录/通过捆绑包传递)

我的问题是如何让每条记录都可点击?请注意,在我的活动B中,我只有一个来自活动A 的字符串的文本视图。我希望每个单词都可以单击,然后它将转到相应的活动。

FYI, 这是选定的

enter image description here

更新:

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;

}


感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用HTML href作为文字的值,然后添加此行以使其显示为网络链接

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>

这里的诀窍是您要创建自定义网址,而不是host,您可以设置一个可以启动不同活动的操作。请注意,action/hostscheme可以是任意字符串,只要它们与您在创建链接时使用的值匹配

因此,当您使用此类链接单击该文本时,它将调用与scheme/host

匹配的活动

我使用的解决方案与你的解决方案非常相似,并且工作正常。

答案 1 :(得分:1)

您可以使用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。)