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

62
我通常会在用户首次使用我的应用程序时设置某种类型的AlertDialog,解释如何使用该应用程序并对他们刚下载的内容进行总体介绍。 我还通常从strings.xml文件中加载字符串。
我想做的是使我的字符串资源中的一个单词像Web页面上的超链接一样可点击。 基本上,您将拥有一个AlertDialog,并且在字符串资源中将有一个突出显示的单词或可能只是一个网址,他们可以按下以访问该站点。 我想我可以添加一个按钮,以带他们前往该站点,但我只是想知道是否可以使字符串资源中的单词成为可点击的超链接。
5个回答

96

在你的资源文件中,可以使用HTML格式的链接:

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

然后在你的TextView上使用setMovementMethod(LinkMovementMethod.getInstance())来使链接可点击。

另外,TextView还有一个android:autoLink属性,也可以实现这个功能。


1
太棒了 :) 我会尝试设置并在我成功后回复 :) - James andresakis
1
我发现链接后面的字符串都没有显示出来(例如<string name="s">This shows and <a href="http://foo.bar/">so does this</a> but this doesn\'t</string>)。你怎么解决这个问题? - Andrew Wyld
实际上,@andrew,我也是这么想的,因为在Eclipse中的图形布局裁剪了文本,但在我的手机上运行时,它可以正常呈现整个字符串。 - qix
2
属性应该是“href”,而不是“ref”吗? - riwnodennyk
1
你能发布一下你的实现输出吗?比如它在 GUI 上是什么样子的? - Raulp
2
android:autoLink="web" 在 Android 8.0 版本上可以处理非 HTML 链接,但无法处理 <a></a> 标签链接。使用 setMovementMethod 可以解决这个问题。 - Robin Davies

19
我发现了一些有趣的东西。如果你们中有人注意到了,请告诉我。
下面的超链接在使用时无法正常工作。
android:autoLink="web"

但是可以与……一起使用

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

<string name="my_link">
    <a href="https://dev59.com/BGox5IYBdhLWcg3wcj53">
        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="https://dev59.com/BGox5IYBdhLWcg3wcj53"> 
        https://dev59.com/BGox5IYBdhLWcg3wcj53
    </a>
</string>

3
请使用 autoLink="all" - c0dehunter
1
在</string>之后关闭</a>标签? - Leo DroidCoder
android:linksClickable="true" - Kyriakos Georgiopoulos

10

如@Nikolay Elenkov所回答的,在Kotlin中,我使用了以下方式从字符串资源中使用它(详细步骤适合初学者):

在我的布局中放置一个复选框:

<CheckBox
        android:id="@+id/termsCB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/spacing_normal"
        android:layout_marginTop="@dimen/spacing_normal"
        android:text="@string/terms_and_conditions" />

在 strings.xml 中。
<string name="terms_and_conditions">I read and accept the <a href="http://www.spiffyshow.com/">Terms and Conditions</a></string>

在我的activity类中,在onCreate()方法中:

termsCB.movementMethod = LinkMovementMethod.getInstance()

4

对我而言,超链接总是很好用的,只要我:

  1. 将这两行代码添加到activity/fragment中:
textView.setMovementMethod(LinkMovementMethod.getInstance())
textView.setText(Html.fromHtml(getString(R.string.link)))
  1. 不要在xml中添加任何内容(例如autoLink等)。
  2. strings.xml中定义链接,如下所示:

<string name="link"><![CDATA[<a href="https://google.com">Google链接</a>]]></string>


3

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" />

嘿,谢谢你提供的信息 :) 我甚至没有想过使用 Webview,这很酷。 - James andresakis
14
使用 WebView 来处理这种琐碎的事情真的是个坏主意。 - Eduard B.
Eduard B.,你能详细说明为什么吗? - Mr.Drew

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接