【问题标题】:Android active link of url in TextViewTextView中url的Android活动链接
【发布时间】:2011-10-18 03:36:33
【问题描述】:

我从 Web 服务获取动态文本并在 TextView 中显示相同的内容。有时 TextView 的 URL 类似于 <a href="http://hello.com">hello</a>。我已经使用以下代码设置了文本。

textView.setText(Html.fromHtml(sampletext));

并在包含TextView的相应xml中设置android:autoLink="web"。现在链接以蓝色和下划线正确显示,但我发现它只是一个死链接。如果我们尝试点击它,什么都不会发生。我必须做什么才能使链接处于活动状态?

【问题讨论】:

    标签: android url textview


    【解决方案1】:

    在重温所有解决方案后,一个带有一些解释的总结:

    android:autoLink="web" 
    

    即使没有设置 android:linksClickable 也会找到一个 URL 并创建一个链接,链接默认是可点击的。您不必单独保留 URL,即使在文本中间也可以检测到并可点击。

    <TextView
        android:text="My web site: www.stackoverflow.com"
        android:id="@+id/TextView1"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:autoLink="web">
    </TextView>
    

    通过代码设置链接,原理相同,布局中不需要pattern或者android:autoLink,使用Linkify会自动找到链接:

      final TextView myClickableUrl = (TextView) findViewById(R.id.myClickableUrlTextView);
      myClickableUrl.setText("Click my web site: www.stackoverflow.com");
      Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);
    

    【讨论】:

    • 这里还要说明的是,如果你需要的不仅仅是网页链接,你可以使用all属性,它支持所有类型的链接。
    【解决方案2】:

    这对我有用:

    <TextView
        android:text="www.hello.com"
        android:id="@+id/TextView01"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:autoLink="web">
    </TextView>
    

    【讨论】:

    • 我用过这个......但它是蓝色和带下划线的文字,但我发现它只是一个死链接。没有点击动作发生。那是我的问题。
    • 我知道这是旧的,你需要设置 clickable true 并定义 onclick 功能
    【解决方案3】:

    要保存任何一次真正的解决方案是

        <TextView
        android:text="www.hello.com"
        android:id="@+id/TextView01"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent"
        android:autoLink="web"
        android:linksClickable="true">
    </TextView>
    

    我使用它,它工作得很好

    【讨论】:

      【解决方案4】:

      有两种情况:

      • 文字看起来像"click on http://www.hello.com"

      那么您只需在xml中设置autoLink属性,以便在文本中自动检测到链接:

      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:autoLink="web"
          android:text="click on http://www.hello.com"/>
      
      • 文字看起来像click on &lt;a href="http://hello.com"&gt;hello&lt;/a&gt;

      那你必须通过代码来做,告诉文本是html,并为点击指定一个链接移动方法:

          String text = "click on <a href=\"http://hello.com\">hello</a>";
          TextView textView = view.findViewById(R.id.textView);
          textView.setText(Html.fromHtml(text));
          textView.setMovementMethod(LinkMovementMethod.getInstance());
      

      【讨论】:

        【解决方案5】:

        看看这个方法:

        String text = "Visit stackoverflow.com";
        TextView label = new TextView(this);
        label.setText(text);
        Pattern pattern = Pattern.compile("stackoverflow.com");
        Linkify.addLinks(label, pattern, "http://");
        

        【讨论】:

          【解决方案6】:

          我已经找到了一些想法

          TextView tv = ( TextView ) findViewById( R.id.link );  
              WebView wv = ( WebView ) findViewById( R.id.webView );  
              URLSpan[] urlSpans = tv.getUrls();  
              for ( URLSpan urlSpan : urlSpans )  
              {  
                  wv.loadUrl( urlSpan.getURL() );  
              }  
          

          字符串.xml

          <?xml version="1.0" encoding="utf-8"?>  
          <resources>  
            <string name="app_name">Hello, Android</string>  
            <string name="link">'<a href="http://www.google.com" rel="nofollow">Google</a>'</string>  
          </resources> 
          

          main.xml

          <?xml version="1.0" encoding="utf-8"?>  
          <LinearLayout  
                  xmlns:android="http://schemas.android.com/apk/res/android"  
                  android:orientation="vertical"  
                  android:layout_width="fill_parent"  
                  android:layout_height="fill_parent"  
                  >  
          
            <TextView  
                    android:id="@+id/link"  
                    android:layout_width="wrap_content"  
                    android:layout_height="wrap_content"  
                    android:autoLink="all"  
                    android:linksClickable="true"  
                    android:text="@string/link" />  
          
            <WebView  
                    android:id="@+id/webView"  
                    android:layout_width="fill_parent"  
                    android:layout_height="wrap_content"  
                    android:layout_weight="1.0" />  
          
          </LinearLayout>  
          

          【讨论】:

          • 对于string.xml中的链接字符串标签值,请确保您对以下字符进行转义,否则项目可能无法构建。 & 更改为 & 改为 >
          【解决方案7】:

          在您的 XML 中,您需要在 TextView 中添加 android:linksClickable="true"

          【讨论】:

            【解决方案8】:

            如果您在 textview 中显示来自 strings.xml 的字符串,则包含 Web 链接的字符串不应包含单词“a href="。如果从 strings.xml 文件中删除这些单词,则链接将起作用。

            【讨论】:

              【解决方案9】:

              将这些代码行添加到您的textView 中的xml 文件中,它将完全正常工作..

              android:autoLink="web" android:textColorLink="@android:color/holo_orange_dark" android:linksClickable="true"

              或者如果想要在textview 中添加您自己的链接,请在您的 java 文件中添加这些代码行

                final SpannableString s = new SpannableString("https://play.google.com/store/apps/details?id=cn.wps.moffice_eng");
                Linkify.addLinks(s, Linkify.ALL);
              

              通过函数在你的TextView中设置这个's'字符串

              Textview.setText(s);
              

              别忘了添加这一行

               ((TextView)findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
              

              id 将是您的textview id

              享受...

              【讨论】:

                【解决方案10】:

                Answer 是对的,但不完整,因为我的 xml 中有一些来自其他答案的属性,如 autoLink 和 linksClickable 和编程方式不起作用。此外,当我从字符串资源中传递带有 html 的字符串时,它也不起作用,所以请注意,您必须清理您的 xml 并直接传递字符串,就像在那个答案中一样。

                    String text = "click on <a href=\"http://hello.com\">hello</a>";
                    TextView textView = view.findViewById(R.id.textView);
                    textView.setText(Html.fromHtml(text));
                    textView.setMovementMethod(LinkMovementMethod.getInstance());
                

                如果没有 LinkMovementMethod,我没有尝试过,但现在我可以了,因为它终于可以工作了。其他答案对我不起作用或适用于可见的 url 文本,而不是可点击的文本作为链接。

                【讨论】:

                  猜你喜欢
                  • 2012-10-18
                  • 2011-05-18
                  • 2017-11-07
                  • 1970-01-01
                  • 2020-09-13
                  • 2011-06-29
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-12-09
                  相关资源
                  最近更新 更多