First on:
First, write a GridView with an expression.
public class Emotionview extends LinearLayout implements Onitemclicklistener {private GridView mgridview;private static F Inal arraylist<integer> emotiondisplaylist = new arraylist<integer> ();p ublic static final Linkedhashmap <integer, string> emotionskeysrc = new Linkedhashmap<integer, string> ();p ublic static final hashmap< String, integer> emotionskeystring = new hashmap<string, integer> ();p rivate class Gridviewadapter extends Baseadapter {list<integer> list;public gridviewadapter (list<integer> list) {super (); this.list = List;} public int GetCount () {return list.size ();} Public Object getItem (int position) {return list.get (position);} public long getitemid (int position) {return position;} Public View GetView (int position, View Convertview, viewgroup parent) {int resId = (Integer) getItem (position); ImageView I v = null;if (Convertview = = null) {IV = (ImageView) (Convertview = new ImageView (GetContext (). Getapplicationcontext ()));} else {IV = (IMAGEview) Convertview;} Iv.setimageresource (resId); Iv.setbackgroundresource (r.drawable.bg_face); int height = getresources (). Getdimensionpixelsize (r.dimen.emotion_item_view_height); iv.setpadding (0, height, 0, height); return IV;}} static {Emotionskeysrc.put (R.drawable.face1, "Fury"); Emotionskeysrc.put (R.drawable.face2, "cake"); Emotionskeysrc.put ( R.drawable.face3, "Candle"); Emotionskeysrc.put (R.drawable.face4, "Cheers"); Emotionskeysrc.put (R.drawable.face5, "mad"); Emotionskeysrc.put (R.drawable.face6, "decay"), Emotionskeysrc.put (R.drawable.face7, "Halo"); Emotionskeysrc.put ( R.drawable.face8, "Zongzi"); Emotionskeysrc.put (R.drawable.face9, "fan"); Emotionskeysrc.put (R.drawable.face10, "flower"); Emotionskeysrc.put (R.drawable.face11, "Football"); Emotionskeysrc.put (R.drawable.face12, "green ribbon"); Emotionskeysrc.put ( R.drawable.face13, "Hum"), Emotionskeysrc.put (R.drawable.face14, "heart"); Emotionskeysrc.put (R.drawable.face15, "popsicles"); Emotionskeysrc.put (r.drawable.face16, "haha"); Emotionskeysrc.put (R.drawable.face17, "Love You"); Emotionskeysrc.put ( R.drawable.face18, "Moon"); Emotionskeysrc.put (R.drawable.face19, "pig Head"); Emotionskeysrc.put (R.drawable.face20, "Rain"); Emotionskeysrc.put (R.DRAWABLE.FACE21, "red card"); Emotionskeysrc.put (R.drawable.face22, "Tears"); Emotionskeysrc.put ( R.drawable.face23, "whistle"); Emotionskeysrc.put (R.drawable.face24, "Sleepy"); Emotionskeysrc.put (R.drawable.face25, "hehe"); Emotionskeysrc.put (R.drawable.face26, "Sunshine"); Emotionskeysrc.put (R.drawable.face27, "Khan"); Emotionskeysrc.put ( R.drawable.face28, "yellow card"), Emotionskeysrc.put (r.drawable.face29, "hehe"); Emotionskeysrc.put (R.drawable.face30, "sad") ;iterator<integer> Iterator = Emotionskeysrc.keyset (). Iterator (); int i = 0;while (Iterator.hasnext ()) {int temp = i Terator.next (); Emotionskeystring.put (Emotionskeysrc.get (temp), temp); Emotiondisplaylist.add (temp); i++;}} public interface Emotionadapter {void doAction (int resId, String desc);} Public Emotionview (Context context) {super (context); Initviews ();} Public Emotionview (context context, AttributeSet Attrs) {Super (context, attrs); Initviews ();} private void Initviews () {Context context = GetContext (); Layoutinflater.from (context). Inflate (R.layout.emotion_main, this); Mgridview = (GridView) Findviewbyid ( R.id.gridview); Mgridview.setadapter (new Gridviewadapter (emotiondisplaylist)); Mgridview.setonitemclicklistener ( this);} @Overridepublic void Onitemclick (adapterview<?> arg0, View arg1, int position, long arg3) {int value = Emotiondispla Ylist.get (position); Listener.onclick (value);} MyListener listener;public void Setlistener (MyListener listener) {This.listener = Listener;}}
Layout is:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayoutxmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Wrap_content "android:layout_height=" @dimen/emotion_view_height "Android: Background= "#d1d8e3" ><gridviewandroid:id= "@+id/gridview" android:layout_width= "Fill_parent" android:layout _height= "Fill_parent" android:numcolumns= "@integer/emotion_colnum" android:cachecolorhint= "@null" Android: Stretchmode= "ColumnWidth" android:listselector= "#00000000" android:fadingedgelength= "0DP" ></GridView> </RelativeLayout>
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "> <com.example.weibotest.view.emotionviewandroid:id=" @+id/emotion_view "Android:layout_ Width= "Fill_parent" android:layout_height= "Wrap_content" android:layout_centerinparent= "true"/> < EditText android:id= "@+id/textview" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:layout_marginleft= "10DP" android:layout_marginright= "10DP" android:textsize= "18sp" android:textcolor= "# 000000 "android:layout_marginbottom=" 10DP "android:layout_above=" @id/emotion_view " /></relativelayout >
In this activity, write:
Emotionview Memotionview; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); final TextView TextView = (TextView) Findviewbyid (r.id.textview); Memotionview = (Emotionview) Findviewbyid (R.id.emotion_view); Textview.settext ("<strong>a[Halo]dfsfdf[Sunshine]ds</strong>"); String str1 = Textview.gettext (). toString (); spannablestring spannablestring = new spannablestring (STR1); Pattern pattern = null;<strong> Pattern = Pattern.compile ("\\[(\\s+?) \\]"); Here is the filter out [XX] This form of string, the following is the form of the string replaced by the corresponding expression </strong>matcher Matcher = Pattern.matcher (STR1); DRAWABLESRC = Null;while (Matcher.find ()) {int start = Matcher.start (); int end = Matcher.end ();d rawablesrc = emotionview.e Motionskeystring.get<strong> (Matcher.group (1)) </strong>;if (drawablesrc! = null && DRAWABLESRC > 0) {<strong>spannablestring.setspan (new Imagespan (Mainactivity.this, DRAWABLESRC), start, End,spanned.span_exclusive_exclusive); </strong>}}textview.settext (spannablestring,textview.buffertype.spannable); Memotionview.setlistener (New MyListener () {@Overridepublic void onclick (int value) {String str1 = Textview.gettext (). ToString (); spannablestring str = new spannablestring (STR1), if (value > 0) {str.setspan (new Imagespan (Mainactivity.this, value), 4 , 5,spanned.span_exclusive_exclusive); Textview.settext (str,textview.buffertype.spannable);}});
Code: http://download.csdn.net/detail/baidu_nod/7697419