您的位置:首页 > 其它

运用spinner和正则表达式pattern来实现文字中表情文字的替换

2016-09-26 16:38 477 查看
第一步:在string中写入array表情文字

第二部:编写util类实现

用于解析字段中的表情文字,然后用图片替换,运用Hasmap来实现存储,键用来表示表情文字,值用来表示图片id

第三部:在activity类中,抓取表情文字,然后用图片替换

以下为源代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="emotions">
<item>[爱你]</item>
<item>[花心]</item>
<item>[抓狂]</item>
</string-array>
</resources>


activity类:

package app.coolweather.com.weibo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Util.EmotionsParse;

/**
* Created by Administrator on 2016/8/27.
*/
public class AtActivity extends Activity {

EmotionsParse emotionsParse;
private ProgressDialog progressDialog;
private TextView textView;
private static String START="start";
private static String END="end";
private static String PHRASE="phrase";
private String str="#《爱够了没》第二季开播了# 麻辣的话题看够没?[爱你][花心][抓狂]强大的美女观察团看够了没?@陈汉典 回归,@HOLD住姊LINLIN 爆笑加盟,《#爱够了没#》第二季播出了!五位帅哥一一登台亮相,Hold住姐现场征婚,不知道Hold住姐青睐哪一个呢? http://t.cn/ShSRRN ";

private static final String TOPIC="#.+?#";
private static final String NAME="@([\u4e00-\u9fa5A-Za-z0-9_]*)";
private static final String URL="http://.*";
private static final String EMOTION="\\[[\u4E00-\u9FA5a-zA-Z0-9]*\\]";

private SpannableString spannableString = new SpannableString(str);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.at_layout);
showDialg();
progressDialog.dismiss();
textView=(TextView)findViewById(R.id.at_text_view);
hightLight(Pattern.compile(TOPIC));
hightLight(Pattern.compile(NAME));
hightLight(Pattern.compile
4000
(URL));
phrase(Pattern.compile(EMOTION));

//设置spannableString
textView.setText(spannableString);

}

/**
* 对查找到的文字进行图片转换
*/
public void phrase(Pattern pattern){

List<HashMap<String, String>> lst  = this.getStartAndEnd(pattern);  //获取查找到的文字字段
for (HashMap<String, String> map : lst)                 //遍历里面得文字,改变其的属性
{
//         Drawable drawable=this.getResources().getDrawable(R.drawable.crazy);
Drawable drawable=emotionsParse.getDrawableByMyself(map.get( PHRASE));              //这句话为空
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan=new ImageSpan(drawable);
spannableString.setSpan(imageSpan, Integer.parseInt(map.get(START)), Integer.parseInt(map.get(END)), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//具体百度
}

}

/**
* 对查找到的文字进行调高调亮
* @param pattern
*/
public void hightLight(Pattern pattern)
{

List<HashMap<String, String>> lst  = this.getStartAndEnd(pattern);  //获取查找到的文字字段

for (HashMap<String, String> map : lst)                 //遍历里面得文字,改变其的属性
{
//spinnableString就是修改文本的属性
//   SpannableString spannableString = new SpannableString(str);
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);        //设置颜色
spannableString.setSpan(span, Integer.parseInt(map.get(START)), Integer.parseInt(map.get(END)), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);//具体百度
}

}

/**
* 用正则表达式匹配字段是否含有需要的文字,有的话就放入list中
* @param pattern
* @return
*/
public List<HashMap<String, String>> getStartAndEnd(Pattern pattern)
{
List<HashMap<String, String>> lst = new ArrayList<HashMap<String,String>>();
Matcher matcher = pattern.matcher(str);                   //在str中查找pattern的字段,用正则表达式匹配在文本中是否有查找的字段,如果有就讲其存储在list中
while(matcher.find())
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(PHRASE, matcher.group());                    //将抓取到的值保存在group当中
map.put(START, matcher.start()+"");                  // start函数返回匹配到的子字符串的开始位置,没有匹配到的话,应该是-1,小于0
map.put(END, matcher.end()+"");                      //group是用来存放正则表达式的匹配结果的,就是一个变量,其中的内容是匹配结果
lst.add(map);
}
return lst;
}

private void showDialg()
{

if(null == progressDialog)
{
progressDialog = new ProgressDialog(this);
}
progressDialog.setMessage("正在登录...");
progressDialog.show();
}
}


util类:

package Util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import java.util.HashMap;
import app.coolweather.com.weibo.R;

/**用于解析字段中的表情文字,然后用图片替换,运用Hasmap来实现存储,键用来表示表情文字,值用来表示图片id
* Created by Administrator on 2016/9/26.
*/
public class EmotionsParse {
private Context context;
private static  String[] phrases;
private static int[] emotions_id={
R.drawable.love,
R.drawable.hx,
R.drawable.crazy
};
private static HashMap<String,Integer>  hashMap = new HashMap<String,Integer>();

/**
*构造函数将两个数据保存到hasap中
*/
public EmotionsParse(Context context) {
this.context = context;
phrases = context.getResources().getStringArray(R.array.emotions);             //获取strings中的文字
if (phrases.length != emotions_id.length)
throw new RuntimeException("图片文字大小不符合");
int length = emotions_id.length;

for (int i = 0; i < length; i++) {
hashMap.put(phrases[i], emotions_id[i]);
}
}

/**
*根据键从haspmap中读取数据,然后设置drawable
*/
public Drawable getDrawableByMyself(String phrase){

return context.getResources().getDrawable(hashMap.get(phrase));

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: