1. 程式人生 > >Android鍵盤 AOSP監聽delete按鍵

Android鍵盤 AOSP監聽delete按鍵

用搜狗輸入法監聽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"/>