[英]Set clickable url link in textview
我的字符串是url字符串和href内的url的组合,例如:“ http://www.google.co.in欲了解更多详细信息,请单击
“否,我不需要同时显示可点击的内容:1- http:www.google.co.in 2-在这里
并点击两个链接,它应该显示相应的网址。
在您的XML文件中使用android:autoLink="web"
。
要么
只需在您的资源(Strings.xml)中使用HTML格式链接:
<string name="my_link"><a ref="http://somesite.com/">Click me!</a></string>
然后,可以在TextView上使用setMovementMethod(LinkMovementMethod.getInstance())以使链接可单击。
你可以试试:
// 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" />
现在,您必须为textView2
和textView4
定义onClickListener
,并且textView4
需要进行定义。 :)
编码愉快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.