【问题标题】:Android-Is it possible to add a clickable link into a string resourceAndroid-是否可以在字符串资源中添加可点击的链接
【发布时间】:2012-03-01 12:52:33
【问题描述】:

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

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

【问题讨论】:

    标签: android xml hyperlink android-alertdialog


    【解决方案1】:

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

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

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

    还有TextViewandroid:autoLink 属性也应该可以工作。

    【讨论】:

    • 太棒了 :) 我会尝试设置它并在我让它工作时回复:)
    • 我发现字符串 after 链接中的所有内容都没有显示(所以<string name="s">This shows and <a href="http://foo.bar/">so does this</a> but this doesn\'t</string>)。你是怎么做到的?
    • 属性应该是“href”,而不是“ref”吗?
    • 你能发布你的实现的输出吗?它在 GUI 上看起来如何?
    • android:autoLink="web" 适用于 v26 Oreo 上的非 html 链接;但不适用于 标记的链接.. setMovementMethod 确实有效..
    【解决方案2】:

    我发现了一些有趣的东西。让我知道你们中是否有人观察到这一点。

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

    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
        </a>
    </string>
    

    【讨论】:

    • 使用autoLink="all"
    • 在 之后关闭 ??
    • android:linksClickable="true"
    【解决方案3】:

    正如@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】:

      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。
      • 使用 WebView 做这样一件小事真是个坏主意。
      • Eduard B.,您能详细说明为什么会这样吗?
      【解决方案5】:

      对我来说,超链接总是很有效,当我:

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

      &lt;string name="link"&gt;&lt;![CDATA[&lt;a href="https://google.com"&gt;Google link&lt;/a&gt;]]&gt;&lt;/string&gt;

      【讨论】:

        猜你喜欢
        • 2016-11-13
        • 2011-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-13
        • 2011-10-04
        • 2020-10-01
        • 2021-11-28
        • 1970-01-01
        相关资源
        最近更新 更多