Non-cancelable dialog being dismissed on search button click

瘦欲@ 提交于 2019-12-12 10:01:29

问题


I'm showing a non-cancelable dialog in my application, but it gets cancelled if the user presses SEARCH button. I've tried to override onSearchRequested and onKeyDown, but it doesn't help. Any suggestion?


回答1:


I also came across this problem and Jamasan's solution did not work for me. I instead added the following code to my custom dialog class (extending Dialog):

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        return true;
    } else {
        return false;
    }
}

keyCode and KeyEvent.KEYCODE_SEARCH are both int. The docs for onKeyDown says

If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

Works for me.




回答2:


Override the Activity's onKeyDown event, and check for KEYCODE_SEARCH to return false

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    char c = (char) event.getUnicodeChar();

    if (c == KeyEvent.KEYCODE_SEARCH) {
        return false;
    } else {
        return super.onKeyDown(keyCode, event);
    }
}

Returning false just blocks that key press (as if it didn't happen). Otherwise running super.onKeyDown(..) just processes it regularly.

Good luck.



来源:https://stackoverflow.com/questions/5200413/non-cancelable-dialog-being-dismissed-on-search-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!