Android键盘 AOSP监听delete按键
来源:互联网 发布:北京java工程师待遇 编辑:程序博客网 时间:2024/05/22 03:17
转载:http://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event
用搜狗输入法监听delete按键,EditText设置setOnKeyListener,onKey中,keyCode为KeyEvent.KEYCODE_DEL就是delete按键
editText.setOnKeyListener(new View.OnKeyListener() { @Override public 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))); } @Override public 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); } @Override public 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); } @Override public boolean deleteSurroundingText(int beforeLength, int afterLength) { // magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace if (beforeLength == 1 && afterLength == 0) { Log.e(TAG, "### deleteSurroundingText::"); // backspace return 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.ZanyEditText android: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按键
- android 键盘按键监听
- android 键盘按键监听
- Android 监听软键盘按键与改变软键盘右下角按键样式
- Android 按键的监听
- 监听android home按键
- Android监听HOME按键
- Android监听HOME按键
- Android监听HOME按键
- Android如何监听按键
- Android监听HOME按键
- Android耳机按键监听
- android home按键监听
- android--监听按键
- Android 按键监听
- android按键监听事件
- android监听返回按键
- android 按键、键盘移植
- Android自定义日历控件,自带农历节假日,已经开源,即取即用~
- wampserver You don't have permission to access / on this server. 解决 方法
- 说真的,我烦透了各种“干货”
- SegmentFault 技术周刊 Vol.19 - Linux 文本处理三利器
- pip和conda安装源更改
- Android键盘 AOSP监听delete按键
- 使用UPnP来穿透NAT使内网接口对外网可见
- android白盒测试之junit
- 目标识别
- js(借助jquery库)实现倒计时
- vim tab设置为4个空格
- MethodHandle VS Method, Java反射及MethodHandle性能测试
- 自定义view分类和注意事项
- <a> 标签的 target 属性