Android TV webview禁止自动获取焦点 遥控器按键监听 dispatchKeyEvent

开发过程中遇到android webview 自动获取焦点 设置 android:focusable="false" 以及父布局设置 android:focusable="true"
android:focusableInTouchMode="true" 均不起作用,webview无输入框等获取焦点控件暂时不知道为什么能够自动获取焦点,且不会相应遥控器DOWN按键。解决办法是 重写dispatchKeyEvent() 拦截遥控器点击事件。

 

 @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub

        switch (keyCode) {

            case KeyEvent.KEYCODE_ENTER:     //确定键enter
            case KeyEvent.KEYCODE_DPAD_CENTER:
                Log.d(TAG, "enter--->");
//                showOption();
                break;

            case KeyEvent.KEYCODE_BACK:    //返回键
                Log.d(TAG, "back--->");
//                showExitDialog();
                return true;   //这里由于break会退出,所以我们自己要处理掉 不返回上一层

            case KeyEvent.KEYCODE_SETTINGS: //设置键
                Log.d(TAG, "setting--->");

                break;

            case KeyEvent.KEYCODE_DPAD_DOWN:   //向下键

                /*    实际开发中有时候会触发两次,所以要判断一下按下时触发 ,松开按键时不触发
                 *    exp:KeyEvent.ACTION_UP
                 */
                Log.d(TAG, "down1--->");
                if (event.getAction() == KeyEvent.ACTION_DOWN) {

                    Log.d(TAG, "down1--->");
                }


                break;

            case KeyEvent.KEYCODE_DPAD_UP:   //向上键
                Log.d(TAG, "up--->");

                break;

            case KeyEvent.KEYCODE_0:   //数字键0
                Log.d(TAG, "0--->");

                break;

            case KeyEvent.KEYCODE_DPAD_LEFT: //向左键

                Log.d(TAG, "left--->");

                break;

            case KeyEvent.KEYCODE_DPAD_RIGHT:  //向右键
                Log.d(TAG, "right--->");
                break;

            case KeyEvent.KEYCODE_INFO:    //info键
                Log.d(TAG, "info--->");

                break;

            case KeyEvent.KEYCODE_PAGE_DOWN:     //向上翻页键
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                Log.d(TAG, "page down1--->");

                break;


            case KeyEvent.KEYCODE_PAGE_UP:     //向下翻页键
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                Log.d(TAG, "page up--->");

                break;

            case KeyEvent.KEYCODE_VOLUME_UP:   //调大声音键
                Log.d(TAG, "voice up--->");

                break;

            case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键
                Log.d(TAG, "voice down1--->");

                break;
            case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音
                Log.d(TAG, "voice mute--->");
                break;

            case KeyEvent.KEYCODE_HOME://房子
                Log.d(TAG, "-----房子-----");
                break;
            case KeyEvent.KEYCODE_MENU://菜单
                Log.d(TAG, "-----菜单-----");
                break;

            default:
                break;
        }

        return super.onKeyUp(keyCode, event);
    }

 

/**
 * 监听遥控器按键事件,交由系统处理
 */
@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);
}

你可能感兴趣的:(Android,AndroidTv,Tv开发填坑记录)