如何防止软键盘出现在我的活动中?

发布于 2024-10-12 00:41:00 字数 1112 浏览 10 评论 0原文

我正在编写一个以全屏横向模式运行的 Android 游戏,并且按钮位于窗口的左下角和右下角。问题是这些按钮之一(在许多手机上)就在“菜单”按钮旁边,因此玩家可能会意外地按“菜单”。

如果短暂按下它,我只需暂停游戏并显示游戏内菜单。那里没问题。

但如果按住按钮的时间更长,Android 会在屏幕下半部分打开软键盘。由于它会妨碍并且在此活动中完全无用,因此我想禁用它。

我尝试了以下方法。

通过 InputMethodManager

来自: 隐藏 Activity 上的软键盘,无需任何键盘操作< /a>

由于我只有一个视图(GLSurfaceView),所以我在 Activity.onCreate() 中尝试了此操作:

InputMethodManager imm = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
imm.hideSoftInputFromInputMethod(glSurfaceView.getApplicationWindowToken(), 0);

它不起作用:软键盘仍然出现在菜单上长按。

通过 AndroidManifest.xml

来自: 如何阻止 Android 软键盘出现在我的整个应用程序中

我将其添加到我的清单中:

<activity 
    android:windowSoftInputMode="stateAlwaysHidden"
>

也什么都不做。

那么……还有办法吗?如何?

I'm writing an Android game that runs in fullscreen landscape mode, and has buttons placed at the bottom left and bottom right of the window. The problem is that one of these buttons is (on many phones) right next to the Menu button, so the player might accidentally press Menu instead.

If it is pressed briefly, I simply pause the game and show the in-game menu. No problem there.

But if the button is held down longer, Android opens up the soft keyboard on the bottom half of the screen. Since it gets in the way, and is completely useless in this Activity, I would like to disable it.

I tried the following approaches.

Via InputMethodManager

From: Hide soft keyboard on activity without any keyboard operations

Since I have only one view (a GLSurfaceView) I tried this in my Activity.onCreate():

InputMethodManager imm = ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE));
imm.hideSoftInputFromInputMethod(glSurfaceView.getApplicationWindowToken(), 0);

It doesn't work: the soft keyboard still comes up on Menu long-press.

Via the AndroidManifest.xml

From: How to stop the android soft keyboard from ever coming up in my entire application

I added this to my manifest:

<activity 
    android:windowSoftInputMode="stateAlwaysHidden"
>

Does a great deal of nothing as well.

So... is there even a way? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心房的律动 2024-10-19 00:41:00

这至少是我眼前问题的解决方案。无论按下按钮多长时间,它都会显示游戏内菜单。

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

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    // From the docs:
    // "Note that in order to receive this callback, someone in the event [chain]
    // must return true from onKeyDown(int, KeyEvent) and call startTracking() on the event."
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // Override default handling, and don't pop up the soft keyboard.
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

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

但这感觉就像一个黑客,所以我希望有人能提出更好的解决方案。

Here is, at least, a solution to my immediate problem. It shows the in-game menu, no matter how long the button was pressed.

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

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    // From the docs:
    // "Note that in order to receive this callback, someone in the event [chain]
    // must return true from onKeyDown(int, KeyEvent) and call startTracking() on the event."
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        // Override default handling, and don't pop up the soft keyboard.
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

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

But it feels like a hack, so I'm hoping that someone comes up with a better solution.

牵强ㄟ 2024-10-19 00:41:00

但是如果按住按钮的时间更长,
Android 打开软键盘
屏幕的下半部分。

你有什么电话?你确定吗?我从来没有见过这种情况发生,我只是尝试了一下,但它在我的手机上不起作用。

另外,这听起来像是一个用户问题。不要试图颠覆用户。如果用户真的想在您的应用程序中打开键盘,您应该让他们打开,如果没有用,他们会回击,键盘就会消失。

更令人担忧的问题应该是您的按钮离菜单按钮太近了。

But if the button is held down longer,
Android opens up the soft keyboard on
the bottom half of the screen.

What phone do you have? Are you sure? I've never once seen that happen and I just tried it and it doesn't work on my phone.

Also, that sounds like a user problem. Don't try to subvert the user. If the user REALLY wants to open a keyboard in your app, you should let them and if it's useless, they'll hit back and it will go away.

A more concerning issue should be that your buttons are so close to the menu buttons.

泪之魂 2024-10-19 00:41:00

尝试使用 hideSoftInputFromWindow( ) 来代替。根据文档:

请求从当前接受输入的窗口上下文中隐藏软输入窗口。

Try using hideSoftInputFromWindow() instead. According to the documentation:

request to hide the soft input window from the context of the window that is currently accepting input.

×眷恋的温暖 2024-10-19 00:41:00

在 android 清单中使用 android:windowSoftInputMode="adjustPan" 。
我认为这是防止视图上升的最佳选择。

use android:windowSoftInputMode="adjustPan" in android manifest.
I think this is best choice to prevent the view goes up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文