我想在我正在开发的 Android 应用程序上放置一个超链接。
我试过这个:
main.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/hyperlink"
android:id="@+id/hyperlink"
android:autoLink="web"
>
</TextView>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebLink</string>
<string name="hyperlink">http://google.com</string>
</resources>
但问题是,链接看起来像这样:http://google.com而且我不想显示实际的网址。
1) 如何用“点击此处访问 Google”之类的文本替换链接,并且该文本与网站 url 链接?
2) 如何放置电子邮件地址(同样的问题,如何将其替换为诸如“单击此处发送电子邮件”之类的文本,并且该文本应与 email@domain.com 链接)
我也试过这个教程:http://coderzheaven.com/2011/05/10/textview-with-link-in-android/
但我收到以下错误消息:
Description Resource Path Location Type
http cannot be resolved to a variable MyLink.java /MyLink/src/com/MyLink line 21 Java Problem
Syntax error on token "" <br /> <a href="", ? expected after this token MyLink.java /MyLink/src/com/MyLink line 21 Java Problem
Type mismatch: cannot convert from String to boolean MyLink.java /MyLink/src/com/MyLink line 20 Java Problem
最佳答案
使用默认的Linkify class .
这是一个 Example和教程中的代码:
这是我给你的示例代码,我想这会解决你的问题:
public class StackOverflowActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 1) How to replace link by text like "Click Here to visit Google" and
// the text is linked with the website url ?
TextView link = (TextView) findViewById(R.id.textView1);
String linkText = "Visit the <a href='http://stackoverflow.com'>StackOverflow</a> web page.";
link.setText(Html.fromHtml(linkText));
link.setMovementMethod(LinkMovementMethod.getInstance());
// 2) How to place email address
TextView email = (TextView) findViewById(R.id.textView2);
String emailText = "Send email: <a href=\"mailto:person@stackoverflow.com\">Click Me!</a>";
email.setText(Html.fromHtml(emailText));
email.setMovementMethod(LinkMovementMethod.getInstance());
}
}
关于android - 如何在 Android 应用程序上放置指向网站的超链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6696283/