EditText TextView中显示表情

android自带的短信中就实现了这个功能,用的方法就是用图片代替字符转,在源码的mms包中就有相应的源码,我们只需要拿过来稍加修改就可以用。下面上码:

1、首先是XML

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="default_smiley_name">
   <item>/heart</item>
<item>/rose</item>
<item>/surprised</item>
<item>/pig</item>
<item>/clap</item>
<item>/ye</item>
<item>/pitiful</item>
<item>/lucky</item>
<item>/wink</item>
<item>/angry</item>
<item>/dizzy</item>
<item>/parent</item>
<item>/serious</item>
<item>/doze</item>
<item>/sweat</item>
<item>/happy</item>
<item>/cry</item>
<item>/doubt</item>
<item>/color</item>
<item>/moon</item>
</array>
</resources>

2、Smileys这个类,用于存储图片和字符串数组并进行关联

public class Smileys
{
private static final int[] sIconIds =
{ R.drawable.b001, R.drawable.b002,
R.drawable.b003, R.drawable.b004, R.drawable.b005, R.drawable.b006,
R.drawable.b007, R.drawable.b008, R.drawable.b009, R.drawable.b010,
R.drawable.b011, R.drawable.b012, R.drawable.b013, R.drawable.b014,
R.drawable.b015, R.drawable.b016, R.drawable.b017, R.drawable.b018,
R.drawable.b019, R.drawable.b020 };


{
};
public static int HEART = 0;
public static int ROSE = 1;
public static int SURPRISED = 2;
public static int PIG = 3;
public static int CLAP = 4;
public static int YE = 5;
public static int PITIFUL = 6;
public static int LUCKY = 7;
public static int WINK = 8;
public static int ANGRY = 9;
public static int DIZZY = 10;
public static int PARENT = 11;
public static int SERIOUS = 12;
public static int DOZE = 13;
public static int SWEAT = 14;
public static int HAPPY = 15;
public static int CRY = 16;
public static int DOUBT = 17;
public static int COLOR = 18;
public static int MOON = 19;


public static int getSmileyResource(int which)
{
return sIconIds[which];
}
}

3、就是最重要的部分,将字符串解析为图片

public class SmileyParser
{
private static SmileyParser instance;


public static SmileyParser getInstance()
{
return instance;
}


public static void init(Context context)
{
instance = new SmileyParser(context);
}


private final Context context;
private final String[] smileyTexts;
private final Pattern pattern;
private final HashMap<String, Integer> smileyMap;


private SmileyParser(Context context)
{
this.context = context;
this.smileyTexts = context.getResources().getStringArray(
R.array.default_smiley_name);
this.smileyMap = buildSmileyToRes();
this.pattern = buildPattern();
}


public static final int[] DEFAULT_SMILEY_RES_IDS =
{
Smileys.getSmileyResource(Smileys.HEART), // 0
Smileys.getSmileyResource(Smileys.ROSE), // 1
Smileys.getSmileyResource(Smileys.SURPRISED), // 2
Smileys.getSmileyResource(Smileys.PIG), // 3
Smileys.getSmileyResource(Smileys.CLAP), // 4
Smileys.getSmileyResource(Smileys.YE), // 5
Smileys.getSmileyResource(Smileys.PITIFUL), // 6
Smileys.getSmileyResource(Smileys.LUCKY), // 7
Smileys.getSmileyResource(Smileys.WINK), // 8
Smileys.getSmileyResource(Smileys.ANGRY), // 9
Smileys.getSmileyResource(Smileys.DIZZY), // 10
Smileys.getSmileyResource(Smileys.PARENT), // 11
Smileys.getSmileyResource(Smileys.SERIOUS), // 12
Smileys.getSmileyResource(Smileys.DOZE), // 13
Smileys.getSmileyResource(Smileys.SWEAT), // 14
Smileys.getSmileyResource(Smileys.HAPPY), // 15
Smileys.getSmileyResource(Smileys.CRY), // 16
Smileys.getSmileyResource(Smileys.DOUBT), // 17
Smileys.getSmileyResource(Smileys.COLOR), // 18
Smileys.getSmileyResource(Smileys.MOON), // 19
};


private HashMap<String, Integer> buildSmileyToRes()
{
if (DEFAULT_SMILEY_RES_IDS.length != smileyTexts.length)
{
throw new IllegalStateException("Smiley resource ID/text mismatch");
}


HashMap<String, Integer> smileyToRes = new HashMap<String, Integer>(
smileyTexts.length);
for (int i = 0; i < smileyTexts.length; i++)
{
smileyToRes.put(smileyTexts[i], DEFAULT_SMILEY_RES_IDS[i]);
}


return smileyToRes;
}


private Pattern buildPattern()
{
StringBuilder patternString = new StringBuilder(smileyTexts.length * 3);
patternString.append('(');
for (String s : smileyTexts)
{
patternString.append(Pattern.quote(s));
patternString.append('|');
}
patternString.replace(patternString.length() - 1,
patternString.length(), ")");
return Pattern.compile(patternString.toString());
}


public CharSequence addSmileySpans(CharSequence text)
{
SpannableStringBuilder builder = new SpannableStringBuilder(text);


Matcher matcher = pattern.matcher(text);
while (matcher.find())
{
int resId = smileyMap.get(matcher.group());
builder.setSpan(new ImageSpan(context, resId), matcher.start(),
matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return builder;
}
}


4、就是使用了

SmileyParser.init(this);

SmileyParser parser = SmileyParser.getInstance();
String str = "/heart";
edit.setText(parser.addSmileySpans(str));
text.setText(parser.addSmileySpans(str));

这样,我们想要达到的效果就出来了。嘿嘿,本人有点小懒,没有注释,大家细细看看,不难的。

如想交流可加android交流群:103531414

你可能感兴趣的:(EditText TextView中显示表情)