在EditText中添加QQ表情
本文参考自: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表情的更多相关文章
- 微信公众平台中添加qq在线聊天代码
微信公众平台是个不错的媒体,可以和你的小伙伴们即时交流,但你的小伙伴们是用手机上的微信,打字自然就慢了:有人说用微信网页版,那个也不习惯,再说也不一定所有人都知道网页版微信.(2014.01.22更新 ...
- WordPress文章中插入qq表情
看见一些博客中使用了QQ表情,这个效果还是很不错的,可以让文章看起来更爽,那么这个是怎么实现的呢? 下面我就来说说方法. 工具:QQ表情包,下载地址:http://yunpan.cn/cLw6UhwB ...
- 如何在网页中添加“QQ交流”
今天在撸码时,想到这个问题,有些网页中会有诸如,那么如何在网页添加"QQ交谈"? 第一步.登录QQ: 第二步.打开网页:QQ推广,启用QQ通讯组件: 第三步.选择组件样式,设置提示 ...
- 微信公众号自定义菜单中添加emoji表情
做微信公众号开发,可能会遇到如何加入emoji表情的问题.今天在“海南旅游小管家”公众号的菜单中加入了emoji表情,特此记录备忘. 1.登录微信公众号,在左侧找到[开发者工具]菜单,点击进入,找到[ ...
- TextView,EditText中添加不同颜色的文字
在很多时候,在我们项目里需要用到在一个TextView中要显示不同颜色的文字 private Spanned colorText(String text) { return Html.fromHtml ...
- 页面中添加qq客服
html页面 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- 网页中添加QQ在线客服
方法一:调用本地已安装的QQ进行会话 <a href='tencent://message/?uin=QQ号码&Site=网站地址&Menu=yes'></a> ...
- TextView和EditText中添加图片(ImageSpan)
编辑框中加图片,以前一直以为很复杂,后来发现android有些类已经很好的实现了这些功能. 代码如下: [java] view plaincopy mSubjectDetailView = (Text ...
- Hexo中添加emoji表情
国庆的三天假前,都是玩CF和LOL的无限乱斗过来的,输了怨没随机到好的英雄,赢了就高高兴兴的
随机推荐
- .NetCore 利用Jenkins在 Windows平台下打包发布Angular项目
准备环境 安装Jenkins 首先装node,版本根据实际环境而定(node安装包中包含了npm) 安装一般都配置好了环境变量,检查下如果没有就配置下 Jenkins中安装NPM插件 GIt获取代码 ...
- WinForm1
一.窗体的各种属性 二.控件 1.公共控件 2.容器控件 3.菜单控件
- ADO.Net1
一.ADO.Net 数据库连接技术 二.查询 1.步骤: 1)使用数据库空间:using System.Data.SqlClient; 2)连接数据库 3)创建数据库操作命令 4)输入操作命令 5)开 ...
- 【Eclipse】Eclipse常用操作
[文章索引] (1)POJO类自动生成Getter和Setter方法 (2)自动生成构造方法(构造函数) (3)通过类自动生成它的接口 (4)自动生成Junit测试类 (5)定制视图 (6) ...
- kaldi 三个脚本cmd.sh path.sh run.sh
参考 kaldi 的全部资料_v0.4 cmd.sh 脚本为: 可以很清楚的看到有 3 个分类分别对应 a,b,c.a 和 b 都是集群上去运行这个样子, c 就是我们需要的.我们在虚拟机上运行的 ...
- Angular 快速学习笔记(1) -- 官方示例要点
创建组件 ng generate component heroes {{ hero.name }} {{}}语法绑定数据 管道pipe 格式化数据 <h2>{{ hero.name | u ...
- VS2010中设置程序以管理员身份运行
VS2010中设置程序以管理员身份运行 直接项目右键---属性---连接器---清单文件---uac执行级别 选择requireAdministrator 重新编译 即可 这样程序直接运行就拥有管理员 ...
- Javascript的字符串(String)操作学习
1.bold() 方法用于把字符串显示为粗体.语法: stringObject.bold() 如下,对str进行bold操作之后,实际上时对这个字符串加了<b>标签,在文档中将以粗体进行展 ...
- Python3 BP神经网络
转自麦子学院 """ network.py ~~~~~~~~~~ A module to implement the stochastic gradient descen ...
- vue打包以及在Apache环境下的配置
vue打包,我们都清楚,实在记不住命令的可以去package.json中看: npm run build 打包后会生成dist文件夹,将dist文件夹下的所有文件复制到Apache下的www的文件夹下 ...