Android-是否可以将可点击链接添加到字符串资源中

问题描述 投票:32回答:3

当用户第一次使用我的应用程序时,我通常会设置某种AlertDialog,并解释如何使用该应用程序,并对他们刚刚下载的内容进行全面介绍。我通常也会从strings.xml文件加载我的字符串。

我想要做的是使我的字符串资源中的一个单词可以像网页上的超链接一样点击。基本上你有一个AlertDialog,在字符串资源中会有一个突出显示的单词,或者可能只是一个他们可以按的网址。我想我可以添加一个按钮,将它们带到网站,但我只是想知道在你的字符串资源中是否可以使用可点击的超链接。

android xml hyperlink alertdialog
3个回答
54
投票

只需在资源中使用HTML格式链接:

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

然后,您可以在setMovementMethod(LinkMovementMethod.getInstance())上使用TextView使链接可点击。

还有TextViewandroid:autoLink属性也应该有效。


10
投票

我找到了有趣的东西。如果你们中有人观察到这一点,请告诉我。

如果您使用,下面的超链接不起作用

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>

4
投票

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" />
© www.soinside.com 2019 - 2024. All rights reserved.