簡體   English   中英

android: <a href>在TextView中</a>設置鏈接

[英]android: Set link with <a href> in TextView

我動態創建TextView並希望將文本設置為可鏈接。 文字值為“ Google ”。 我提到了像這樣的互聯網和博客它以同樣的方式顯示,但我無法產生預期的結果。

我嘗試了不同的方法,但我看到的輸出是僅包含文本的整個文本。 我試過的代碼是:

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
// Make Linkable
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv1.setText(Html.fromHtml(l.getLeftString()));

/*SpannableString s = new SpannableString(l.getLeftString());
Linkify.addLinks(s, Linkify.WEB_URLS);
tv1.setText(s);                 
tv1.setMovementMethod(LinkMovementMethod.getInstance());
*/
dialogLayout.addView(tv1);

在我的輸出中,我看到“ 谷歌 ”,沒有鏈接。 我也嘗試過清潔項目並再次構建它,但沒有成功。 我希望只有“Google”標有藍色下划線(默認情況下),點擊Google后,瀏覽器會打開http://google.com

我的代碼缺少什么來獲得輸出? BTW For REF:我使用64位Win 7,Java,Eclipse,Android API 8-2.2

任何幫助都非常感謝。

我終於使用以下代碼了解它:

TextView tv1 = new TextView(this);
tv1.setLayoutParams(textOutLayoutParams);
tv1.setText(Html.fromHtml("<a href=\""+ l.getRightString() + "\">" + l.getLeftString() + "</a>"));
tv1.setClickable(true);
tv1.setMovementMethod (LinkMovementMethod.getInstance());
dialogLayout.addView(tv1);

l.getRightString() - 包含一個像http:\\ www.google.com l.getLeftString()的網址 - 包含“Go to Google”等網址的文字

結果:在我的對話框中用藍色和帶下划線的文本“轉到谷歌”,點擊它后,瀏覽器打開並顯示相應的頁面。 在返回/退出瀏覽器時,它再次從它離開的狀態進入應用程序。

希望這可以幫助。

將html保存在字符串中

<string name="link">&lt;a href="http://www.google.com">Google&lt;/a></string>

將textview ID設置為

textViewLinkable

在主要活動中使用以下代碼:

((TextView) findViewById(R.id.textViewLinkable)).setMovementMethod(LinkMovementMethod.getInstance());
((TextView) findViewById(R.id.textViewLinkable)).setText(Html.fromHtml(getResources().getString(R.string.link)));

這是我在Android N上測試的簡單實現。

String termsText = "By registering, you are agree to our";
String termsLink = " <a href=https://www.yourdomain.com/terms-conditions.html >Terms of Service</a>";
String privacyLink = " and our <a href=https://www.yourdomain.com/privacy-policy.html >Privacy Policy</a>";
String allText = termsText + termsLink + privacyLink;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
    ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText, Html.FROM_HTML_MODE_LEGACY));
} 
else {
    ((TextView) findViewById(R.id.text_terms_conditions)).setMovementMethod(LinkMovementMethod.getInstance());
    ((TextView) findViewById(R.id.text_terms_conditions)).setText(Html.fromHtml(allText));
}

我也面臨着我使用以下解決的相同問題

String str_text = "<a href=http://www.google.com >Google</a>";
TextView link;
link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());
link.setText(Html.fromHtml(str_text));

用於將鏈接顏色更改為藍色使用

link.setLinkTextColor(Color.BLUE);
txtview.setMovementMethod(LinkMovementMethod.getInstance());

將此語句傳遞給textview,並在string.xml中將字符串設置為

<string name="txtCredits"> <a href="http://www.google.com"></a></string>

現在將此字符串名稱“android:text =”@ string / txtCredits“傳遞給txtview所在的xml類。

在GitHub上使用此代碼autolink-java

像這樣

private String getLink(String string){

    LinkExtractor linkExtractor = LinkExtractor.builder()
            .linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
            .build();
    Iterable<Span> spans = linkExtractor.extractSpans(string);

    StringBuilder sb = new StringBuilder();
    for (Span span : spans) {
        String text = string.substring(span.getBeginIndex(), span.getEndIndex());
        if (span instanceof LinkSpan) {
            // span is a URL
            sb.append("<a href=\"");
            sb.append(text);
            sb.append("\">");
            sb.append(text);
            sb.append("</a>");
        } else {
            // span is plain text before/after link
            sb.append(text);
        }
    }

    return sb.toString();  // "wow <a href=\"http://test.com\">http://test.com</a> such linked"
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM