将 AlertDialog.Builder 与 EditText 一起使用时,软键盘不会弹出

发布于 2024-09-13 23:54:45 字数 453 浏览 14 评论 0原文

我正在使用 AlertDialog.Builder 来创建一个输入框,并使用 EditText 作为输入方法。

不幸的是,尽管 EditText 处于焦点状态,但软键盘不会弹出,除非您再次明确触摸它。

有没有办法强制它弹出?

我已尝试以下操作,在 (AlertDialog.Builder).show(); 之后,但无济于事。

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);

有人可以帮忙吗?

谢谢!!

I am using AlertDialog.Builder in order to create an input box, with EditText as the input method.

Unfortunately, the Soft Keyboard doesn't pop, although the EditText is in focus, unless you explicitly touch it again.

Is there a way to force it to pop?

I've tried the following, after the (AlertDialog.Builder).show(); but for no avail.

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);

Anyone can help?

Thanks!!

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

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

发布评论

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

评论(14

无尽的现实 2024-09-20 23:54:48

试试这个,它对我有用

    Window window = dialog.getWindow();
    if (window != null) { // After the window is created, get the SoftInputMode
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

Try this, its working for me

    Window window = dialog.getWindow();
    if (window != null) { // After the window is created, get the SoftInputMode
        window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }
听不够的曲调 2024-09-20 23:54:48

我找到了一个简单可靠的解决方案,如果你有一个复杂的布局,可编辑字段不在根目录中,只需在对话框布局的根目录中放置一个隐藏的 EditText ,

<!-- Just to trick AlertDialog to not hide soft keyboard -->
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />

这基本上是为了欺骗 这部分兼容/androidx。

我曾经使用上面的 onResume 解决方案,但我无法使用 AlertDialog.Builder() 的更简单的 API 来删除 AppCompatDialogFragment 的使用但现在我可以简单地使用更简单的 API。

I found an easy and reliable solution to this, just put a hidden EditText on root of your dialog layout if you got a complex layout which an editable field isn't in root,

<!-- Just to trick AlertDialog to not hide soft keyboard -->
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone" />

This is basically to trick this part of compat/androidx.

I used to use onResume solution above but with that I couldn't use simpler API of AlertDialog.Builder() to remove the use of AppCompatDialogFragment but now I can simply use the easier API.

捂风挽笑 2024-09-20 23:54:47

试试这个,它对我有用

如果你想显示软键盘:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(input.getWindowToken(), 0);

如果你想隐藏它:

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);

Try this, its working for me

If you want to display soft keyboard:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(input.getWindowToken(), 0);

And if you want to hide the it:

  InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
莫相离 2024-09-20 23:54:47
final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
蓝眸 2024-09-20 23:54:46

我做了这样的事情

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();

I've made such a thing

AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

dialog.show();
与君绝 2024-09-20 23:54:46

我已经设法像这样解决它:

Dialog = builder.create();
Dialog.show();
Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

I've managed to solve it like this:

Dialog = builder.create();
Dialog.show();
Dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
小梨窩很甜 2024-09-20 23:54:46

我发现相同的代码在平板电脑上可以正常工作,键盘确实会弹出,但在手机上却不会,因此进一步研究,似乎指向“调整”选项。

我正在用这个,感觉干净很多。

AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();

I found out that the same code works properly on Tablet, the keyboard does pop up, but on Phone it doesn't, so researching further, seems to point to the "adjust" option.

I am using this, feels much cleaner.

AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();
空宴 2024-09-20 23:54:46

就我而言,在显示对话框时能够显示键盘的唯一方法是添加到我的 DialogFragment

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    myEditText.requestFocus();
}

请注意 SOFT_INPUT_STATE_ALWAYS_VISIBLE 而不是 SOFT_INPUT_STATE_VISIBLE.

来自文档:

// Visibility state for softInputMode: please always make the soft input 
// area visible when this window receives input focus.
int SOFT_INPUT_STATE_ALWAYS_VISIBLE;

In my case the only way I was able to show the keyboard when the Dialog was shown was by adding to my DialogFragment:

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    myEditText.requestFocus();
}

Note the SOFT_INPUT_STATE_ALWAYS_VISIBLE instead of SOFT_INPUT_STATE_VISIBLE.

From documentation:

// Visibility state for softInputMode: please always make the soft input 
// area visible when this window receives input focus.
int SOFT_INPUT_STATE_ALWAYS_VISIBLE;
硬不硬你别怂 2024-09-20 23:54:46

此处给出了更好的解决方案。

dialog.getWindow().clearFlags(
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

没有解决方法。 EditText 的行为符合预期。

A much better solution is given here.

dialog.getWindow().clearFlags(
         WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        |WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

No workaround. EditText behaves as expected.

甚是思念 2024-09-20 23:54:46

就我而言,当我在显示对话框之前(创建对话框之后)设置 SoftInputMode 时,它​​没有显示。下面的代码对我有用,我在显示对话框后设置了 SoftInputMode。

Kotlin:

val dialog = MaterialAlertDialogBuilder(context) // Other builder code
                .create()
dialog.show()
dialog.window?.apply { // After the window is created, get the SoftInputMode
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}

Java:

AlertDialog dialog = MaterialAlertDialogBuilder(getContext()) // Other builder code
                .create();
dialog.show();
Window window = dialog.getWindow();
if(window != null){ // After the window is created, get the SoftInputMode
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

我希望这可以帮助任何与我遇到同样问题的人。

In my case, the SoftInputMode wasn't getting displayed when I set it which was before showing the dialog (after creating it). The below code worked for me where I set the SoftInputMode after showing the dialog.

Kotlin:

val dialog = MaterialAlertDialogBuilder(context) // Other builder code
                .create()
dialog.show()
dialog.window?.apply { // After the window is created, get the SoftInputMode
    clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
    setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
}

Java:

AlertDialog dialog = MaterialAlertDialogBuilder(getContext()) // Other builder code
                .create();
dialog.show();
Window window = dialog.getWindow();
if(window != null){ // After the window is created, get the SoftInputMode
    window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

I hope this helps anyone who was having the same problem as me.

南城追梦 2024-09-20 23:54:46

当您调用 showDialog() 来显示在 onCreateDialog() 中使用 AlertDialog 创建的对话框时,

您应该将代码放在 onPrepareDialog() 中:

@Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
    TextView editText=(TextView) dialog.findViewById(R....);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       @Override
       public void onFocusChange(View v, boolean hasFocus) {
         if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
         }
       }
    });
}

When you call showDialog() to show a Dialog created using AlertDialog in onCreateDialog()

You should put the code in onPrepareDialog():

@Override
protected void onPrepareDialog (int id, Dialog dialog, Bundle args)
{
    TextView editText=(TextView) dialog.findViewById(R....);

    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       @Override
       public void onFocusChange(View v, boolean hasFocus) {
         if (hasFocus) {
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
         }
       }
    });
}
一花一树开 2024-09-20 23:54:46
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);
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-09-20 23:54:46

此处已经回答了这个问题。使用 OnFocusChangeListener 对我有用。

This was answered here already. Using an OnFocusChangeListener worked for me.

演多会厌 2024-09-20 23:54:46

调用 AlertDialog.onCreate 后添加 EditText 时会出现此问题。

https://developer.android.com/reference/androidx/appcompat/应用程序/AlertDialog.Builder

AlertDialog 类负责自动设置
android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM 为您服务
基于对话框中的任何视图是否返回 true
View.onCheckIsTextEditor()。

您需要清除 FLAG_ALT_FOCUSABLE_IM 标志。

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 

因为AlertDialog.show是在DialogFragment.onStart中调用的,所以可以在DialogFragment.onStart中插入代码。

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

或者,如果您不使用 DialogFragment,则可以使用 Dialog.setOnShowListener。

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }
});

This problem occurs when EditText is added after AlertDialog.onCreate is called.

https://developer.android.com/reference/androidx/appcompat/app/AlertDialog.Builder

The AlertDialog class takes care of automatically setting
android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM for you
based on whether any views in the dialog return true from
View.onCheckIsTextEditor().

You need to clear the FLAG_ALT_FOCUSABLE_IM flag.

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 

Because AlertDialog.show is called in the DialogFragment.onStart, you can insert the code in the DialogFragment.onStart.

@Override
public void onStart() {
    super.onStart();
    getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}

Or you can use the Dialog.setOnShowListener if you do not use a DialogFragment.

dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文