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="http://schemas.android.com/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="http://schemas.android.com/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 = Pattern.compile("\\[(\\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);
}
}
});
原文:http://blog.csdn.net/baidu_nod/article/details/38310729
android textview表情,android如何在textview或edittext上添加表情相关推荐
- 苹果计算机怎么添加在快捷方式,如何在 iPhone 主屏幕上添加文件快捷方式?
如何在 iPhone 主屏幕上添加文件快捷方式?文件管理系统一直是 iOS 的弱项,这是由于 iOS 文件操作逻辑与 Windows 完全不同,是iOS「沙盒」机制的产物,那么如何在 iOS 现有框架 ...
- TextView控件上添加表情图片
自己弄了个自定义控件继承自TextView package org.face; import android.content.Context; import android.graphics.draw ...
- php开发添加表情功能,WordPress网站评论区如何实现添加表情包功能?
做网站过程中,可以给自己的网站添加评论框,供用户评论.默认情况下,Wordpress网站评论框是没有添加表情功能的,那么WordPress网站评论区如何实现添加表情包功能?今天我们介绍一下如何给自己的 ...
- php插入word图片,如何在PHPOffice / PHPWord模板上添加/设置图像?
我在PHPWord上有一个模板实例.是否可以替换或添加图像?有点像setImageValue吗? $phpWord = new \PhpOffice\PhpWord\Template('a.docx' ...
- swift 如何在IOS应用图标上添加消息数
在应用图标右上角添加消息数提醒,可以很方便的告知用户该应用中有无新消息需要处理.下面用xcode 7.3.1来简要说明一下如何用swift语言进行此功能的实现. 1.修改 AppDelegate.sw ...
- C#如何在panl控件上添加Form窗体
1. if (treeView1.SelectedNode.Text == "个人信息"){Form1 f4 = new Form1();f4.TopLevel = false;p ...
- iphone指纹识别射频_如何在iPhone或iPad上添加其他Touch ID指纹
iphone指纹识别射频 Fingerprint scanners have been an option on some laptop models for about as long as the ...
- linux7如何删除用户,如何在CentOS 7上添加和删除用户
CentOS以及所有其他Linux发行版都是多用户操作系统.每个用户对于各种命令行和GUI应用程序可以具有不同的权限级别和特定设置. 知道如何添加和删除用户是每个Linux用户应了解的最基本技能之一. ...
- 如何在iPhone,iPad,Mac上添加或删除受信任的电话号码?
如何在iPhone和iPad上添加或删除受信任的电话号码 在继续执行以下过程之前,请确保已 在Apple帐户上启用了双重身份验证.完成后,只需按照以下步骤开始.https://www.macv.com ...
最新文章
- 在Windows 8下成功安装.Net3.5的方法
- git 错误 RPC
- 结构体:计算学生平均分
- TransactionScope 分布式事务
- C语言求最大公约数和最小公倍数的几种算法
- 自适应宽_移动端实现自适应缩放界面的方法汇总
- python多个对象调用类方法、且之间有联系_趣味解读Python面向对象编程 (类和对象)...
- 【Elasticsearch】Elasticsearch 基于scoll技术滚动搜索大量数据
- Alluxio部署(local模式)
- 原生前端:input标签 number类型输入框如何清除上下加减按钮?
- NumPy库---Axis理解
- 前端路由UmiJs快速上手
- uniapp开发App调用微信授权登陆
- 为web-polygraph添加user_agent和add_headers配置变量
- 3款电脑必装软件,功能强大且免费,打死也舍不得卸载
- 也说360和QQ之战
- UPS智能云监控报警器使用手机SIM卡和物联网卡的区别
- 教你在硬件不满足Windows 11最低硬件要求的情况下安装Windows 11(绝对有效)
- 【多元统计分析】08.协方差阵的假设检验
- 哪些语言适用于人工智能 选哪个开发语言更好
热门文章
- 《被讨厌的勇气》书摘心得之一切烦恼都来自人际关系(2)
- 开源分享,让技术发光——最受欢迎“开发者布道师”评选结果来啦!
- ef1a启动子_启动子的选择和预测
- html折叠 手风琴效果,jQuery制作效果超棒的手风琴折叠菜单
- 高三班主任写给学生的一封信(在读大学的要看完)
- 工程机械租赁商如何对世界各地设备进行统一集中管理
- 2021年12月电子学会图形化四级编程题解析含答案:聪明的小猫
- 文章分享-七巧板拼凸多边形-PAUL SCOTT tangrams
- 如何对nginx进行平滑升级
- 基于微信小程序在线电子书阅读系统 电子书小程序毕业设计 毕业论文 开题报告和效果图参考