2012-01-24 39 views
8

我想知道是否以及如何將Web鏈接添加到TextView小部件。在我的應用程序中,我顯示了一些文字並在文字旁邊顯示了一張圖像。我想在文本中插入可點擊的互聯網鏈接。這可能嗎?將網頁鏈接添加到TextView小部件

+1

http://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable – VicVu

回答

10

您只需要設置android:autolink屬性。

<TextView 
     android:autoLink="web" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     android:text="http://www.google.com" /> 
+0

Thx,它完美的工作! –

+0

非常直接,容易添加。謝謝! +1 – Darrell

0

這是我如何做它通過代碼

private void setAsLink(TextView view, String url){ 
     Pattern pattern = Pattern.compile(url); 
     Linkify.addLinks(view, pattern, "http://"); 
     view.setText(Html.fromHtml("<a href='http://"+url+"'>http://"+url+"</a>")); 
    } 
0

如果您的網站鏈接是你是顯示在TextView的文本中的不同:

TextView的在你的佈局文件:

<TextView 
    android:id="@+id/textview_with_hidden_clickable_link" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/string_with_text_and_link"/> 

你的字符串資源文件:

<string name="string_with_text_and_link"> 
    <a href="http://any_web_site">The text in your TextView</a> 
</string> 

而在你的Java類:

((TextView)findViewById(R.id.textview_with_hidden_clickable_link)) 
    .setMovementMethod(LinkMovementMethod.getInstance()); 

注:http://在字符串資源是必要的。