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

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

程序员文章站 2022-04-12 19:27:34
首先大致看看安卓按键事件的默认分发机制package com.example.keytest;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.KeyEvent;import android.view.View;import android.view.ViewGroup;public class Ma...

首先大致看看安卓按键事件的默认分发机制

package com.example.keytest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends AppCompatActivity {
    ViewGroup mViewGroup1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /* mViewGroup1=findViewById(R.id.mViewGroup1);
        mViewGroup1.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                return false;
            }
        });*/
    }

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


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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getAction()){
            case KeyEvent.ACTION_DOWN:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyDown_ACTION_DOWN");
                break;
            case KeyEvent.ACTION_UP:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyDown_ACTION_UP");
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
package com.example.keytest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

public class ViewGroup1 extends LinearLayout {

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


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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getAction()){
            case KeyEvent.ACTION_DOWN:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyDown_ACTION_DOWN");
                break;
            case KeyEvent.ACTION_UP:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyDown_ACTION_UP");
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}
package com.example.keytest;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

public class MyView extends androidx.appcompat.widget.AppCompatButton {
    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

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


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

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (event.getAction()){
            case KeyEvent.ACTION_DOWN:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyDown_ACTION_DOWN");
                break;
            case KeyEvent.ACTION_UP:
                Log.i("wangjiasheng_"+this.getClass().getSimpleName(),"onKeyDown_ACTION_UP");
                break;
        }
        return super.onKeyDown(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

由此可见安卓TouchEvent和KeyEvent机制是不同的,TouchEvent事件向下分发的过程中如果事件没有处理,将会调用mView的OnKeyDown,然后再调用Activity的OnKeyDown

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