我正在使用 AlertDialog.Builder 来创建一个输入框,并使用 EditText 作为输入法。
不幸的是,尽管 EditText 处于焦点位置,但软键盘不会弹出,除非您再次明确触摸它。
有没有办法强制它弹出?
在 (AlertDialog.Builder).show(); 之后,我尝试了以下操作但无济于事。
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);
任何人都可以帮忙吗?
谢谢!!
我做了这样的事
AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
我设法像这样解决它:
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);
dialog.getWindow().setSoftInputMode()
after dialog.show()
会产生令人讨厌的副作用:配置更改后对话框仍保留在屏幕上,仍与已销毁的 Activity/Fragment 相关联。
我发现相同的代码在平板电脑上正常工作,键盘确实弹出,但在手机上却没有,所以进一步研究,似乎指向“调整”选项。
我在用这个,感觉干净多了。
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
能够调整对话框的大小并“向上推”。
在我的情况下,我能够在显示对话框时显示键盘的唯一方法是添加到我的 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;
当您调用 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);
}
}
});
}
给出了一个更好的解决方案 here。
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
没有解决方法。 EditText
的行为符合预期。
就我而言,在显示对话框之前(在创建它之后)设置 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);
}
我希望这可以帮助任何和我有同样问题的人。
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);
已回答 here。使用 OnFocusChangeListener 对我有用。
试试这个,它对我有用
如果要显示软键盘:
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);
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
在调用 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);
}
});
试试这个,它对我有用
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);
}
我找到了一个简单可靠的解决方案,如果您有一个可编辑字段不在根目录中的复杂布局,只需在对话框布局的根目录上放置一个隐藏的 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。
不定期副业成功案例分享
OnFocusChangeListener
方法对我来说似乎都很重要并且会造成麻烦。您必须从AlertDialog.Builder
创建AlertDialog
!setSoftInputMode
行放在 之前dialog.show()
否则它将不起作用。 +1 用于简单正确的解决方案顺便说一句