失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Android键盘 AOSP监听delete按键

Android键盘 AOSP监听delete按键

时间:2019-12-07 09:42:40

相关推荐

Android键盘 AOSP监听delete按键

转载:/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 /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回调:

/detail/jieqiong1/9729520

上边demo里没加deleteSurroundingText方法,添加deleteSurroundingText方法的demo如下:

/detail/jieqiong1/9729907

如果觉得《Android键盘 AOSP监听delete按键》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。