Non-cancelable dialog being dismissed on search button click

蓝咒 提交于 2019-12-06 07:33:00

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.

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.

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