Android键盘 AOSP监听delete按键

转载: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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值