2012-02-09 60 views
21

我通常會設置某種AlertDialog以在用戶第一次使用我的應用程序時觸發,並解釋如何使用該應用程序並給出整體介紹到他們剛剛下載的內容。我通常也從strings.xml文件加載我的字符串。Android-是否可以將可點擊鏈接添加到字符串資源中

我想要做的是讓我的字符串資源中的一個單詞可以像網頁上的超鏈接一樣點擊。基本上你會有一個AlertDialog,在字符串資源中會有一個突出顯示的字或可能只是他們可以按的網址。我想我可以添加一個按鈕,將他們帶到網站,但我只是想知道是否可以在您的字符串資源中創建一個可單擊的超鏈接。

回答

43

只需使用一個HTML格式的鏈接在您的資源:

<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>

然後可以使用setMovementMethod(LinkMovementMethod.getInstance())TextView使鏈接點擊。

還有TextViewandroid:autoLink屬性,它也應該工作。

+1

真棒:)我嘗試設置這個,併發布時,我得到它的工作:) – 2012-02-09 02:22:38

+4

android:autoLink =「網絡」完美地爲我工作。 – Rajkiran 2012-05-25 09:39:16

+1

我發現字符串*中的所有內容都不顯示(所以'這表示並且so does this,但這並不是')。你怎麼做到這一點? – 2013-05-23 17:10:57

5

Android不會使包含有效鏈接的字符串自動點擊。您可以執行的操作是將自定義視圖添加到對話框中,並使用WebView顯示警報消息。在這種情況下,您可以將html存儲在資源中,並且可以點擊。

View alertDialogView = LayoutInflater.inflate(R.layout.alert_dialog_layout, null); 

WebView myWebView = (WebView) alertDialogView.findViewById(R.id.dialogWebView); 
myWebView.loadData("<a href=\"http://google.com\">Google!</a>", "text/html", "utf-8"); 
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this); 
builder.setView(alertDialogView); 

alert_dialog_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="wrap_content" 
android:orientation="vertical"> 
<WebView android:id="@+id/dialogWebView" android:layout_height="wrap_content" 
    android:layout_width="wrap_content" /> 
+0

嘿感謝的作品信息:)我甚至沒有考慮使用webview這很酷。 – 2012-02-09 02:21:36

+2

使用WebView這麼簡單的事情真的是一個壞主意。 – 2017-03-31 14:12:39

3

我發現了一些有趣的東西。讓我知道你們中的任何人是否觀察到這一點。

下面的超鏈接,如果您使用

android:autoLink="web" 

TextView link = (TextView) findViewById(R.id.link); 
link.setMovementMethod(LinkMovementMethod.getInstance()); 

<string name="my_link"> 
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> 
     Click me! 
    </a> 
</string> 

工作不工作,但如果你使用下面的鏈接它用

android:autoLink="web" (or) 
TextView link = (TextView) findViewById(R.id.link); 
link.setMovementMethod(LinkMovementMethod.getInstance()); 

<string name="my_link"> 
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> 
     http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource 
</string> 
    </a> 
相關問題