如何防止软键盘出现在我的活动中?
我正在编写一个以全屏横向模式运行的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这至少是我眼前问题的解决方案。无论按下按钮多长时间,它都会显示游戏内菜单。
但这感觉就像一个黑客,所以我希望有人能提出更好的解决方案。
Here is, at least, a solution to my immediate problem. It shows the in-game menu, no matter how long the button was pressed.
But it feels like a hack, so I'm hoping that someone comes up with a better solution.
你有什么电话?你确定吗?我从来没有见过这种情况发生,我只是尝试了一下,但它在我的手机上不起作用。
另外,这听起来像是一个用户问题。不要试图颠覆用户。如果用户真的想在您的应用程序中打开键盘,您应该让他们打开,如果没有用,他们会回击,键盘就会消失。
更令人担忧的问题应该是您的按钮离菜单按钮太近了。
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.
尝试使用 hideSoftInputFromWindow( ) 来代替。根据文档:
Try using hideSoftInputFromWindow() instead. According to the documentation:
在 android 清单中使用 android:windowSoftInputMode="adjustPan" 。
我认为这是防止视图上升的最佳选择。
use android:windowSoftInputMode="adjustPan" in android manifest.
I think this is best choice to prevent the view goes up.