Solve the problem that the onKeyUp() and onKeyDown() key events cannot be monitored when the Activity pops up

The guardian of the princess is always the knight,
and the princess always chooses the prince.

— Guangzhou · 14℃ · cloudy day · when the sky gets dark, you are the sun~

problem

Monitoring volume + key events, but because the Dialog is displayed, the monitoring fails:

Problem display:

After solving:

Insert picture description here

There are two ways to easily solve the above problems. I will describe how to solve them in detail below.

Solution

In fact, the most fundamental problem is that the Activity loses focus. When I press the volume + key, ACTION_DOWN/ACTION_UPtwo KeyEvent events will be generated, which will eventually be handled by Activity or View. Obviously, when the pop-up window is displayed, the pop-up window has the current focus. , And the onKeyUp() and onKeyDown() methods are rewritten in the Activity, so the Activity cannot get the key event.

  • Set button monitoring for the pop-up windowsetOnKeyListener()

This is the simplest solution, code example:

        
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        dialog = builder.create();
        dialog.setTitle("弹窗示例");
        dialog.setView(view, 0, 0, 0, 0);
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
    
    
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
    
    
                //指定按键事件:监听音量+键松开事件
                if (keyCode == KeyEvent.KEYCODE_VOLUME_UP && event.getAction() == KeyEvent.ACTION_UP) {
    
    
                    // TODO: 2021/3/21
                   Toast.makeText(this, "音量+键 被点击!", Toast.LENGTH_SHORT).show();
                }
                return false;
            }
        });

  • Override the dispatchKeyEvent event of the pop-up window

This method is a little more complicated, you need to create a new MyDialog, inherit AlertDialog, and then rewrite the dispatchKeyEvent()method, reserve the interface for Activity callback to make it more flexible, code example:

MyDialog class


public class MyDialog extends AlertDialog {
    
    

    public MyDialog(Context mContext) {
    
    
        super(mContext);
    }

    @Override
    public boolean dispatchKeyEvent(android.view.KeyEvent event) {
    
    
        //指定按键事件:监听音量+键松开事件
        if (event.getKeyCode() == android.view.KeyEvent.KEYCODE_VOLUME_UP && event.getAction() == KeyEvent.ACTION_UP) {
    
    
            if (mKeyEventListener != null) mKeyEventListener.onKeyEvent();
        }
        return super.dispatchKeyEvent(event);
    }

    private OnKeyEventListener mKeyEventListener;
    public void setOnKeyEventListener(OnKeyEventListener mKeyEventListener) {
    
    
        this.mKeyEventListener = mKeyEventListener;
    }
    public interface OnKeyEventListener {
    
    
        void onKeyEvent();
    }
}


Use in Activity:


        myDialog = new MyDialog(this);
        myDialog.setTitle("自定义弹窗");
        myDialog.setView(view, 0, 0, 0, 0);
        myDialog.setOnKeyEventListener(new MyDialog.OnKeyEventListener() {
    
    
            @Override
            public void onKeyEvent() {
    
    
                // TODO: 2021/3/21
                Toast.makeText(this, "音量+键 被点击!", Toast.LENGTH_SHORT).show();
            }
        });
        

Thank you very much for seeing here, it would be my honor to help you!

Guess you like

Origin blog.csdn.net/qq_36270361/article/details/115047495