2014-04-15 104 views
1

我在佈局TextView集TextView中包含一個可點擊的HTML鏈接

<TextView 
    android:id="@+id/homepage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

我想設置一個在這個TextView的HTML鏈接,並使鏈接點擊(點擊時打開瀏覽器上的鏈接)。我試過如下:

我定義資源的字符串,字符串中包含HTML鏈接到谷歌網站:

<string name="home_page">please go to &lt;a ref="www.google.com">www.google.com!&lt;/a>. 

在我的活動:

TextView homepage = (TextView)findViewById(R.id.homepage); 

String text = getString(R.string.home_page) 
CharSequence styledText = Html.fromHtml(text); 

homepage.setText(styledText.toString()); 

結果請到到www.google.com,但www.google.com不是可點擊的鏈接。如何使它可點擊? (我的意思是打開瀏覽器的鏈接被點擊時)

+0

爲此使用可點擊跨度。 http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring – Raghunandan

回答

3

試試這個..

不要忘記www.前使用http://否則你將得到ActivityNotFoundException

TextView homepage = (TextView)findViewById(R.id.homepage); 
homepage.setMovementMethod(LinkMovementMethod.getInstance()); 
homepage.setText(Html.fromHtml("<font color='#696969'> please go to <a href=\""+"http://www.google.com"+"\">"+"http://www.google.com"+"</a></font>")); 
-1
<TextView 
     android:id="@+id/links" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:autoLink="web" 
     android:text="www.google.com" /> 

試試這個代碼,它是爲我工作。

相關問題