Android手机输入法按键监听-dispatchKeyEvent

在Android项目中,遇到一个关于手机输入键盘的问题,当使用全屏手写输入法时,酷派手机键盘的上下左右按钮导致界面意外跳转。通过重写Activity的dispatchKeyEvent方法,拦截并处理KEYCODE_DPAD_LEFT、RIGHT、UP、DOWN四个按键,防止界面异常切换。同时,注意到键盘的粘贴复制等操作键与方向键有相同的keyCode,但无内容时无法检测到按键。dispatchKeyEvent在按键事件中起到处理和分发的作用。
摘要由CSDN通过智能技术生成

近期在项目开发中遇到一个关于手机输入键盘的坑。特来记录下。

应用场景
项目中有一个界面是用viewpaper加三个fragment写的,当中viewpaper被我屏蔽了左右滑动,上面有三个点击按钮,点击他们能够切换页面(不要问我为什么这么写,由于你不知道需求有多么的操蛋)。每一个fragment里面都有五六个竖着排列的edittext。仅仅有一个fragment里面的edittext都填写了才让跳到下一个。大致效果图例如以下:

这里写图片描写叙述

遇到的问题
測试的时候发现一个问题(说实话确实非常佩服这个測试妹子,这都能測出来!),当把输入法切换成手写全屏后(记住是全屏手写),酷派手机键盘上会有上下左右四个方向按钮。

其它手机可能仅仅有两个,不同手机可能不一样。当点击四个按钮的时候会发现界面能够上下左右跳转,能从第一个fragment跳到第二个fragment。

。。也就是说推断edittext是否填写的推断无效了。禁止滑动也无效了!!。

解决方法
重写Activity中的dispatchKeyEvent事件然后相应按键进行处理:

@Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        // TODO Auto-generated method stub
        if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
                || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
                || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT
                || event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
            return true;
        }

        return super.dispatchKeyEvent(event);
    }

KEYCODE_DPAD_LEFT (21)、KEYCODE_DPAD_UP (19)、KEYCODE_DPAD_RIGHT (22)、KEYCODE_DPAD_DOWN (20)这四个值相应的就是键盘上的四个按钮(左、上、右、下)的值。这里我的处理方案是直接给屏蔽了他的点击事件,相应的处理逻辑能够自己写。

这里另一点要说下:

这里写图片描写叙述

如上图这里键盘也有自带的上下左右用于粘贴复制等操作,这四个键的keyCode跟上面四个的是一样的。只是有一点不同,就是当editText里面没有写东西的时候会检測不到按键。

再说下dispatchKeyEvent:
当键盘按下时的触发顺序 -> dispatchKeyEvent –> onUserInteraction –> onKeyDown
假设按下紧接着松开。则是触发两次。紧跟再触发–> dispatchKeyEvent –> onUserInteraction
–>onKeyUp
dispatchKeyEvent是做按键处理和分发的工作,假设你想要onKeyDown还能够接收到应该这样实现

public boolean dispatchKeyEvent(KeyEvent event){
return super.dispatchKeyEvent(event);
}

最后给大家附上键盘按键的code值:

 /** Key code constant: Unknown key code. */
    public static final int KEYCODE_UNKNOWN         = 0;
    /** Key code constant: Soft Left key.
     * Usually situated below the display on phones and used as a multi-function
     * feature key for selecting a software defined function shown on the bottom left
     * of the display. */
    public static final int KEYCODE_SOFT_LEFT       = 1;
    /** Key code constant: Soft Right key.
     * Usually situated below the display on phones and used as a multi-function
     * feature key for selecting a software defined function shown on the bottom right
     * of the display. */
    public static final int KEYCODE_SOFT_RIGHT      = 2;
    /** Key code constant: Home key.
     * This key is handled by the framework and is never delivered to applications. */
    public static final int KEYCODE_HOME            = 3;
    /** Key code constant: Back key. */
    public static final int KEYCODE_BACK            = 4;
    /** Key code constant: Call key. */
    public static final int KEYCODE_CALL            = 5;
    /** Key code constant: End Call key. */
    public static final int KEYCODE_ENDCALL         = 6;
    /** Key code constant: '0' key. */
    public static final int KEYCODE_0               = 7;
    /** Key code constant: '1' key. */
    public static final int KEYCODE_1               = 8;
    /** Key code constant: '2' key. */
    public static final int KEYCODE_2               = 9;
    /** Key code constant: '3' key. */
    public static final int KEYCODE_3               = 10;
    /** Key code constant: '4' key. */
    public static final int KEYCODE_4               = 11;
    /** Key code constant: '5' key. */
    public static final int KEYCODE_5               = 12;
    /** Key code constant: '6' key. */
    public static final int KEYCODE_6               = 13;
    /** Key code constant: '7' key. */
    public static final int KEYCODE_7               = 14;
    /** Key code constant: '8' key. */
    public static final int KEYCODE_8               = 15;
    /** Key code constant: '9' key. */
    public static final int KEYCODE_9               = 16;
    /** Key code constant: '*' key. */
    public static final int KEYCODE_STAR            = 17;
    /** Key code constant: '#' key. */
    public static final int KEYCODE_POUND           = 18;
    /** Key code constant: Directional Pad Up key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_UP         = 19;
    /** Key code constant: Directional Pad Down key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_DOWN       = 20;
    /** Key code constant: Directional Pad Left key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_LEFT       = 21;
    /** Key code constant: Directional Pad Right key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_RIGHT      = 22;
    /** Key code constant: Directional Pad Center key.
     * May also be synthesized from trackball motions. */
    public static final int KEYCODE_DPAD_CENTER     = 23;
    /** Key code constant: Volume Up key.
     * Adjusts the speaker volume up. */
    public static final int KEYCODE_VOLUME_UP       = 24;
    /** Key code constant: Volume Down key.
     * Adjusts the speaker volume down. */
    public static final int KEYCODE_VOLUME_DOWN     = 25;
    /** Key code constant: Power key. */
    public static final int KEYCODE_POWER           = 26;
    /** Key code constant: Camera key.
     * Used to launch a camera application or take pictures. */
    public static final int KEYCODE_CAMERA          = 27;
    /** Key code constant: Clear key. */
    public static final int KEYCODE_CLEAR           = 28;
    /** Key code constant: 'A' key. */
    public static final int KEYCODE_A               = 29;
    /** Key code constant: 'B' key. */
    public static final int KEYCODE_B               = 30;
    /** Key code constant: 'C' key. */
    public static final int KEYCODE_C               = 31;
    /** Key code constant: 'D' key. */
    public static final int KEYCODE_D               = 32;
    /** Key code constant: 'E' key. */
    public static final int KEYCODE_E               = 33;
    /** Key code constant: 'F' key. */
    public static final int KEYCODE_F               = 34;
    /** Key code constant: 'G' key. */
    public static final int KEYCODE_G               = 35;
    /** Key code constant: 'H' key. */
    public static final int KEYCODE_H               = 36;
    /** Key code constant: 'I' key. */
    public static final int KEYCODE_I               = 37;
    /** Key code constant: 'J' key. */
    public static final 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值