软键盘仅在 EditText 焦点上显示一次
感谢您的阅读。
我面临一个奇怪的问题:我的应用程序行为是这样的,当 Activity
启动时,我在 EditText
上 requestFocus()
并显示软键盘。
但是,当我按后退按钮关闭键盘并点击 EditText
时,我不会再次弹出键盘。唯一的出路是再次启动Activity
。
这是我的代码:
EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(0, 0);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
这是我的 XML:
<EditText android:id="@+id/editText"
android:layout_width="wrap_content"
android:imeOptions="actionSearch" android:hint="Test Hint"
android:layout_height="wrap_content" android:layout_centerHorizontal="true"
android:maxLength="30">
</EditText>
任何帮助将不胜感激!
谢谢!
Thanks for reading.
I am facing a strange problem: My app behavior is such that when the Activity
starts, I requestFocus()
on an EditText
and show the soft keyboard.
However, when I press the back button to dismiss the keyboard and tap the EditText
I don't get the keyboard to pop up ever again. Only way out is to the start the Activity
again.
Here's what my code looks like:
EditText editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null) {
imm.toggleSoftInput(0, 0);
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
And here's my XML:
<EditText android:id="@+id/editText"
android:layout_width="wrap_content"
android:imeOptions="actionSearch" android:hint="Test Hint"
android:layout_height="wrap_content" android:layout_centerHorizontal="true"
android:maxLength="30">
</EditText>
Any help would be greatly appreciated!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试在
Runnable
中打开和隐藏,如TO OPEN
TO CLOSE
Try to open and hide inside a
Runnable
as,TO OPEN
TO CLOSE
您使用了错误的视图来显示输入窗口。
You used the wrong view for showing the input window.
试试这个:
try this:
在请求焦点之前清除强制
textField.clearFocus();
之前,上述任何操作都不起作用,因此我在onResume
中的最终代码如下所示。None of above work until i clear the forces
textField.clearFocus();
before requesting focus, so my final code inonResume
looks like this.在您的 Activity 中使用此编码,它将隐藏您的键盘
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Used this coding in your Activity,It will hide your keyboard
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
我连续几天都面临同样的问题。通过在代码中创建线程和延迟提供的解决方案效率不高。框架有解决方案。您可以在这里查看我的简短解决方案: https://stackoverflow.com/a/74442682/6906562
I faced the same problems for days. Solutions provided with creating threads and delays in the code are not efficient. The framework has solution. you can check my short solution here: https://stackoverflow.com/a/74442682/6906562