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

0 0
原创粉丝点击