JSON解析的内容中含有table标签怎么处理以及自定义标签的处理

一、普遍情况

private void handlerHtmlTag(String data) {
		textView.setText(Html.fromHtml(data));
	}

二、自定义标签处理

GameTagHandler

package com.example.htmldemo;

import org.xml.sax.XMLReader;

import android.content.Context;
import android.text.Editable;
import android.text.Html.TagHandler;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class GameTagHandler implements TagHandler {
	private int startIndex = 0;
	private int stopIndex = 0;
	private Context mContext;
	
	public GameTagHandler(Context context) {
		super();
		this.mContext = context;
	}

	@Override
	public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
		if (tag.equalsIgnoreCase("game")) {
			if (opening) {
				startGame(tag, output, xmlReader);
			} else {
				endGame(tag, output, xmlReader);
			}
		}

	}

	public void startGame(String tag, Editable output, XMLReader xmlReader) {
		output.append("");
		startIndex = output.length();
	}

	public void endGame(String tag, Editable output, XMLReader xmlReader) {
		output.append("\n");
		stopIndex = output.length();
		output.setSpan(new GameSpan(), startIndex, stopIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
	}
	
	private class GameSpan extends ClickableSpan implements OnClickListener {

		@Override
		public void onClick(View v) {
			// 跳转某页面
			Toast.makeText(mContext, "点击了game标记之间的内容", Toast.LENGTH_SHORT).show();
		}
	}
}














MainActivity中

private static final String dataTag = "点击<game ><span>这里</span></game>跳转到游戏";
handlerGameTag(dataTag);
private void handlerGameTag(String data) {
		textView.setText(Html.fromHtml(data, null, new GameTagHandler(this)));
		textView.setClickable(true);
		textView.setMovementMethod(LinkMovementMethod.getInstance());//设置点击事件
	}
显示效果

JSON解析的内容中含有table标签怎么处理以及自定义标签的处理_第1张图片

三、table标签处理

TableTagHandler

package com.example.htmldemo;

import org.xml.sax.XMLReader;

import android.content.Context;
import android.text.Editable;
import android.text.Html.TagHandler;
import android.text.Spanned;

public class TableTagHandler implements TagHandler {
	private Context mContext;
	private int startIndex;
	private int endIndex;

	public TableTagHandler() {
		super();
	}

	public TableTagHandler(Context mContext) {
		super();
		this.mContext = mContext;
	}

	@Override
	public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
		if (tag.equalsIgnoreCase("table")) {
			tableTagHanlder(opening, tag, output, xmlReader);
		} else if (tag.equalsIgnoreCase("tbody")) {
			tBodyTagHandler(opening, tag, output, xmlReader);
		} else if (tag.equalsIgnoreCase("tr")) {
			trTagHandler(opening, tag, output, xmlReader);
		} else if (tag.equalsIgnoreCase("td")) {
			tdTagHandler(opening,tag,output,xmlReader);
		}

	}


	private void tableTagHanlder(boolean opening, String tag, Editable output, XMLReader xmlReader) {
		if (opening) {
			startIndex = output.length();
		} else {
			output.append("\n");
			endIndex = output.length();
		}
	}

	private void tBodyTagHandler(boolean opening, String tag, Editable output, XMLReader xmlReader) {
		if (opening) {
			startIndex = output.length();
		} else {
			endIndex = output.length();
		}
	}
	private void trTagHandler(boolean opening, String tag, Editable output, XMLReader xmlReader) {
		if(opening){
			startIndex = output.length();
		}else{
			output.append("\n");
			endIndex = output.length();
		}
		
	}
	private void tdTagHandler(boolean opening, String tag, Editable output, XMLReader xmlReader) {
		if(opening){
			startIndex  = output.length();
		}else{
			output.append("\t");
			endIndex = output.length();
		}
		
	}
}

MainActivity中

private static final String dataTag2 = "<table>" + "<tbody>" + "<tr><td>number</td><td>title</td><td>size</td></tr>" + "<tr><td>21</td><td>22</td><td>23</td></tr>"+ "<tr><td>31</td><td>32</td><td>33</td></tr>" + "</tbody>" + "</table>";

handlerTableTag(dataTag2);

private void handlerTableTag(String data) {
		textView.setText(Html.fromHtml(data, null, new TableTagHandler(this)));

	}
显示效果


上面显示的表格如果表格比较长,或者每个框中的内容长短不一,就无法对齐,复杂的表格更加无法看懂。所以上面的情况只能针对一些简单的表格。



四、完美解决table标签的显示问题

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >

    <include layout="@layout/titlebar_readmore" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp" >

            <TextView
                android:id="@+id/read_more_text_header"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <WebView
                android:id="@+id/read_more_text_webview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                 android:background="@color/white"
                android:visibility="gone" />

            <TextView
                android:id="@+id/read_more_text_footer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:visibility="gone" />
        </LinearLayout>
    </ScrollView>

</LinearLayout>

ReadMoreActivity

package com.sound.chinabuye.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.sound.chinabuye.R;

public class ReadMoreActivity extends Activity implements OnClickListener {
	private ImageView back_to_main;
	private TextView header, footer;
	private WebView webView;
	private String text;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_readmore);
		/*** 管理Activity ****/
		manageActivity();
		// 取出数据
		text = getIntent().getStringExtra("description");
		// 初始化控件
		initView();

	}
	private void initView() {
		back_to_main = (ImageView) this.findViewById(R.id.back_to_main);
		back_to_main.setOnClickListener(this);
		header = (TextView) this.findViewById(R.id.read_more_text_header);
		webView = (WebView) this.findViewById(R.id.read_more_text_webview);
		footer = (TextView) this.findViewById(R.id.read_more_text_footer);
		<span style="color:#ff0000;">if (text.contains("<table")) {
			webView.setVisibility(View.VISIBLE);
			footer.setVisibility(View.VISIBLE);
			String[] arr1 = text.split("<table");
			header.setText(Html.fromHtml(arr1[0]));
			
			String[] arr2 = arr1[1].split("</table>");
			String str2 = "<table" + arr2[0] + "</table>";
			webView.loadDataWithBaseURL(null, str2, "text/html", "utf-8", null);
			footer.setText(Html.fromHtml(arr2[1]));
		} else {
			header.setText(Html.fromHtml(text));
		}</span>
	}

	private void manageActivity() {
		ActivityInstanceManager.getActivityInstanceManager().addActivity(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back_to_main:
			finish();
			overridePendingTransition(R.anim.activity_open, R.anim.activity_close);
			break;
		default:
			break;
		}
	}

}

上面的if...else判断针对是否有table标签分别作了处理,如果没有table标签,直接使用Html工具处理;如果有table标签,将含有table标签的部分抽取出来用webview进行显示,其余的部分用Html进行处理,通过TextView显示。

显示效果如下:

没有表格的情况:

JSON解析的内容中含有table标签怎么处理以及自定义标签的处理_第2张图片

有表格的情况:

JSON解析的内容中含有table标签怎么处理以及自定义标签的处理_第3张图片


在有表格的情况下,不管多复杂的表格都完美展示,即使表格太长也没有问题,可以左右滚动进行查看。




















你可能感兴趣的:(html标签字符串的显示,table标签字符串的显示)