显示对话框的软键盘
我正在显示一个带有编辑文本视图的对话框。但是,只有当用户在编辑视图内按下时,软键盘才会打开。所以我尝试使用以下代码调用InputMethodManager。
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);
对话框字段是输入字段。但是,我到底什么时候应该这样做呢?我在对话框的 onStart() 方法中尝试过,但没有任何反应。我之前也尝试过请求对话字段的焦点,但这没有任何改变。
我也在两个版本中尝试过这段代码
dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange (View v, boolean hasFocus)
{
if (hasFocus)
{
Main.log("here");
dialogInput.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
/*
InputMethodManager mgr =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(dialogField,0);
*/
}
}
});
。但没有软键盘愿意出现。 Main.log 只是一个日志,它向我显示该函数实际上被调用了。是的,它被称为。
在对话框打开之前,我可以使用 SHOW_FORCED 标志获取键盘。但退出时它不会关闭。我只能在显示对话框之前执行此操作。在任何回调内它也不起作用。
I am displaying a dialog with an edittext view. However, the softkeyboard will open only if the user presses inside the editview. So I tried calling an InputMethodManager with the following code.
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(dialogField,0);
The dialogField is the input field. However, when exactly am I supposed to do this? I tried it in the onStart() method of the dialog, but nothing happens. I also tried requesting the focus for the dialogField before, but that changes nothing.
I also tried this code
dialogField.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
public void onFocusChange (View v, boolean hasFocus)
{
if (hasFocus)
{
Main.log("here");
dialogInput.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
/*
InputMethodManager mgr =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(dialogField,0);
*/
}
}
});
in both versions. But no soft keyboard would like to appear. The Main.log is just a log, which shows me that the function is actually called. And yes, it is called.
I could get the keyboard with the SHOW_FORCED flag before the dialog opens. But then it will not close on exit. And I can only do that BEFORE I show the dialog. Inside any callbacks it does not work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
很棒的问题,我也试图这样做并找到了解决方案。
使用对话框构建器类
AlertDialog.Builder
,您必须像这样调用对话框:这对我来说效果很好。
注意:您必须导入 android.view.WindowManager.LayoutParams; 以获得常量值。
Awesome question, I was trying to do that too and found a solution.
Using the dialog builder class
AlertDialog.Builder
you will have to invoke the dialog like this:This worked fine for me.
Note: you must
import android.view.WindowManager.LayoutParams;
for the constant value there.Kotlin
这是经过测试的代码。
确保通过
show()
方法访问window
属性。从create()
方法获取window
为我返回null
,因此键盘没有显示。从
androidx.appcompat.app.AlertDialog
导入AlertDialog
。从
android.view
导入WindowManager
。Kotlin
Here's the tested code.
Make sure you access the
window
property fromshow()
method. Gettingwindow
fromcreate()
method was returningnull
for me, so the keyboard wasn't showing.Import
AlertDialog
fromandroidx.appcompat.app.AlertDialog
.Import
WindowManager
fromandroid.view
.这是我的解决方案,它对于对话效果很好。
也许您还需要将其添加到 AndroidManifest.xml 中的活动标记中,以便在对话框关闭时关闭键盘。
Here's my solution, it's working well for dialog.
Maybe also you need to add this to your activity tag in AndroidManifest.xml for closing the keyboard when the dialog is dismissed.
使用 Kotlin 的对话框片段
请覆盖 onStart 方法,然后使用以下代码覆盖关闭方法
如果您想在关闭后关闭,
Dialog Fragment With Kotlin
override onStart Method
if you want to close after dismiss then override dismiss method with below code
我已经尝试并取得了成功:
I have tried and had success with: