本文参考自: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. 【推荐】关于JS中的constructor与prototype【转】

    最初对js中 object.constructor 的认识: 在学习JS的面向对象过程中,一直对constructor与prototype感到很迷惑,看了一些博客与书籍,觉得自己弄明白了,现在记录如下 ...

  2. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  3. jquery 点击页面流畅弹出预定文字

    js代码: <script src="https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js"></script&g ...

  4. Java集合&Spring源码浅读

    记录自己现在知道的,以后了解了更多的话,再继续补上来 Java集合类 Collection 接口 说明:是List,set 的父类.定义了集合初始模样.集合只存储对象. Jdk8文档,内部方法定义有: ...

  5. ZJOI2019day1退役记

    ZJOI2019day1退役记 每天都在划水,考场上心态炸了,也没什么好说的. 有人催我更退役记,等成绩出来了再更更吧,成绩出来也没心情更了,落差好大,还打不过文化课选手 虽然被卡常数卡到心态爆炸,但 ...

  6. HDU 5908 Abelian Period 暴力

    Abelian Period 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5908 Description Let S be a number st ...

  7. Code Fragment-UI加载策略之-可视者优先加载

    通常情况 通常程序的UI不太复杂,我们会直接加载这些UI信息 复杂的UI 加载的元素就相对多一些. 加载的数据相对多. 因为UI元素和数据元素都比较多,加载的时间相对多. 可视者优先加载 不是默认的加 ...

  8. .NET开源作业调度框架(Quartz.NET和FluentScheduler)实战项目演练

    一.课程介绍 明人不说暗话,跟着阿笨一起玩NET .本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享 ...

  9. C#远程调用技术WebService葵花宝典

    一.课程介绍 直接开门见山吧,在学习之前阿笨想问大家一句,关于WebService远程过程调用技术(RPC) 你真的会了吗?不要跟老夫扯什么WebService技术已经过时,如果你的内心有在偷偷告诉你 ...

  10. 高速排序C++实现

    //高速排序 #include<iostream> #include<functional> #include<Windows.h> using namespace ...