prevent取消操作模式由preSS后退按钮按钮、模式、操作、prevent

2023-09-12 21:46:35 作者:残留了一地的伤

操作模式 getActivity()startActionMode(calback); 将被自动取消后,后退按钮pressed。有可能避免这种行为呢?我需要做的另一个操作后的后退按钮是$ P $中的行动模式pssed在某些情况下。

Action mode started by calling getActivity().startActionMode(calback); is automatically canceled after back button pressed. Is possible avoid this behavior? I need to do another operation after back button was pressed in some situation during action mode.

推荐答案

这是一个有趣的问题。当ActionMode有效的返回键事件被内部消耗。本次活动是不可以传播为 onBack pressed()的onkeyup(INT键code, KeyEvent的事件)回调。

This is an interesting problem. When the ActionMode is active the back key event is consumed internally. The event is not propagated to either onBackPressed() or onKeyUp(int keyCode, KeyEvent event) callbacks.

幸运的是,你可以使用 dispatchKeyEvent(KeyEvent的事件)这仍称。

Fortunately, you can use dispatchKeyEvent(KeyEvent event) which is still called.

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if(mActionModeIsActive) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
           // handle your back button code here
           return true; // consumes the back key event - ActionMode is not finished
        }
    }
    return super.dispatchKeyEvent(event);
}

您可能不知道这将是你在ActionMode子菜单的情况下的行为,你与返回键关闭它。在这种情况下 dispatchKeyEvent()不叫,所以你可以放心地使用code。

You might wonder what will be the behavior in case you have a submenu in the ActionMode and you close it with the back key. In this case dispatchKeyEvent() is not called so you can safely use the code.

以上code工作也与ActionBarSherlock。我发现的唯一的问题是在Android 3.1设备时,本机ActionMode被使用,在这种情况下, dispatchKeyEvent()不叫。使用ActionBarSherlock的ActionMode来解决它。

The above code works also with ActionBarSherlock. The only problem I found is on Android 3.1 device when the native ActionMode is used, in this case the dispatchKeyEvent() is not called. Use ActionBarSherlock's ActionMode to solve it.

 
精彩推荐