本文参考自:http://blog.csdn.net/wulianghuan/article/details/8583921

在输入框中输入表情是每个聊天软件的必备功能,做到这点仅需要将表情放入工程图片文件夹中,然后用这段代码来添加图片即可,也就是说将图片作为一个文字添加到输入框中。

                        //获取表情图片文件名
Field field=R.drawable.class.getDeclaredField("f" + randomId);
int resourceId=Integer.parseInt(field.get(null).toString());
//在Android中要显示图片信息,必须使用BitMap的对象来加载
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), resourceId);
//要让图片替代指定的文字就要用ImageSpan
ImageSpan imageSpan = new ImageSpan(MainActivity.this, bitmap);
//f就是图片的前缀名
SpannableString spannableString = new SpannableString("f");
//设置字体
spannableString.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.append(spannableString);

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext"
android:layout_marginTop="10dp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="添加表情"/> <TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用android:digits属性(输入数字)" /> <EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/edittext1"
android:layout_margin="10dp"
android:digits="0123456789"
android:layout_marginTop="10dp"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:text="使用android:digits属性(输入26个小写字母)"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/edittext2"
android:layout_margin="10dp"
android:digits="abcdefghijklmnopqrstuvwxyz"
android:layout_marginTop="10dp"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview3"
android:text="使用android:inputType属性(输入数字)"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/edittext3"
android:layout_margin="10dp"
android:inputType="number|textCapCharacters"
android:layout_marginTop="10dp"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview4"
android:text="使用android:inputType属性(输入Email)"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/edittext4"
android:layout_margin="10dp"
android:inputType="textEmailAddress"
android:layout_marginTop="10dp"/> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview5"
android:text="使用android:inputType属性(输入有符号的浮点数)"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/edittext5"
android:layout_margin="10dp"
android:numeric="decimal|signed"
android:layout_marginTop="10dp"/> </LinearLayout>

MainActivity.java

package com.kale.edittext02;

import java.lang.reflect.Field;
import java.util.Random; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends ActionBarActivity {
private EditText editText;
private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.edittext);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//产生随机数 随机数是从0开始
int randomId= new Random().nextInt(7);//nextInt(7)的范围是0-6
try {
//获取表情图片文件名
Field field=R.drawable.class.getDeclaredField("f" + randomId);
int resourceId=Integer.parseInt(field.get(null).toString());
//在Android中要显示图片信息,必须使用BitMap的对象来加载
Bitmap bitmap=BitmapFactory.decodeResource(getResources(), resourceId);
//要让图片替代指定的文字就要用ImageSpan
ImageSpan imageSpan = new ImageSpan(MainActivity.this, bitmap);
//f就是图片的前缀名
SpannableString spannableString = new SpannableString("f");
//设置字体
spannableString.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.append(spannableString);
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
});
} }

源码下载:http://download.csdn.net/detail/shark0017/7623047

在EditText中添加QQ表情的更多相关文章

  1. 微信公众平台中添加qq在线聊天代码

    微信公众平台是个不错的媒体,可以和你的小伙伴们即时交流,但你的小伙伴们是用手机上的微信,打字自然就慢了:有人说用微信网页版,那个也不习惯,再说也不一定所有人都知道网页版微信.(2014.01.22更新 ...

  2. WordPress文章中插入qq表情

    看见一些博客中使用了QQ表情,这个效果还是很不错的,可以让文章看起来更爽,那么这个是怎么实现的呢? 下面我就来说说方法. 工具:QQ表情包,下载地址:http://yunpan.cn/cLw6UhwB ...

  3. 如何在网页中添加“QQ交流”

    今天在撸码时,想到这个问题,有些网页中会有诸如,那么如何在网页添加"QQ交谈"? 第一步.登录QQ: 第二步.打开网页:QQ推广,启用QQ通讯组件: 第三步.选择组件样式,设置提示 ...

  4. 微信公众号自定义菜单中添加emoji表情

    做微信公众号开发,可能会遇到如何加入emoji表情的问题.今天在“海南旅游小管家”公众号的菜单中加入了emoji表情,特此记录备忘. 1.登录微信公众号,在左侧找到[开发者工具]菜单,点击进入,找到[ ...

  5. TextView,EditText中添加不同颜色的文字

    在很多时候,在我们项目里需要用到在一个TextView中要显示不同颜色的文字 private Spanned colorText(String text) { return Html.fromHtml ...

  6. 页面中添加qq客服

    html页面 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...

  7. 网页中添加QQ在线客服

    方法一:调用本地已安装的QQ进行会话 <a href='tencent://message/?uin=QQ号码&Site=网站地址&Menu=yes'></a> ...

  8. TextView和EditText中添加图片(ImageSpan)

    编辑框中加图片,以前一直以为很复杂,后来发现android有些类已经很好的实现了这些功能. 代码如下: [java] view plaincopy mSubjectDetailView = (Text ...

  9. Hexo中添加emoji表情

    国庆的三天假前,都是玩CF和LOL的无限乱斗过来的,输了怨没随机到好的英雄,赢了就高高兴兴的

随机推荐

  1. Polly公共处理 -重试(Retry)

    封装处理下Polly重试 private ILogger<PollyHelper> _logger; /// <summary> /// /// </summary> ...

  2. zabbix 自动发现

    转自:https://blog.csdn.net/yyy72999/article/details/76065374 zabbix自动发现/zabbix自动发现规则 置顶2017年07月25日 14: ...

  3. 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)

    本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...

  4. [转]c++优先队列(priority_queue)用法详解

    既然是队列那么先要包含头文件#include <queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队 优先队列具有队列的所有特性, ...

  5. html (第四本书第八章参考)

    上机1 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...

  6. 数据转换bug花了半天时间 Java.math.BigDecimal cannot be cast to java.lang.String

    从数据库取出一个 Count函数 统计的值 在代码中要转成Integer类型的时候 Integer.parseInt((String)map.get("ID_")) 报了一下错误: ...

  7. jsp的9大内置对象和4大作用域

  8. Codeforces Round #281 (Div. 2) A. Vasya and Football 模拟

    A. Vasya and Football 题目连接: http://codeforces.com/contest/493/problem/A Description Vasya has starte ...

  9. 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem C. Contest 水题

    Problem C. Contest 题目连接: http://codeforces.com/gym/100714 Description The second round of the annual ...

  10. Mac的brew和brew cask区别以及安装brew cask

    brew多用于命令行. brew cask主要用于有GUI的软件,例如VLC等等. brew cask是brew的一个子集,也就是一个扩展. 安装brew cask扩展: ruby -e " ...