ChatGPT解决这个技术问题 Extra ChatGPT

当使用带有 EditText 的 AlertDialog.Builder 时,软键盘不会弹出

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

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

有没有办法强制它弹出?

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

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

任何人都可以帮忙吗?

谢谢!!

请格式化您的源代码。
然后我也支持你:)我在几个小时内搜索了同样的问题,而 grine4ka 的最后一个答案效果很好

m
mcont

我做了这样的事

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

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

dialog.show();

太感谢了。我已经搜索了一段时间,这就是你想走的路。所有的 OnFocusChangeListener 方法对我来说似乎都很重要并且会造成麻烦。您必须从 AlertDialog.Builder 创建 AlertDialog
这真的是一个解决方案吗?这只是强制键盘显示,无论是否有输入字段,无论输入字段是否有焦点,对吧? =)
我实际上设法“解决它”(解决方法)。我将 setOnFocusChangeListener 用于 EditText,并在 onFocusChange 中检查它是否有焦点(“hasFocus”var),如果有,我会 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
注意:为此,您需要将 setSoftInputMode 行放在 之前 dialog.show() 否则它将不起作用。 +1 用于简单正确的解决方案顺便说一句
如果它不起作用,添加行'edittext.requestFocus()',它对我有用
A
Alex Fragotsis

我设法像这样解决它:

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);

请注意,在 Android 4-8 上调用 dialog.getWindow().setSoftInputMode() after dialog.show() 会产生令人讨厌的副作用:配置更改后对话框仍保留在屏幕上,仍与已销毁的 Activity/Fragment 相关联。
P
Phuah Yee Keat

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

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

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

谢谢。这肯定比使用 SOFT_INPUT_STATE_ALWAYS_VISIBLE 更好。由于 SOFT_INPUT_STATE_ALWAYS_VISIBLE 将阻止对话框的 UI 组件,因此 SOFT_INPUT_ADJUST_RESIZE 能够调整对话框的大小并“向上推”。
v
vovahost

在我的情况下,我能够在显示对话框时显示键盘的唯一方法是添加到我的 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;

这是唯一对我有用的解决方案,我已经尝试了很多。我的是来自 alertdialog builder 的 dialogfragment 构建。重要的一点似乎是将上面的代码放在 onResume() 中。其他任何地方都行不通!
v
vovahost

当您调用 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);
         }
       }
    });
}

C
Community

给出了一个更好的解决方案 here

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

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


这个对我有用,另一种解决方案是带来焦点,但没有显示键盘。
A
AurumTechie

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

科特林:

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)
}

爪哇:

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);
}

我希望这可以帮助任何和我有同样问题的人。


M
Mohammed Shoeb
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);

C
Community

已回答 here。使用 OnFocusChangeListener 对我有用。


该问题询问如何为 AlertDialog.Builder 对象设置软输入模式,但是您引用的线程给出了使用 AlertDialog 对象的示例。如果我尝试使用建议的代码(在 OnFocusChangeListener 中使用 alert.getWindow().setSoftInputMode(...))Eclipse 对象,则没有为 AlertDialog.Builder 类型定义方法 getWindow()。你能帮我解决这个问题吗?
s
sud007

试试这个,它对我有用

如果要显示软键盘:

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);

T
TonnyTao
final AlertDialog.Builder alert = new AlertDialog.Builder(context);

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

最好在代码中包含一些上下文/解释,因为这会使答案对 OP 和未来的读者更有用。
S
Sungsuh Park

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

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

AlertDialog 类会根据对话框中的任何视图是否从 View.onCheckIsTextEditor() 返回 true,自动为您设置 android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM。

您需要清除 FLAG_ALT_FOCUSABLE_IM 标志。

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

因为在DialogFragment.onStart 中调用了AlertDialog.show,所以可以在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);
    }
});

M
Manideep

试试这个,它对我有用

    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);
    }

E
Ebrahim Byagowi

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

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

这基本上是为了欺骗 compat/androidx 的 this part

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