【问题标题】:set TextView with string which contains a clickable HTML link使用包含可点击 HTML 链接的字符串设置 TextView
【发布时间】:2014-04-15 09:00:34
【问题描述】:

我在布局中有一个TextView

 <TextView
    android:id="@+id/homepage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

我想在这个TextView中设置一个带有HTML链接字符串,并使链接可点击(在浏览器中打开链接时点击)。我尝试了以下方法:

我在资源中定义了字符串,该字符串包含一个指向谷歌网站的 HTML 链接:

<string name="home_page">please go to &lt;a ref="www.google.com">www.google.com!&lt;/a>.

在我的活动中:

TextView homepage =  (TextView)findViewById(R.id.homepage);

String text = getString(R.string.home_page)
CharSequence styledText = Html.fromHtml(text);

homepage.setText(styledText.toString());

结果是 please go to www.google.com ,但 www.google.com 不是可点击的链接。如何让它可点击? (我的意思是点击时在浏览器中打开链接)

【问题讨论】:

标签: android textview


【解决方案1】:

试试这个..

不要忘记在www.之前使用http://,否则你会得到ActivityNotFoundException

TextView homepage =  (TextView)findViewById(R.id.homepage);
homepage.setMovementMethod(LinkMovementMethod.getInstance());
homepage.setText(Html.fromHtml("<font color='#696969'> please go to <a href=\""+"http://www.google.com"+"\">"+"http://www.google.com"+"</a></font>"));

【讨论】:

    【解决方案2】:

    此博客中描述了执行此操作的所有代码.. 检查它 http://android-developers.blogspot.fi/2008/03/linkify-your-text.html

    【讨论】:

      【解决方案3】:

      下面的代码对我有用:-

      <TextView
          android:id="@+id/txt_post_message"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:autoLink="web"
          android:textSize="16sp" />
      

      Autolink inside a TextView in android

      http://alltechsolution.wordpress.com/2012/06/17/how-do-i-make-links-in-a-textview-clickable/

      http://www.technotalkative.com/android-textview-autolink-attribute/

      【讨论】:

        【解决方案4】:
        <TextView 
                android:id="@+id/links"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:autoLink="web" 
                android:text="www.google.com" />
        

        试试这个对我有用的代码。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-08-21
          • 1970-01-01
          • 2018-03-16
          • 2011-02-13
          • 2012-03-13
          相关资源
          最近更新 更多