【问题标题】:Set clickable url link in textview在 textview 中设置可点击的 url 链接
【发布时间】:2014-05-28 12:50:55
【问题描述】:

我的字符串是 url 字符串和 href 中的 url 的组合,例如: " 前往http://www.google.co.in 了解更多详情请点击

here

"

不,我需要同时显示可点击: 1- http:www.google.co.in 2-这里

点击两个链接,它应该会显示相应的网址。

【问题讨论】:

标签: android textview


【解决方案1】:

在您的 XML 文件中使用 android:autoLink="web"

或者

只需在您的资源 (Strings.xml) 中使用 HTML 格式链接:

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

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

【讨论】:

  • 在我的字符串中,两者都像 string is="Go google.co.in 和有关更多详细信息,请单击 这里" 和我从 html.fromhtml() 方法中使用它。我使用了自动链接,但它从这里删除了链接。
【解决方案2】:

你可以试试:

// text2 has links specified by putting <a> tags in the string
// resource.  By default these links will appear but not
// respond to user input.  To make them active, you need to
// call setMovementMethod() on the TextView object.

TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());

另一种方式:

https://stackoverflow.com/a/17544212/1318946

编辑:

它不是完全正确的解决方案,但您可以使用这个解决您的问题。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Go"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/textView1"
    android:text="Google.com"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#0000ff" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="5dp"
    android:text="For more Details"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/textView3"
    android:layout_alignBottom="@+id/textView3"
    android:layout_marginLeft="5dp"
    android:layout_toRightOf="@+id/textView3"
    android:text="Click here"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#0000ff" />

输出如下:

现在您必须为textView2textView4 定义onClickListener,您可以随心所欲。 :)

快乐编码。

【讨论】:

  • 在我的字符串中,两者都像 string is="Go google.co.in 和更多详细信息,请单击 这里" 和我从 html.fromhtml() 方法中使用它。我使用了自动链接,但它从这里删除了链接。
  • pratik butani 响应来自 web 服务作为字符串,必须使用它进行管理。无法创建两个文本视图。不同的工作。
  • Pratik Butani "转至 google.co.in 了解更多详情,请点击此处"
  • 如果它是最终的,那么就像我说的那样静态地做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-07
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 2011-02-13
相关资源
最近更新 更多