Android TextView中展示含有html標籤的文字

來源:互聯網
上載者:User

標籤:android   textview html   

兩個小執行個體:

1、服務端返回的帶有html標籤的文字,在textview中展示的時候,能正確的換行。

2、需要展示的下載的超連結,以文字代替,隱藏:

實現效果如下:


代碼如下:

package com.android.study;import android.app.Activity;import android.os.Bundle;import android.text.Html;import android.text.method.LinkMovementMethod;import android.widget.TextView;public class FourthActivity extends Activity {private TextView textView;private TextView textUrl;private TextView textUrl2;// html格式換行private static String msgHtml = "1、在任務結束時間之前完成任務並上傳資料。<br />2、任務內容:連續2天達到10000步。<br />3、您可以在下方的進度顯示查看您的任務進度情況。";// 超連結private static String html = "我的用戶端,:<a href='http://www.baidu.com'>點擊下載</a>";private static String html2 = "我的用戶端,:";private static String html22 = "<a href=\""+ "http://www.baidu.com" + "\">"+ "點擊下載" + "</a> ";CharSequence charSequence = Html.fromHtml(html);@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_texttest);init();}private void init() {// textView.setAutoLinkMask(Linkify.ALL);textView = (TextView) findViewById(R.id.textView);textUrl = (TextView) findViewById(R.id.textUrl);textUrl2 = (TextView) findViewById(R.id.textUrl2);/** * html格式換行 */textView.setText(Html.fromHtml(msgHtml));/** * 超連結: 法一 */textUrl.setText(charSequence);textUrl.setMovementMethod(LinkMovementMethod.getInstance());/** * 超連結: 法二 */textUrl2.setText(html2);textUrl2.append(Html.fromHtml(html22));textUrl2.setMovementMethod(LinkMovementMethod.getInstance());}}

超連結形式兩者在該Demo中都可以實現效果。

但法一在我的項目代碼中使用不成功,換了法二後可以了。

有知道該情況的可以留言噢。

Android TextView中展示含有html標籤的文字

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.