小部件textview中的可单击超链接

时间:2013-06-04 19:22:46

标签: android

由于某种原因,我将文本视图设置为可点击并将自动链接设置为网络后仍然无法打开浏览器。我是否需要使用事件监听器然后以这种方式处理它?还是我忽略了一些简单的东西?我将首先发布textview XML代码:

    <TextView
        android:id="@+id/textView6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.11"
        android:autoLink="web"
        android:background="@drawable/back"
        android:clickable="true"
        android:gravity="center"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

3 个答案:

答案 0 :(得分:1)

这段代码是在大约一年前编写的,但我认为它应该可行:

TextView result_view = new TextView(this);
result_view.setText(Html.fromHtml("<a href=" + linkUrl + ">"+ linkTitle + "</a>"));
result_view.setMovementMethod(LinkMovementMethod.getInstance());

答案 1 :(得分:0)

我不得不使用onclick事件来处理浏览器启动。我仍然是Android编程的完全业余爱好者所以我从here得到了信息:

// Register an onClickListener
            Intent broswerIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://www.cnn.com"));
            broswerIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            broswerIntent.setComponent(new ComponentName("com.android.browser",

            "com.android.browser.BrowserActivity"));
            PendingIntent pendingIntent = PendingIntent.getActivity(
                    getApplicationContext(), 0, broswerIntent, 0);

答案 2 :(得分:0)

添加以下行。

final TextView input = new TextView(this);
            alert.setView(input);
            input.setText(Html.fromHtml("www.google.com")); // you can pass your strings over there. 
            Linkify.addLinks(input, Linkify.ALL); 
            alert.setMessage("Test");

希望这会对你有所帮助。

相关问题