本文参考自: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. Mybatis输入映射和输出映射

    本节内容: 输入参数映射 输出映射 resultMap Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 一.环境准备 复制 ...

  2. Java第三阶段学习(十二、HttpServletRequest与HttpServletResponse)

    一.HttpServletRequest 1.概述: 我们在创建Servlet时会覆盖service()方法,或doGet()/doPost(),这些方法都有两个参数,一个为代表请求的request和 ...

  3. Python3 turtle安装和使用教程

    Python3 turtle安装和使用教程   Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x.纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数 ...

  4. django表单字段

    在通过django.forms.Forms创建表单字段时,其中每个字段都有一些自定义的验证逻辑和一些其他钩子 虽然表单字段的Field类主要使用在Form类中,但也可以直接实例化来使用它们,以更好的了 ...

  5. poj-1459-最大流dinic+链式前向星-isap+bfs+stack

    title: poj-1459-最大流dinic+链式前向星-isap+bfs+stack date: 2018-11-22 20:57:54 tags: acm 刷题 categories: ACM ...

  6. Linux环境Tomcat运行报错java.lang.OutOfMemoryError

    java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "http-bio-8080-ex ...

  7. 使用ApiPost测试接口时需要先登录怎么办?利用Cookie模拟登陆!

    ApiPost简介: ApiPost是一个支持团队协作,并可直接生成文档的API调试.管理工具.它支持模拟POST.GET.PUT等常见请求,是后台接口开发者或前端.接口测试人员不可多得的工具 . 下 ...

  8. 怎样c# java md5值保持一致

    c#方式 /// <summary> /// 获得字符串md5. /// </summary> /// <param name="myString"& ...

  9. java基础学习总结——super关键字

    一.super关键字

  10. java 实现生产者-消费者模式

    生产和消费者模式有很多种,现在介绍几种常见的方式 wait/notify实现生产和消费者模式 1.使用wait/notify实现生产和消费者模式: public class Depot { // 实际 ...