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();
}
}
});
} }
<?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>

代码:http://download.csdn.net/detail/shark0017/7623047

感谢:http://blog.csdn.net/wulianghuan/article/details/8583921

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

  1. android自定义控件---添加表情

    android自定义控件---添加表情 一.定义layout文件,图片不提供了 <?xml version="1.0" encoding="utf-8"? ...

  2. Label 添加表情图片

    // 添加表情 NSTextAttachment *attch = [[NSTextAttachment alloc] init]; // 表情图片 attch.image = [UIImage im ...

  3. Android开发——为EditText添加烟花效果的实现

    )什么时候发射烟花:监听EditText的文字改变,获取文字数量的变化以确定风的方向,还有获取光标的位置确定爆炸的位置.光标的位置没有具体的方法确定坐标,要通过反射自己计算. 2.  主要实现类 库里 ...

  4. ASP.NET SignalR 与LayIM配合,轻松实现网站客服聊天室(四) 添加表情、群聊功能

    休息了两天,还是决定把这个尾巴给收了.本篇是最后一篇,也算是草草收尾吧.今天要加上表情功能和群聊.基本上就差不多了,其他功能,读者可以自行扩展或者优化.至于我写的代码方面,自己也没去重构.好的,我们开 ...

  5. 在EditText插入表情,并发送表情

    在EditText插入表情,点击发送按钮,将qq表情显示在TextView中: [mw_shl_code=java,true]public class EditTextActivity extends ...

  6. mysql 数据库 表字段添加表情兼容

    项目中的几个需要支持Emoji表情符号,手机自带的表情,其实添加也很简单: 1 修改数据库 配置my.cnf  init-connect='SET NAMES utf8mb4'             ...

  7. Android 给EditText添加下划线

    在安卓高版本,默认是有下划线的,其默认下划线的颜色是由其主题颜色来控制的! 控制如下: <style name="AppTheme" parent="Theme.A ...

  8. Git-commit-中添加表情

    git commit 中使用表情 我们经常可以在github上看到国外大佬的commit信息中有很多可爱的表情,这是怎么做到的呢? ok,可以这样使用哦:git commit -m '提交信息 :em ...

  9. EditText添加了ImageSpan后,在两者中间不能输入纯文本

    严格来说是连续插入两个ImageSpan之后,在其中间不能够输入纯文本内容. 最后发现问题出现在了SpannableString在设置ImageSpan的时候第四个参数flag的问题. spannab ...

随机推荐

  1. zoj 3827(2014牡丹江现场赛 I题 )

    套公式 Sample Input 33 bit25 25 50 //百分数7 nat1 2 4 8 16 32 3710 dit10 10 10 10 10 10 10 10 10 10Sample ...

  2. 【LOJ】#2513. 「BJOI2018」治疗之雨

    题解 具体就是列一个未知数方程\(dp[i]\)表示有\(i\)滴血的时候期望多少轮 \(dp[i] = 1 + \sum_{j = 1}^{i + 1} a_{i,j}dp[j]\) \(dp[n] ...

  3. JDBC事务与事务隔离级别详解

    事务基本概念 一组要么同时执行成功,要么同时执行失败的SQL语句.是数据库操作的一个执行单元. 事务开始于: 连接到数据库上,并执行一条DML语句insert.update或delete 前一个事务结 ...

  4. rabbitMQ的安装(Windows下)

    在公司接触到这一块,信息中间件的使用,在公司没有时间了解的更加深入,只是在简单的使用,这里将深入学习一番. 参考:http://blog.csdn.net/lu1005287365/article/d ...

  5. C#开发Unity游戏教程之游戏对象的行为逻辑方法

    C#开发Unity游戏教程之游戏对象的行为逻辑方法 游戏对象的行为逻辑——方法 方法(method),读者在第1章新建脚本时就见过了,而且在第2章对脚本做整体上的介绍时也介绍过,那么上一章呢,尽管主要 ...

  6. bzoj 4000 矩阵快速幂优化DP

    建立矩阵,跑快速幂 /************************************************************** Problem: 4000 User: idy002 ...

  7. Codeforces 576D Flights for Regular Customers 矩阵快速幂+DP

    题意: 给一个$n$点$m$边的连通图 每个边有一个权值$d$ 当且仅当当前走过的步数$\ge d$时 才可以走这条边 问从节点$1$到节点$n$的最短路 好神的一道题 直接写做法喽 首先我们对边按$ ...

  8. 使用NewLife网络库构建可靠的自动售货机Socket服务端(一)

    最近有个基于tcp socket 协议和设备交互需求,想到了新生命团队的各种组件,所以决定用NewLife网络库作为服务端来完成一系列的信息交互. 第一,首先说一下我们需要实现的功能需求吧 1,首先客 ...

  9. 使用清华源和阿里源替代Ubuntu源

    sudo nano /etc/apt/source.list 替换为如下文本 # 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释 deb https://mirrors. ...

  10. GitLab查询当前版本

    gitlab-rake gitlab:env:info 其实还有很多方法可以参考GitLab的帮助文档:https://docs.gitlab.com/omnibus/README.html 参考: ...