android 键盘监听弹起和关闭

https://blog.csdn.net/Dota_wy/article/details/78781197

电话键

键名 描述 键值 
KEYCODE_CALL 拨号键 5 
KEYCODE_ENDCALL 挂机键 6 
KEYCODE_HOME 按键Home 3 
KEYCODE_MENU 菜单键 82 
KEYCODE_BACK 返回键 4 
KEYCODE_SEARCH 搜索键 84 
KEYCODE_CAMERA 拍照键 27 
KEYCODE_FOCUS 拍照对焦键 80 
KEYCODE_POWER 电源键 26 
KEYCODE_NOTIFICATION 通知键 83 
KEYCODE_MUTE 话筒静音键 91 
KEYCODE_VOLUME_MUTE 扬声器静音键 164 
KEYCODE_VOLUME_UP 音量增加键 24 
KEYCODE_VOLUME_DOWN 音量减小键 25

控制键

键名 描述 键值 
KEYCODE_ENTER 回车键 66 
KEYCODE_ESCAPE ESC键 111 
KEYCODE_DPAD_CENTER 导航键 确定键 23 
KEYCODE_DPAD_UP 导航键 向上 19 
KEYCODE_DPAD_DOWN 导航键 向下 20 
KEYCODE_DPAD_LEFT 导航键 向左 21 
KEYCODE_DPAD_RIGHT 导航键 向右 22 
KEYCODE_MOVE_HOME 光标移动到开始键 122 
KEYCODE_MOVE_END 光标移动到末尾键 123 
KEYCODE_PAGE_UP 向上翻页键 92 
KEYCODE_PAGE_DOWN 向下翻页键 93 
KEYCODE_DEL 退格键 67 
KEYCODE_FORWARD_DEL 删除键 112 
KEYCODE_INSERT 插入键 124 
KEYCODE_TAB Tab键 61 
KEYCODE_NUM_LOCK 小键盘锁 143 
KEYCODE_CAPS_LOCK 大写锁定键 115 
KEYCODE_BREAK Break/Pause键 121 
KEYCODE_SCROLL_LOCK 滚动锁定键 116 
KEYCODE_ZOOM_IN 放大键 168 
KEYCODE_ZOOM_OUT 缩小键 169

 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);
    LinearLayout roots = findViewById(R.id.roots);
    EditText editText = findViewById(R.id.editText);
    softKeyboardStateHelper = new SoftKeyboardStateHelper(this);
    softKeyboardStateHelper.addSoftKeyboardStateListener(this);

}

@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
    Toast.makeText(Main5Activity.this, "键盘打开", Toast.LENGTH_SHORT).show();
}

@Override
public void onSoftKeyboardClosed() {
    Toast.makeText(Main5Activity.this, "键盘关闭", Toast.LENGTH_SHORT).show();
}

@Override
public void onPointerCaptureChanged(boolean hasCapture) {

}

 

 

 

public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
    private View activityRootView;
    private int   lastSoftKeyboardHeightInPx;
    private boolean isSoftKeyboardOpened;
    private int height ;//控制软键盘打开时机

    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed();
    }

    private final List listeners = new LinkedList();


    public SoftKeyboardStateHelper(Activity contextObj) {
        if (contextObj == null) {
            Log.i("dota", "contextObj is null");
            return;
        }
        activityRootView = findContentView(contextObj);
        if (activityRootView!=null){
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
//        this(activityRootView, false);
    }

    public SoftKeyboardStateHelper(Activity contextObj, boolean isSoftKeyboardOpened) {
        if (contextObj == null) {
            Log.i("dota", "contextObj is null");
            return;
        }
        activityRootView = findContentView(contextObj);
        if (activityRootView!=null){
            activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        }
//        this.activityRootView     = activityRootView;
//        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }

    private View findContentView(Activity contextObj) {
        return contextObj.findViewById(android.R.id.content);
    }

    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        activityRootView.getWindowVisibleDisplayFrame(r);

        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (heightDiff == 1920){
            height = 200;
        }else {
            height = 100;
        }
        if (!isSoftKeyboardOpened && heightDiff > height ) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < height ) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed();
        }
    }

    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }

    public boolean isSoftKeyboardOpened() {
        return isSoftKeyboardOpened;
    }

    /**
     * Default value is zero (0)
     * @return last saved keyboard height in px
     */
    public int getLastSoftKeyboardHeightInPx() {
        return lastSoftKeyboardHeightInPx;
    }

    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.add(listener);
    }

    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.remove(listener);
    }

    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardOpened(keyboardHeightInPx);
            }
        }
    }

    private void notifyOnSoftKeyboardClosed() {
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardClosed();
            }
        }
    }
}

 

你可能感兴趣的:(andriod)