TextView超链接不工作?
为什么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" />
谢谢。
只更换这个环节,它的工作:
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"><a href=http://www.google.co.in>Google</a></string>
它不工作,因为您不能将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);
然后再次运行该应用程序并且点击应该被正确处理。
谢谢。这是工作,但超链接不会出现。如何设置超链接。 – Sekar 2012-07-10 12:45:50
您可以使用'TextView'的setText(“http://www.google.co.in”)方法。 – keyboardsurfer 2012-07-10 12:52:27
谢谢。但是当我可以使用setText(“Google”);''TextView'的方法时,超链接不会出现 – Sekar 2012-07-10 13:07:24
反引号不是在HTML用于字符串分隔符。使用Java中的'\“在嵌入引号的字符串中引用引号,同时也抛弃了'setClickable()'和'setMovementMethod()',因为这些应该由你提出的'TextView'内容来处理。 – CommonsWare 2012-07-10 12:37:43