如何在Android应用程序上放置超链接到网站?

我想在我正在开发的 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”等文本替换链接,文本是否与网站网址链接

2)如何放置电子邮件地址(同样的问题,如何用“Click Here to Email”等文本替换它,文本应与[email protected]链接)

我也试过这个教程: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='https://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:[email protected]\">Click Me!</a>";
    email.setText(Html.fromHtml(emailText));
    email.setMovementMethod(LinkMovementMethod.getInstance());
}

}

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...