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

Jam*_*kis 35 xml android hyperlink android-alertdialog

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

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

Nik*_*kov 62

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

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

然后,您可以使用setMovementMethod(LinkMovementMethod.getInstance())TextView来使链接可单击.

还有TextView一个android:autoLink属性也应该起作用.

  • android:autoLink ="web"对我来说非常适合. (5认同)
  • 该属性应该是“ href”,而不是“ ref”吗? (2认同)
  • android:autoLink="web" 适用于 v26 Oreo 上的非 html 链接;但不适用于 &lt;a&gt;&lt;/a&gt; 标记的链接.. setMovementMethod 确实有效.. (2认同)

Pra*_*ash 12

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

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

android:autoLink="web"
Run Code Online (Sandbox Code Playgroud)

但合作

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>
Run Code Online (Sandbox Code Playgroud)

但如果你使用以下链接,它适用于两者

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
    </a>
</string>
Run Code Online (Sandbox Code Playgroud)

  • 使用`autoLink =“ all”`。 (2认同)

Shy*_*dda 6

正如@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" />
Run Code Online (Sandbox Code Playgroud)

在strings.xml中

<string name="terms_and_conditions">I read and accept the <a href="http://www.spiffyshow.com/">Terms and Conditions</a></string>
Run Code Online (Sandbox Code Playgroud)

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

termsCB.movementMethod = LinkMovementMethod.getInstance()
Run Code Online (Sandbox Code Playgroud)


Rus*_*lan 6

对我来说,当我执行以下操作时,超链接总是效果很好:

  1. 将这两添加到活动/片段中:
textView.setMovementMethod(LinkMovementMethod.getInstance())
textView.setText(Html.fromHtml(getString(R.string.link)))
Run Code Online (Sandbox Code Playgroud)
  1. 不要向 xml 添加任何内容(例如 autoLink 等)。
  2. 在strings.xml中定义链接,例如

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