android表情Gson EditText TextView


先看效果图



主要代码

package com.example.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import android.app.Activity;
import android.os.Bundle;
import android.text.Spanned;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		TextView tv = (TextView) findViewById(R.id.tv);
		TextView jsonTv=(TextView) findViewById(R.id.jsonTv);
		EditText editText = (EditText) findViewById(R.id.edittext);

		String json = "{\"nickname\":\"xiaoming\",\"age\":10,\"emoj\":\"[:f001}[:f002}[:f003}hello[:f004}[:f005}\"}";
		jsonTv.setText(json);
		
		Gson gson = new GsonBuilder().registerTypeAdapter(Spanned.class, new String2Spanned()).create();

		Person person = gson.fromJson(json, Person.class);

		editText.append(person.getNickname());
		editText.append(" ");
		editText.append(person.getAge() + " ");
		editText.append(person.getEmoj());

		tv.append(Spanned2String.parse(editText.getText()));

	}

}

package com.example.gson;

import android.text.Spanned;

public  class Person {
	private Spanned emoj;
	private String nickname;
	private int age;

	public Spanned getEmoj() {
		return emoj;
	}

	public void setEmoj(Spanned emoj) {
		this.emoj = emoj;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

package com.example.gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.example.gson.application.MyApplication;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;

import android.graphics.drawable.Drawable;
import android.provider.Settings.Global;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.SpannableString;
import android.text.Spanned;

/**
 * 字符串转表情Spanned 表情格式 [:f000}
 * 
 * @author wanjian
 *
 */
public class String2Spanned extends TypeAdapter<Spanned> {

	static Pattern pattern;

	static Map<String, Integer> map = new HashMap<String, Integer>();

	static ImageGetter imageGetter = new ImageGetter() {
		@Override
		public Drawable getDrawable(String source) {
			int id = Integer.parseInt(source);
			Drawable d = MyApplication.application.getResources().getDrawable(id);
			d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
			return d;
		}
	};

	static {

		pattern = Pattern.compile("\\[:f\\d{3}\\}");
		map.put("[:f000}", R.drawable.f000);
		map.put("[:f001}", R.drawable.f001);
		map.put("[:f002}", R.drawable.f002);
		map.put("[:f003}", R.drawable.f003);
		map.put("[:f004}", R.drawable.f004);
		map.put("[:f005}", R.drawable.f005);
		map.put("[:f006}", R.drawable.f006);
		map.put("[:f007}", R.drawable.f007);
		map.put("[:f008}", R.drawable.f008);
		map.put("[:f009}", R.drawable.f009);
		map.put("[:f010}", R.drawable.f010);
		map.put("[:f011}", R.drawable.f011);
		map.put("[:f012}", R.drawable.f012);
		map.put("[:f013}", R.drawable.f013);
		map.put("[:f014}", R.drawable.f014);
		map.put("[:f015}", R.drawable.f015);
		map.put("[:f016}", R.drawable.f016);
		map.put("[:f017}", R.drawable.f017);
		map.put("[:f018}", R.drawable.f018);
		map.put("[:f019}", R.drawable.f019);
		map.put("[:f020}", R.drawable.f020);
		map.put("[:f021}", R.drawable.f021);
		map.put("[:f022}", R.drawable.f022);
		map.put("[:f023}", R.drawable.f023);
		map.put("[:f024}", R.drawable.f024);
		map.put("[:f025}", R.drawable.f025);
		map.put("[:f026}", R.drawable.f026);
		map.put("[:f027}", R.drawable.f027);
		map.put("[:f028}", R.drawable.f028);
		map.put("[:f029}", R.drawable.f029);
		map.put("[:f030}", R.drawable.f030);
		map.put("[:f031}", R.drawable.f031);
		map.put("[:f032}", R.drawable.f032);
		map.put("[:f033}", R.drawable.f033);
		map.put("[:f034}", R.drawable.f034);
		map.put("[:f035}", R.drawable.f035);
		map.put("[:f036}", R.drawable.f036);
		map.put("[:f037}", R.drawable.f037);
		map.put("[:f038}", R.drawable.f038);
		map.put("[:f039}", R.drawable.f039);
		map.put("[:f040}", R.drawable.f040);
		map.put("[:f041}", R.drawable.f041);
		map.put("[:f042}", R.drawable.f042);
		map.put("[:f043}", R.drawable.f043);
		map.put("[:f044}", R.drawable.f044);
		map.put("[:f045}", R.drawable.f045);
		map.put("[:f046}", R.drawable.f046);
		map.put("[:f047}", R.drawable.f047);
		map.put("[:f048}", R.drawable.f048);
		map.put("[:f049}", R.drawable.f049);
		map.put("[:f050}", R.drawable.f050);
		map.put("[:f051}", R.drawable.f051);
		map.put("[:f052}", R.drawable.f052);
		map.put("[:f053}", R.drawable.f053);
		map.put("[:f054}", R.drawable.f054);
		map.put("[:f055}", R.drawable.f055);
		map.put("[:f056}", R.drawable.f056);
		map.put("[:f057}", R.drawable.f057);
		map.put("[:f058}", R.drawable.f058);
		map.put("[:f059}", R.drawable.f059);
		map.put("[:f060}", R.drawable.f060);
		map.put("[:f061}", R.drawable.f061);
		map.put("[:f062}", R.drawable.f062);
		map.put("[:f063}", R.drawable.f063);
		map.put("[:f064}", R.drawable.f064);
		map.put("[:f065}", R.drawable.f065);
		map.put("[:f066}", R.drawable.f066);
		map.put("[:f067}", R.drawable.f067);
		map.put("[:f068}", R.drawable.f068);
		map.put("[:f069}", R.drawable.f069);
		map.put("[:f070}", R.drawable.f070);
		map.put("[:f071}", R.drawable.f071);
		map.put("[:f072}", R.drawable.f072);
		map.put("[:f073}", R.drawable.f073);
		map.put("[:f074}", R.drawable.f074);
		map.put("[:f075}", R.drawable.f075);
		map.put("[:f076}", R.drawable.f076);
		map.put("[:f077}", R.drawable.f077);
		map.put("[:f078}", R.drawable.f078);
		map.put("[:f079}", R.drawable.f079);
		map.put("[:f080}", R.drawable.f080);
		map.put("[:f081}", R.drawable.f081);
		map.put("[:f082}", R.drawable.f082);
		map.put("[:f083}", R.drawable.f083);
		map.put("[:f084}", R.drawable.f084);
		map.put("[:f085}", R.drawable.f085);
		map.put("[:f086}", R.drawable.f086);
		map.put("[:f087}", R.drawable.f087);
		map.put("[:f088}", R.drawable.f088);
		map.put("[:f089}", R.drawable.f089);
		map.put("[:f090}", R.drawable.f090);
		map.put("[:f091}", R.drawable.f091);

	}

	@Override
	public Spanned read(JsonReader in) throws IOException {
		if (in.peek() == JsonToken.NULL) {
			in.nextNull();
			return null;
		}

		String origStr = in.nextString();
		Matcher matcher = pattern.matcher(origStr);

		StringBuilder stringBuilder = new StringBuilder();

		int last = 0;
		while (matcher.find()) {

			int s = matcher.start();
			int e = matcher.end();
			stringBuilder.append(origStr.substring(last, s));

			String group = matcher.group();
			Integer emojId = map.get(group);
			if (emojId == null) {
				stringBuilder.append(group);
			} else {
				stringBuilder.append("<img src='");
				stringBuilder.append(emojId);
				stringBuilder.append("'/>");
			}

			last = e;

		}
		stringBuilder.append(origStr.substring(last, origStr.length()));

		// String ss = "<img src='" + R.drawable.ic_launcher + "'/>";

		return Html.fromHtml(stringBuilder.toString(), imageGetter, null);
	}

	@Override
	public void write(JsonWriter arg0, Spanned arg1) throws IOException {

	}

}


package com.itboye.sunsun.utils;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;


import android.text.Html;
import android.text.Spanned;

/**
 * 表情转 字符串
 * @author wanjian
 *
 */
public class Spanned2String {

	//  <p dir="ltr"><img src="2130837782"><img src="2130837783"></p>
	//   <p dir="ltr">hello</p>
	
	static Pattern pattern;
	static Map<String,String>map=new HashMap<>();
	static{
		pattern=Pattern.compile("<img src=\"\\d+\">");
		
		map.put(String.valueOf(R.drawable.f000)	, "[:f000}");
		map.put(String.valueOf(R.drawable.f001)	, "[:f001}");
		map.put(String.valueOf(R.drawable.f002)	, "[:f002}");
		map.put(String.valueOf(R.drawable.f003)	, "[:f003}");
		map.put(String.valueOf(R.drawable.f004)	, "[:f004}");
		map.put(String.valueOf(R.drawable.f005)	, "[:f005}");
		map.put(String.valueOf(R.drawable.f006)	, "[:f006}");
		map.put(String.valueOf(R.drawable.f007)	, "[:f007}");
		map.put(String.valueOf(R.drawable.f008)	, "[:f008}");
		map.put(String.valueOf(R.drawable.f009)	, "[:f009}");
		map.put(String.valueOf(R.drawable.f010)	, "[:f010}");
		map.put(String.valueOf(R.drawable.f011)	, "[:f011}");
		map.put(String.valueOf(R.drawable.f012)	, "[:f012}");
		map.put(String.valueOf(R.drawable.f013)	, "[:f013}");
		map.put(String.valueOf(R.drawable.f014)	, "[:f014}");
		map.put(String.valueOf(R.drawable.f015)	, "[:f015}");
		map.put(String.valueOf(R.drawable.f016)	, "[:f016}");
		map.put(String.valueOf(R.drawable.f017)	, "[:f017}");
		map.put(String.valueOf(R.drawable.f018)	, "[:f018}");
		map.put(String.valueOf(R.drawable.f019)	, "[:f019}");
		map.put(String.valueOf(R.drawable.f020)	, "[:f020}");
		map.put(String.valueOf(R.drawable.f021)	, "[:f021}");
		map.put(String.valueOf(R.drawable.f022)	, "[:f022}");
		map.put(String.valueOf(R.drawable.f023)	, "[:f023}");
		map.put(String.valueOf(R.drawable.f024)	, "[:f024}");
		map.put(String.valueOf(R.drawable.f025)	, "[:f025}");
		map.put(String.valueOf(R.drawable.f026)	, "[:f026}");
		map.put(String.valueOf(R.drawable.f027)	, "[:f027}");
		map.put(String.valueOf(R.drawable.f028)	, "[:f028}");
		map.put(String.valueOf(R.drawable.f029)	, "[:f029}");
		map.put(String.valueOf(R.drawable.f030)	, "[:f030}");
		map.put(String.valueOf(R.drawable.f031)	, "[:f031}");
		map.put(String.valueOf(R.drawable.f032)	, "[:f032}");
		map.put(String.valueOf(R.drawable.f033)	, "[:f033}");
		map.put(String.valueOf(R.drawable.f034)	, "[:f034}");
		map.put(String.valueOf(R.drawable.f035)	, "[:f035}");
		map.put(String.valueOf(R.drawable.f036)	, "[:f036}");
		map.put(String.valueOf(R.drawable.f037)	, "[:f037}");
		map.put(String.valueOf(R.drawable.f038)	, "[:f038}");
		map.put(String.valueOf(R.drawable.f039)	, "[:f039}");
		map.put(String.valueOf(R.drawable.f040)	, "[:f040}");
		map.put(String.valueOf(R.drawable.f041)	, "[:f041}");
		map.put(String.valueOf(R.drawable.f042)	, "[:f042}");
		map.put(String.valueOf(R.drawable.f043)	, "[:f043}");
		map.put(String.valueOf(R.drawable.f044)	, "[:f044}");
		map.put(String.valueOf(R.drawable.f045)	, "[:f045}");
		map.put(String.valueOf(R.drawable.f046)	, "[:f046}");
		map.put(String.valueOf(R.drawable.f047)	, "[:f047}");
		map.put(String.valueOf(R.drawable.f048)	, "[:f048}");
		map.put(String.valueOf(R.drawable.f049)	, "[:f049}");
		map.put(String.valueOf(R.drawable.f050)	, "[:f050}");
		map.put(String.valueOf(R.drawable.f051)	, "[:f051}");
		map.put(String.valueOf(R.drawable.f052)	, "[:f052}");
		map.put(String.valueOf(R.drawable.f053)	, "[:f053}");
		map.put(String.valueOf(R.drawable.f054)	, "[:f054}");
		map.put(String.valueOf(R.drawable.f055)	, "[:f055}");
		map.put(String.valueOf(R.drawable.f056)	, "[:f056}");
		map.put(String.valueOf(R.drawable.f057)	, "[:f057}");
		map.put(String.valueOf(R.drawable.f058)	, "[:f058}");
		map.put(String.valueOf(R.drawable.f059)	, "[:f059}");
		map.put(String.valueOf(R.drawable.f060)	, "[:f060}");
		map.put(String.valueOf(R.drawable.f061)	, "[:f061}");
		map.put(String.valueOf(R.drawable.f062)	, "[:f062}");
		map.put(String.valueOf(R.drawable.f063)	, "[:f063}");
		map.put(String.valueOf(R.drawable.f064)	, "[:f064}");
		map.put(String.valueOf(R.drawable.f065)	, "[:f065}");
		map.put(String.valueOf(R.drawable.f066)	, "[:f066}");
		map.put(String.valueOf(R.drawable.f067)	, "[:f067}");
		map.put(String.valueOf(R.drawable.f068)	, "[:f068}");
		map.put(String.valueOf(R.drawable.f069)	, "[:f069}");
		map.put(String.valueOf(R.drawable.f070)	, "[:f070}");
		map.put(String.valueOf(R.drawable.f071)	, "[:f071}");
		map.put(String.valueOf(R.drawable.f072)	, "[:f072}");
		map.put(String.valueOf(R.drawable.f073)	, "[:f073}");
		map.put(String.valueOf(R.drawable.f074)	, "[:f074}");
		map.put(String.valueOf(R.drawable.f075)	, "[:f075}");
		map.put(String.valueOf(R.drawable.f076)	, "[:f076}");
		map.put(String.valueOf(R.drawable.f077)	, "[:f077}");
		map.put(String.valueOf(R.drawable.f078)	, "[:f078}");
		map.put(String.valueOf(R.drawable.f079)	, "[:f079}");
		map.put(String.valueOf(R.drawable.f080)	, "[:f080}");
		map.put(String.valueOf(R.drawable.f081)	, "[:f081}");
		map.put(String.valueOf(R.drawable.f082)	, "[:f082}");
		map.put(String.valueOf(R.drawable.f083)	, "[:f083}");
		map.put(String.valueOf(R.drawable.f084)	, "[:f084}");
		map.put(String.valueOf(R.drawable.f085)	, "[:f085}");
		map.put(String.valueOf(R.drawable.f086)	, "[:f086}");
		map.put(String.valueOf(R.drawable.f087)	, "[:f087}");
		map.put(String.valueOf(R.drawable.f088)	, "[:f088}");
		map.put(String.valueOf(R.drawable.f089)	, "[:f089}");
		map.put(String.valueOf(R.drawable.f090)	, "[:f090}");
		map.put(String.valueOf(R.drawable.f091)	, "[:f091}");

		
	}
	
	public static String decode(String str) {
		  String[] tmp = str.split(";&#|&#|;");
		  StringBuilder sb = new StringBuilder("");
		  for (int i = 0; i < tmp.length; i++) {
		    if (tmp[i].matches("\\d{5}")) {
		      sb.append((char) Integer.parseInt(tmp[i]));
		    } else {
		      sb.append(tmp[i]);
		    }
		  }
		  return sb.toString();
		}
	
	public static String parse(Spanned spanned  ) {
		
		try {
			
			String origHtml= Html.toHtml(spanned);
		//模拟器:	<p dir="ltr">慢慢</p>
			//小米2A <p dir=ltr>慢慢</p>
			
			String origStr=origHtml.substring(origHtml.indexOf(">")+1, origHtml.length()-5);
			origStr=StringEscapeUtils.unescapeHtml(origStr);//html 汉字实体编码转换
			Matcher matcher=pattern.matcher(origStr);
			
//		<img src="2130837782">hello<img src="2130837783">
			StringBuilder stringBuilder=new StringBuilder();
			int last=0;
			while (matcher.find()) {
				int s= matcher.start();
				int e=matcher.end();
				
				stringBuilder.append(origStr.substring( last,s));
				
				
				String group=matcher.group();
				String id=group.substring(10, group.length()-2);
				
				String emojStr=map.get(id);
				if (emojStr==null) {
					stringBuilder.append(group);
					
				}else{
					stringBuilder.append(emojStr);
					
				}
				 
				last=e;
				
			}
			stringBuilder.append(origStr.substring(last, origStr.length()));
			return stringBuilder.toString().replace("<br>", "\n");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			return "";
		}
		
	}

}




ps (需要下载org.apache.commons.lang.jar包  )


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值