Android富文本处理
生活随笔
收集整理的這篇文章主要介紹了
Android富文本处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在使用Android WebView的項目中,富文本有時候會用,所以在此分享一下富文本的簡單實現
如何設置類似<html>...</html> <html> <head></head> <body> 哈哈哈 <a href="http://m.baidu.com/u/10001" class="referer">@天天</a> 我贊你了哦 </body> </html>
方法: TextView tv = new TextView(context); tv.setText(Html.fromHtml("<html>...</html>"));
下來看一下富文本的簡單實現Demo: package com.example.textdemo;import android.app.Activity; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String str = "<html><head></head><body>哈哈哈 <a href=\"http://m.baidu.com/u/10001\" class=\"referer\">@天天</a>我贊你了哦</body></html>";// Spanned : 塊 (類似<span></span>和<div></div>),將字符串str解析成一個一個的塊Spanned spanned = Html.fromHtml(str);TextView tv1 = (TextView) findViewById(R.id.tv1);tv1.setText(spanned);String content = getBlueText("蓋聶, 衛莊, 張良, 天明") + "等 <font color=\"#FF0000\">88人</font>覺得很贊";TextView tv2 = (TextView) findViewById(R.id.tv2);tv2.setText(Html.fromHtml(content));}/*** 為字符串加藍色* @param string* @return*/private String getBlueText(String string) {return String.format("<font color=\"#0000FF\">%s</font>", string); // string 會替換 %s}}
運行效果圖如下:
項目源碼,點擊下載
如何設置類似<html>...</html> <html> <head></head> <body> 哈哈哈 <a href="http://m.baidu.com/u/10001" class="referer">@天天</a> 我贊你了哦 </body> </html>
方法: TextView tv = new TextView(context); tv.setText(Html.fromHtml("<html>...</html>"));
下來看一下富文本的簡單實現Demo: package com.example.textdemo;import android.app.Activity; import android.os.Bundle; import android.text.Html; import android.text.Spanned; import android.widget.TextView;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);String str = "<html><head></head><body>哈哈哈 <a href=\"http://m.baidu.com/u/10001\" class=\"referer\">@天天</a>我贊你了哦</body></html>";// Spanned : 塊 (類似<span></span>和<div></div>),將字符串str解析成一個一個的塊Spanned spanned = Html.fromHtml(str);TextView tv1 = (TextView) findViewById(R.id.tv1);tv1.setText(spanned);String content = getBlueText("蓋聶, 衛莊, 張良, 天明") + "等 <font color=\"#FF0000\">88人</font>覺得很贊";TextView tv2 = (TextView) findViewById(R.id.tv2);tv2.setText(Html.fromHtml(content));}/*** 為字符串加藍色* @param string* @return*/private String getBlueText(String string) {return String.format("<font color=\"#0000FF\">%s</font>", string); // string 會替換 %s}}
運行效果圖如下:
項目源碼,點擊下載
轉載于:https://www.cnblogs.com/smartbetter/p/6647521.html
總結
以上是生活随笔為你收集整理的Android富文本处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 边工作边刷题:70天一遍leetcode
- 下一篇: android sina oauth2.