显示对话框的软键盘

发布于 2024-10-04 04:25:14 字数 1083 浏览 6 评论 0原文

我正在显示一个带有编辑文本视图的对话框。但是,只有当用户在编辑视图内按下时,软键盘才会打开。所以我尝试使用以下代码调用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 技术交流群。

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

发布评论

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

评论(6

阿楠 2024-10-11 04:25:14

很棒的问题,我也试图这样做并找到了解决方案。

使用对话框构建器类AlertDialog.Builder,您必须像这样调用对话框:

AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;

builder.set...

dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();

这对我来说效果很好。

注意:您必须导入 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:

AlertDialog.Builder builder = new AlertDialog.Builder();
AlertDialog dialog;

builder.set...

dialog = builder.create();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();

This worked fine for me.

Note: you must import android.view.WindowManager.LayoutParams; for the constant value there.

锦上情书 2024-10-11 04:25:14
 AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.show();
    Window window = dialog.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
 AlertDialog dialog = new AlertDialog.Builder(this).create();
    dialog.show();
    Window window = dialog.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
尛丟丟 2024-10-11 04:25:14

Kotlin

这是经过测试的代码。

val dialog = AlertDialog.Builder(requireContext()).apply {
    setTitle(…)
    setView(editText)
    setPositiveButton(…)
    setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)

确保通过 show() 方法访问 window 属性。从 create() 方法获取 window 为我返回 null,因此键盘没有显示。

androidx.appcompat.app.AlertDialog 导入 AlertDialog
android.view 导入 WindowManager

Kotlin

Here's the tested code.

val dialog = AlertDialog.Builder(requireContext()).apply {
    setTitle(…)
    setView(editText)
    setPositiveButton(…)
    setNegativeButton(…)
}
val window = dialog.show().window
window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)

Make sure you access the window property from show() method. Getting window from create() method was returning null for me, so the keyboard wasn't showing.

Import AlertDialog from androidx.appcompat.app.AlertDialog.
Import WindowManager from android.view.

电影里的梦 2024-10-11 04:25:14

这是我的解决方案,它对于对话效果很好。

txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

也许您还需要将其添加到 AndroidManifest.xml 中的活动标记中,以便在对话框关闭时关闭键盘。

android:windowSoftInputMode="stateAlwaysHidden"

Here's my solution, it's working well for dialog.

txtFeedback.requestFocus();
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

Maybe also you need to add this to your activity tag in AndroidManifest.xml for closing the keyboard when the dialog is dismissed.

android:windowSoftInputMode="stateAlwaysHidden"
一袭水袖舞倾城 2024-10-11 04:25:14

使用 Kotlin 的对话框片段

请覆盖 onStart 方法,然后使用以下代码覆盖关闭方法

override fun onStart() {
    super.onStart()
    dialog.window?.
    setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
    )
}

如果您想在关闭后关闭,

override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

Dialog Fragment With Kotlin

override onStart Method

override fun onStart() {
    super.onStart()
    dialog.window?.
    setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE
    )
}

if you want to close after dismiss then override dismiss method with below code

override fun onDismiss(dialog: DialogInterface?) {
val inputMethodManager = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
你又不是我 2024-10-11 04:25:14

我已经尝试并取得了成功:

editText.requestFocus();
dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);

I have tried and had success with:

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