转载:http://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event

用搜狗输入法监听delete按键,EditText设置setOnKeyListener,onKey中,keyCode为KeyEvent.KEYCODE_DEL就是delete按键

 editText.setOnKeyListener(new View.OnKeyListener() {@Overridepublic boolean onKey(View v, int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_DEL && event.getAction() == KeyEvent.ACTION_DOWN) {//do something}}}

这种方式对于 Android键盘失效,在stackoverflow上找到一种可用的方式,
The single method we actually want to override is sendKeyEvent in the EditText’s InputConnection class. This method is called when key events occur in an IME. But in order to override this, we need to implement a custom EditText which overrides the onCreateInputConnection method, wrapping the default InputConnection object in a proxy class!
通过重写EditText的InputConnection 类的sendKeyEvent 方法来解决这个问题。按键时会调用这个方法。
需要自定义EditText,重写onCreateInputConnection,onCreateInputConnection中返回InputConnectionWrapper的子类ZanyInputConnection ,在ZanyInputConnection 的sendKeyEvent方法获取delete按键
代码如下:

package com.example.jieqiong1.backedittext;import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputConnectionWrapper;
import android.widget.EditText;import java.util.Random;/*** Created by jieqiong1 on 2017/1/5.*/public class ZanyEditText extends EditText {private static final String TAG = "ZanyEditText";private Random r = new Random();public ZanyEditText(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public ZanyEditText(Context context, AttributeSet attrs) {super(context, attrs);}public ZanyEditText(Context context) {super(context);}public void setRandomBackgroundColor() {setBackgroundColor(Color.rgb(r.nextInt(256), r.nextInt(256), r.nextInt(256)));}@Overridepublic InputConnection onCreateInputConnection(EditorInfo outAttrs) {return new ZanyInputConnection(super.onCreateInputConnection(outAttrs),true);}private class ZanyInputConnection extends InputConnectionWrapper {public ZanyInputConnection(InputConnection target, boolean mutable) {super(target, mutable);}@Overridepublic boolean sendKeyEvent(KeyEvent event) {if (event.getAction() == KeyEvent.ACTION_DOWN&& event.getKeyCode() == KeyEvent.KEYCODE_DEL) {Log.e(TAG, "### sendKeyEvent:: delete");ZanyEditText.this.setRandomBackgroundColor();// Un-comment if you wish to cancel the backspace:// return false;}return super.sendKeyEvent(event);}@Overridepublic boolean deleteSurroundingText(int beforeLength, int afterLength) {// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspaceif (beforeLength == 1 && afterLength == 0) {Log.e(TAG, "### deleteSurroundingText::");// backspacereturn sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))&& sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));}return super.deleteSurroundingText(beforeLength, afterLength);}}
}

布局文件:

    <com.example.jieqiong1.backedittext.ZanyEditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"/>

demo地址,添加了delete回调:
http://download.csdn.net/detail/jieqiong1/9729520

上边demo里没加deleteSurroundingText方法,添加deleteSurroundingText方法的demo如下:
http://download.csdn.net/detail/jieqiong1/9729907

Android键盘 AOSP监听delete按键相关推荐

  1. android 后台键盘按键监听,android键盘的监听

    android 键盘监听 从知乎上看到的一种解决方案,目前还没发现有什么坑. public class SoftKeyBroadManager implements ViewTreeObserver. ...

  2. android 键盘隐藏监听,安卓监听软键盘弹出与隐藏的两种方法

    需求: 现在有一个需求是点击一行文本框,弹出一个之前隐藏的输入框,输入完成后按返回键或者其他的东西隐藏键盘和输入框,将输入框的内容填充到文本框中. 实现: 拿到这个需求的第一反应就是写一个监听来监听键 ...

  3. android 键盘隐藏监听,Android监听键盘显示和隐藏

    问题概况:横板cocos2dx游戏,点击输入框弹出键盘时,界面要求跟随网上平易,不能挡住输入框.这种问题只出现在非全屏键盘到情况下. 方案1:mainActivity重写onconfiguration ...

  4. android设备外接键盘实现监听事件

    需求分析 产品为一款基于android的交互终端,现在已经在android嵌入式系统上运行了一个第三方的交易软件(没有源码).在产品量产投入使用时,可以实现以下两点需求: 设备开机时自动启动交易App ...

  5. 对EditText的软键盘进行监听-----android:imeOptions

    在android发开过程中,有时候需要对EditText的软键盘进行监听. 当点击软键盘回车位置按键的时候,需要实现 完成.前进.下一项.搜索.发送或其他功能,这就需要开发者对软键盘回车的点击事件进行 ...

  6. android 按键消息,Android监听Home按键消息

    Android对屏幕下方常用的四个按键消息处理是不一致的: 搜索按键的消息在onKeyDown或者onKeyUp中接收: 菜单按键的消息在onCreateOptionsMenu.onKeyDown或o ...

  7. jquery实现上下左右键盘监听_基于 jQuery 实现键盘事件监听控件

    最近项目里要做一个画板,需要对键盘事件进行监听,来进行诸如撤回.重做.移动.缩放等操作,因此顺手实现了一个键盘事件监听控件,期间略有收获,整理出来,希望对大家有所帮助,更希望能获得高手的指点. 1. ...

  8. android注册广播监听按钮,Android实现广播监听HOME键操作

    先写了个Service,在服务中通过广播来监听HOME键操作: public class HomeService extends Service{ private MonitoHomeReceiver ...

  9. [Java][详解]使用jintellitype实现键盘全局监听

    [下载jintellitype] 首先要下载jintellitype-1.3.9,这里提供下载地址 [jintellitype-百度网盘下载] 链接: https://pan.baidu.com/s/ ...

  10. JS:打字游戏_键盘事件监听

    1. 打字游戏 屏幕随机出现字母或数字 用户按下键盘,屏幕显示回答正确与否 当用户按下ESC时游戏结束 <!DOCTYPE html> <html lang="en&quo ...

最新文章

  1. 2021年诺贝尔经济学奖评述:解决重大社会问题的自然实验因果框架
  2. 大数据实训记录(二)
  3. Python教程:切片、迭代、列表生成式
  4. html里table的遍历,js遍历table中的tr
  5. 使用C#实现网络时间同步功能
  6. BZOJ1922 SDOI2010 大陆争霸 最短路
  7. 上位机与1200组态步骤_新入S7-1200系统值得注意几点(博图平台)
  8. SEO培训一家之言 素老胡huxingyu
  9. The Book of Mozilla
  10. basic4android 开发 使用类库方法
  11. spss如何调用已经训练好的神经网络模型
  12. 用word这么多年,90%的人居然不知道这12个小技巧
  13. HDU 1521 排列组合 (指数型母函数)
  14. win 10计算机查找大文件,教你如何在Win10系统中查找大文件?
  15. 云上领跑 智慧贵州 中软国际与云上贵州深度合作助力政府数字化转型
  16. 长沙银河计算机中等职业学校图片,长沙银河中等职业学校
  17. 电子计算机与互联网发展简史
  18. POJ 3265 Problem Solving
  19. java免费批注组件_Java给Word某个字串加批注
  20. 分分钟带你学会DNS、WEB、DHCP服务器的搭建

热门文章

  1. 泛零售数据中台建设之灵魂问答 | 奇点云CEO行在直播回顾
  2. GAMES104实录 | 游戏引擎导论(上)
  3. 计算机广告设计好不好找工作,大专学广告设计好就业吗 前景怎么样
  4. 基于MPlayer的QT媒体播放器
  5. apk文件以及打包流程
  6. amd显卡用黑苹果输出黑屏_黑苹果AMD NVIDIA Intel显卡咋驱动?速戳这里
  7. 供应链金融三种模式介绍及对比
  8. 人人都是产品经理2.0-04章摘要
  9. 百度bae专业版svn提交问题
  10. Filecoin网络为什么需要矿池,ipfs\FIL挖矿比单体矿机有什么优势