1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android textview表情 android如何在textview或edittext上添加表情

android textview表情 android如何在textview或edittext上添加表情

时间:2021-10-30 14:07:40

相关推荐

android textview表情 android如何在textview或edittext上添加表情

先上效果图:

首先来写一个表情的GridView

public class EmotionView extends LinearLayout implements OnItemClickListener {

private GridView mGridView;

private static final ArrayList emotionDisplayList = new ArrayList();

public static final LinkedHashMap emotionsKeySrc = new LinkedHashMap();

public static final HashMap emotionsKeyString = new HashMap();

private class GridViewAdapter extends BaseAdapter {

List list;

public GridViewAdapter(List 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 iv = 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, "怒");

emotionsKeySrc.put(R.drawable.face2, "蛋糕");

emotionsKeySrc.put(R.drawable.face3, "蜡烛");

emotionsKeySrc.put(R.drawable.face4, "干杯");

emotionsKeySrc.put(R.drawable.face5, "抓狂");

emotionsKeySrc.put(R.drawable.face6, "衰");

emotionsKeySrc.put(R.drawable.face7, "晕");

emotionsKeySrc.put(R.drawable.face8, "粽子");

emotionsKeySrc.put(R.drawable.face9, "风扇");

emotionsKeySrc.put(R.drawable.face10, "花");

emotionsKeySrc.put(R.drawable.face11, "足球");

emotionsKeySrc.put(R.drawable.face12, "绿丝带");

emotionsKeySrc.put(R.drawable.face13, "哼");

emotionsKeySrc.put(R.drawable.face14, "心");

emotionsKeySrc.put(R.drawable.face15, "冰棍");

emotionsKeySrc.put(R.drawable.face16, "哈哈");

emotionsKeySrc.put(R.drawable.face17, "爱你");

emotionsKeySrc.put(R.drawable.face18, "月亮");

emotionsKeySrc.put(R.drawable.face19, "猪头");

emotionsKeySrc.put(R.drawable.face20, "下雨");

emotionsKeySrc.put(R.drawable.face21, "红牌");

emotionsKeySrc.put(R.drawable.face22, "泪");

emotionsKeySrc.put(R.drawable.face23, "哨子");

emotionsKeySrc.put(R.drawable.face24, "困");

emotionsKeySrc.put(R.drawable.face25, "呵呵");

emotionsKeySrc.put(R.drawable.face26, "阳光");

emotionsKeySrc.put(R.drawable.face27, "汗");

emotionsKeySrc.put(R.drawable.face28, "黄牌");

emotionsKeySrc.put(R.drawable.face29, "嘻嘻");

emotionsKeySrc.put(R.drawable.face30, "伤心");

Iterator iterator = emotionsKeySrc.keySet().iterator();

int i = 0;

while (iterator.hasNext()) {

int temp = iterator.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);

}

@Override

public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) {

int value = emotionDisplayList.get(position);

listener.onclick(value);

}

MyListener listener;

public void setListener(MyListener listener) {

this.listener = listener;

}

}

Layout是:

xmlns:android="/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="@dimen/emotion_view_height"

android:background="#d1d8e3" >

android: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" >

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

android:id="@+id/emotion_view"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

/>

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"

/>

Activity里这样来写:

EmotionView mEmotionView;

@Override

protected 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("a[晕]dfsfdf[阳光]ds");

String str1 = textView.getText().toString();

SpannableString spannableString = new SpannableString(str1);

Pattern pattern = null;

pattern = pile("\\[(\\S+?)\\]"); //这里是过滤出[XX]这种形式的字符串,下面是把这种形式的字符串替换成对应的表情

Matcher matcher = pattern.matcher(str1);

Integer drawableSrc = null;

while (matcher.find()) {

int start = matcher.start();

int end = matcher.end();

drawableSrc = EmotionView.emotionsKeyString.get(matcher.group(1));

if (drawableSrc != null && drawableSrc > 0) {

spannableString.setSpan(new ImageSpan(MainActivity.this, drawableSrc), start, end,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

}

}

textView.setText(spannableString,TextView.BufferType.SPANNABLE);

mEmotionView.setListener(new MyListener() {

@Override

public 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);

}

}

});

原文:/baidu_nod/article/details/38310729

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。