Android - how to disable long press on Search button (Nexus One)

故事扮演 提交于 2019-12-30 05:23:24

问题


The Android documentation describes how to disable the search the search feature in Activity: public boolean onSearchRequested() { return false; }

This works fine for a short press of the search button on the Nexus One. However, it doesn't disable the long press, which still fires off a voice search.

How do I disable the long press Voice search?

Thanks...


回答1:


I extended Stan's answer to only disable long press events.

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

    return super.onKeyDown(keyCode, event);
}



回答2:


It doesn't seem like a very nice thing to do to users, but onKeyDown can be used to disable both tap and long press search in an Activity as shown here:

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



回答3:


In Android 2.0, you can do that :

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_CALL) {
        // todo your code here
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}


来源:https://stackoverflow.com/questions/2215004/android-how-to-disable-long-press-on-search-button-nexus-one

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