2011-07-14 25 views
5

我想在我正在開發的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)如何將電子郵件地址(同樣的問題,如何與文本類似替換「點擊此處」和文本應與[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 

回答

10

,使用T他默認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:[email protected]\">Click Me!</a>"; 
    email.setText(Html.fromHtml(emailText)); 
    email.setMovementMethod(LinkMovementMethod.getInstance()); 
} 

}

+0

這個怎麼樣教程(我試圖讓它工作,但我收到錯誤信息:[http://coderzheaven.com/2011/05/10/textview-with-link-in-android/](http://coderzheaven.com/ 2011/05/10/textview-with-link-in-android /) - 請看上面,我更新了我的帖子。 – super

+1

我沒有看到你的java代碼,所以我不知道是什麼問題。在我看來,你需要將「」改爲「」。我附上上面的工作示例代碼,請嘗試此代碼。 – kameny

+0

感謝它的工作 – super

相關問題