【问题标题】:Android Back Button Overidding HelpAndroid 后退按钮覆盖帮助
【发布时间】:2011-05-03 00:05:33
【问题描述】:

我正在制作一个应用程序、一个游戏,并且我希望玩家能够使用后退按钮进行跳跃(对于单点触控设备)。我的目标平台是 2.1(API 级别 7)。

我已经尝试过 onKeyDown() 和 onBackPressed(),但它们仅在返回按钮被释放时调用,而不是在按下时调用。

1) 这正常吗?

2) 我怎样才能让它在按下按钮时注册按下?

编辑: 我还想补充一点,它可以使用键盘正常工作(按下键时调用 onKeyDown)。

【问题讨论】:

    标签: android back-button android-2.2-froyo android-2.1-eclair


    【解决方案1】:

    更新:我对此感到好奇。看看android.view.View源码:http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/view/View.java

    一个典型的例子是处理 BACK 键来更新应用程序的 UI,而不是让 IME 看到它并自行关闭。

    代码:

    /**
     * Handle a key event before it is processed by any input method
     * associated with the view hierarchy.  This can be used to intercept
     * key events in special situations before the IME consumes them; a
     * typical example would be handling the BACK key to update the application's
     * UI instead of allowing the IME to see it and close itself.
     *
     * @param keyCode The value in event.getKeyCode().
     * @param event Description of the key event.
     * @return If you handled the event, return true. If you want to allow the
     *         event to be handled by the next receiver, return false.
     */
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        return false;
    }
    

    使用 dispatchKeyEvent:

    @Override
    public boolean dispatchKeyEvent (KeyEvent event) {
        Log.d("**dispatchKeyEvent**", Integer.toString(event.getAction()));
        Log.d("**dispatchKeyEvent**", Integer.toString(event.getKeyCode()));
        if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
            Toast.makeText(this, "Back button pressed", Toast.LENGTH_LONG).show();
            return true;
        }
        return false;
    }
    

    独立记录这两个事件,即使是返回键。出于某种原因,唯一没有记录的键是KEYCODE_HOME。事实上,如果您保持按下后退按钮,您将连续看到几个ACTION_DOWN (0) 事件(如果您改为return false;,则会看到更多)。在 Eclair 模拟器和 Samsung Captivate(自定义 Froyo ROM)中测试。

    【讨论】:

    • 这没有帮助。首先,添加一个 onKeyListener 并使用 onKey 只会捕获来自键盘的输入,而不是硬件按钮,例如“返回”、“主页”等按钮。很明显,带有 onKeyDown 的事件总是有 ACTION_DOWN 作为它的动作,而 onKeyUp 有 ACTION_UP。我觉得你没有阅读我写的所有内容(而且阅读时间不长)。
    • @Jonathan 你可能是对的,它没有被调用(我没有特别测试它),但我确实阅读了你的帖子。我建议这个选项是因为:Default implementation of KeyEvent.Callback.onKeyDown(): perform press of the view when KEYCODE_DPAD_CENTER or KEYCODE_ENTER is released, if the view is enabled and clickable.(查看参考)。也许 KEYCODE_BACK 被当作其他两个处理(续)。
    • 还有这个:onKeyDown(int, KeyEvent) Called when a new key event occurs. onKeyUp(int, KeyEvent) Called when a key up event occurs. (developer.android.com/guide/topics/ui/custom-components.html) 请注意,它实际上并没有说“发生新键 down 事件时调用”。这会导致歧义,允许对不同的KEYCODEs 进行不同的实现。也就是说,我没有阅读 OnKeyListener.java 文件的源代码来确定这些。
    • @Jonathan 我对此进行了更多研究。请参阅上面的更新答案。
    • @Aleadam 我似乎无法让 onKeyPreIme 与我的 SurfaceView 一起工作,但我尝试了public boolean dispatchKeyEvent(KeyEvent event)(似乎是 Activity 的等效 onKeyPreIme)但结果相同,ACTION_DOWN 和 ACTION_UP似乎同时被调用:当按钮被释放时。如果您能在您的设备上试用,我将不胜感激。我想知道这只是我的设备还是每个人都一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多