[英]when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop
我正在使用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);
我发现相同的代码在平板电脑上正常工作,键盘确实弹出,但在手机上却没有,所以进一步研究,似乎指向“调整”选项。
我正在用这个,感觉干净多了。
AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();
在我的例子中,当显示对话框时我能够显示键盘的唯一方法是添加到我的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);
}
}
});
}
这里给出了一个更好的解决方案。
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);
这已经在这里回答了。 使用 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);
因为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);
}
});
试试这个,它对我有用
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 的这一部分。
我曾经使用上面的onResume
解决方案,但我无法使用更简单的AlertDialog.Builder()
API 来删除AppCompatDialogFragment
的使用,但现在我可以简单地使用更简单的 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.