2012-07-10 98 views
6

为什么TextView超链接无法使用。TextView超链接不工作?

在自定义dialog box中使用超链接。

超链接没有出现。

我错了。如何解决它。给我指导。

XML代码

<TextView 
android:id="@+id/google_Link" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_gravity="center" 
android:gravity="center" 
android:padding="10dip" 
android:textSize="20dip" 
android:linksClickable="true" 
android:autoLink="all" 
android:textColorLink="#306EFF" 
android:text="" /> 

Android的代码是

TextView googleLink = (TextView) layout.findViewById(R.id.google_Link); 
googleLink.setClickable(true); 
googleLink.setMovementMethod(LinkMovementMethod.getInstance()); 
googleLink.setText(Html.fromHtml("<a href=`http://www.google.co.in`>Google</a>")); 

Android清单代码是提前

<action android:name="android.intent.action.VIEW" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<category android:name="android.intent.category.BROWSABLE" /> 

谢谢。

+0

反引号不是在HTML用于字符串分隔符。使用Java中的'\“在嵌入引号的字符串中引用引号,同时也抛弃了'setClickable()'和'setMovementMethod()',因为这些应该由你提出的'TextView'内容来处理。 – CommonsWare 2012-07-10 12:37:43

回答

5

只更换这个环节,它的工作:

 TextView textView=(TextView) findViewById(R.id.link); 
     textView.setClickable(true); 
     String linkTxt=getResources().getString(R.string.link); 
     textView.setMovementMethod(LinkMovementMethod.getInstance()); 
     textView.setText(Html.fromHtml(linkTxt)); 

在strings.xml中添加这样的:

<string name="link">&lt;a href=http://www.google.co.in&gt;Google&lt;/a&gt;</string> 
+0

谢谢。它的工作。但如何添加我的自定义标题Google。 – Sekar 2012-07-10 12:57:04

+0

谢谢阿卡什。它正在工作。 – Sekar 2012-07-11 07:13:39

+0

它不能正常工作 – JosephM 2017-06-02 11:11:25

0

它不工作,因为您不能将href设置为TextView

你需要设置具有这种在它的onClick方法的OnClickListener:

String url = "http://www.google.co.in"; 
Intent i = new Intent(Intent.ACTION_VIEW); 
i.setData(Uri.parse(url)); 
startActivity(i); 

之后,你可以监听设置为您TextView这样的:googleLink.setOnClickListener(myListener);

然后再次运行该应用程序并且点击应该被正确处理。

+0

谢谢。这是工作,但超链接不会出现。如何设置超链接。 – Sekar 2012-07-10 12:45:50

+0

您可以使用'TextView'的setText(“http://www.google.co.in”)方法。 – keyboardsurfer 2012-07-10 12:52:27

+0

谢谢。但是当我可以使用setText(“Google”);''TextView'的方法时,超链接不会出现 – Sekar 2012-07-10 13:07:24