欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

电视按键事件的处理(4)

程序员文章站 2022-04-12 19:28:22
前三章讲了KeyEvent事件在默认情况下的执行顺序、改变View层和ViewGroup分发逻辑对KeyEvent事件分发的影响,本章讲得是改变Activity对KeyEvent事件的影响先修改activity的dispatchTouchEvent的返回值为true @Override public boolean dispatchKeyEvent(KeyEvent event) { switch (event.getAction()){ ca...

前三章讲了KeyEvent事件在默认情况下的执行顺序、改变View层和ViewGroup分发逻辑对KeyEvent事件分发的影响,本章讲得是改变Activity对KeyEvent事件的影响

先修改activity的dispatchTouchEvent的返回值为true

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getAction()){
            case KeyEvent.ACTION_DOWN:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"dispatchKeyEvent_ACTION_DOWN");
                return true;
            case KeyEvent.ACTION_UP:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"dispatchKeyEvent_ACTION_UP");
                break;
        }
        return super.dispatchKeyEvent(event);
    }

执行结果

com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_DOWN

com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_ViewGroup1: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: onKeyUp_ACTION_UP
com.example.keytest I/wangjiasheng_MainActivity: onKeyUp_ACTION_UP

再将dispatchTouchEvent的返回值改为false

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        switch (event.getAction()){
            case KeyEvent.ACTION_DOWN:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"dispatchKeyEvent_ACTION_DOWN");
                return false;
            case KeyEvent.ACTION_UP:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"dispatchKeyEvent_ACTION_UP");
                break;
        }
        return super.dispatchKeyEvent(event);
    }

执行结果

com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_DOWN

com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_ViewGroup1: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: onKeyUp_ACTION_UP
com.example.keytest I/wangjiasheng_MainActivity: onKeyUp_ACTION_UP

上面是修改dispatchTouchEvent的返回值情况,下面看修改onKeyDown的返回值情况,先将返回值修改为true

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (event.getAction()){
            case KeyEvent.ACTION_DOWN:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyUp_ACTION_DOWN");
               return true;
            case KeyEvent.ACTION_UP:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyUp_ACTION_UP");
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

返回值情况

com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_DOWN
com.example.keytest I/wangjiasheng_ViewGroup1: dispatchKeyEvent_ACTION_DOWN
com.example.keytest I/wangjiasheng_MyView: dispatchKeyEvent_ACTION_DOWN
com.example.keytest I/wangjiasheng_MyView: onKeyDown_ACTION_DOWN
com.example.keytest I/wangjiasheng_MainActivity: onKeyDown_ACTION_DOWN
    
com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_ViewGroup1: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: onKeyUp_ACTION_UP
com.example.keytest I/wangjiasheng_MainActivity: onKeyUp_ACTION_UP

再将返回值修改为false

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (event.getAction()){
            case KeyEvent.ACTION_DOWN:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyUp_ACTION_DOWN");
               return false;
            case KeyEvent.ACTION_UP:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyUp_ACTION_UP");
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

返回值情况

com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_DOWN
com.example.keytest I/wangjiasheng_ViewGroup1: dispatchKeyEvent_ACTION_DOWN
com.example.keytest I/wangjiasheng_MyView: dispatchKeyEvent_ACTION_DOWN
com.example.keytest I/wangjiasheng_MyView: onKeyDown_ACTION_DOWN
com.example.keytest I/wangjiasheng_MainActivity: onKeyDown_ACTION_DOWN
    
com.example.keytest I/wangjiasheng_MainActivity: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_ViewGroup1: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: dispatchKeyEvent_ACTION_UP
com.example.keytest I/wangjiasheng_MyView: onKeyUp_ACTION_UP
com.example.keytest I/wangjiasheng_MainActivity: onKeyUp_ACTION_UP

此章总结

当dispathTouchEvent返回值为true的情况,事件将被丢弃,不会执行activity和所有View层的onKeyDown和onKeyUp,事件不会向上传递也不会向下传递,
当onKeyDown返回值为true和false是对执行结果不影响

本文地址:https://blog.csdn.net/w13635739860/article/details/107560209