【问题标题】:Make a hyperlink textview in android在android中制作超链接textview
【发布时间】:2012-03-06 15:12:08
【问题描述】:

我想为 Google 之类的 textview 文本创建一个链接。反正有没有这样的链接。 (即)点击 Google 一词时,应打开相应的链接。欢迎任何想法。

【问题讨论】:

标签: android url hyperlink textview


【解决方案1】:

试试这个,让我知道会发生什么..

使用java代码:

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

从 API 级别 >= 24 开始,Html.fromHtml(String source) 已弃用,而使用 fromHtml(String, int)

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

或者在布局 xml 文件中,在 TextView 小部件属性中

android:autoLink="web"
android:linksClickable="true"

【讨论】:

  • 我得到了预期的输出。谢谢你的回答
  • 如果超链接文本存在于 strings.xml 中,则建议的答案均无效。它以非常奇怪的方式工作。如果我直接在布局 xml 中设置文本它可以工作,但是当我在 java 代码中设置 textview 数据时它不起作用。
  • 顺便提一下,如果您使用 Java 代码,您需要从 XML 中删除 android:autoLink="web"。我两者都有,但没有用。
  • @Velu 您必须用 &lt;![CDATA[string]&gt;&gt; 包围字符串,其中字符串是带有 ulr 的整个字符串文本。试图解决这个问题一个小时......
【解决方案2】:

在 TextView 的 xml 中使用 android:autoLink="web"。它应该自动转换可点击的网址(如果在文本中找到)

【讨论】:

  • 还包括 android:linksClickable="true"
  • @waqaslam 在这种情况下我们放置了一个链接。
  • @Garg's 这是你在textview上设置的可能包含超链接的文本
【解决方案3】:

所有测试和工作 100%
解决方案:android:autoLink="web"
下面是一个完整的示例

示例布局 Xml

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="email"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/lostpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtLostpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:autoLink="web"
        android:gravity="center"
        android:padding="20px"
        android:text="@string/defaultpassword"
        android:textAppearance="?android:attr/textAppearanceSmall" />

string.xml 中的字符串

<string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>

<string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>

【讨论】:

【解决方案4】:

这也可以通过使用Textview的默认属性来完成

android:autoLink="email"

【讨论】:

    【解决方案5】:

    注意:- Html.fromHtml 在 Android N 中已弃用

    您需要检查并支持Android N及更高版本的Android

                      //Set clickable true
                     tagHeading.setClickable(true);
                     
                      //Handlle click event
                      tagHeading.setMovementMethod(LinkMovementMethod.getInstance());
    
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                        tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>", Html.FROM_HTML_MODE_LEGACY));
                    } else {
                        tagHeading.setText(Html.fromHtml("<a href='https://github.com/hiteshsahu'>https://github.com/hiteshsahu</a>"));
                    }
     
    

    或者

    您可能不想以编程方式在 TextView 上添加 autoLink 标志。

    android:autoLink="web"

    android:linksClickable="true"

    这样就不用加&lt;a href='somelink'&gt;标签了。

    这是一个缺点,如果你想在 text 上添加 hyperlink,你不能这样做。例如,您不能这样做:- [hiteshsahu][1]

               <TextView
                    android:id="@+id/tag_info"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/tag_ll"
                    android:layout_gravity="left"
                    android:layout_margin="10dp"
                    android:autoLink="web"
                    android:linksClickable="true"
                    android:text="https://github.com/hiteshsahu"
                    android:textColor="@color/secondary_text" />
    

    两种方法的结果:-

    https://github.com/hiteshsahu

    【讨论】:

      【解决方案6】:

      对于最新版本的 SDK fromHtml 已弃用 使用下面的行

      String yourtext = "<a style='text-decoration:underline' href='http://www.sample.com'> Sample Website </a>";
          if (Build.VERSION.SDK_INT >= 24) {
              textView.setText(Html.fromHtml(yourtext, Html.FROM_HTML_MODE_LEGACY));
          } else {
              textView.setText(Html.fromHtml(yourtext));
          }
      

      【讨论】:

        【解决方案7】:

        我做了一个textview的以下扩展功能。

        @SuppressLint("SetTextI18n")
        fun TextView.makeHyperLink(url: String) {
            text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT)
            } else {
                Html.fromHtml("<a href='${url}'>${text}</a>")
            }
            movementMethod = LinkMovementMethod.getInstance()
            isClickable = true
        }
        

        这样称呼它

        tvTos.makeHyperLink(tos_url)
        

        您也可以将文本作为参数传递。

        【讨论】:

          猜你喜欢
          • 2015-08-30
          • 2016-12-13
          • 2016-04-07
          • 2020-09-13
          • 2012-07-09
          • 2011-06-29
          • 2012-03-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多