android - 当使用带有EditText的AlertDialog.Builder时,软键盘不会弹出
我使用AlertDialog.Builder来创建一个输入框,使用EditText作为输入方法。
遗憾的是,虽然EditText处于焦点状态,但软键盘不会弹出,除非您再次明确触摸它。
有没有办法强迫它弹出?
我在(AlertDialog.Builder).show()之后尝试了以下内容。 但无济于事。
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);
有人可以帮忙吗?
谢谢!!
10个解决方案
210 votes
我做了这样的事
AlertDialog.Builder b = new AlertDialog.Builder(this);//....
AlertDialog dialog = b.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
grine4ka answered 2019-07-14T19:19:54Z
25 votes
我设法像这样解决它:
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);
Alexander Fragotsis answered 2019-07-14T19:20:17Z
19 votes
我发现相同的代码在平板电脑上正常工作,键盘确实弹出,但在手机上没有,所以进一步研究似乎指向“调整”选项。
我正在使用它,感觉更干净。
AlertDialog d = builder.create();
d.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
d.show();
Phuah Yee Keat answered 2019-07-14T19:20:47Z
6 votes
当您调用showDialog以显示使用onCreateDialog中的AlertDialog创建的对话时
你应该把代码放在这里
@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);
}
}
});
}
user590912 answered 2019-07-14T19:21:17Z
6 votes
在我的情况下,当对话框显示时我能够显示键盘的唯一方法是添加到我的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。
来自文档:
int SOFT_INPUT_STATE_ALWAYS_VISIBLE
Visibility state for softInputMode: please always make the soft input area visible when this window receives input focus.
vovahost answered 2019-07-14T19:21:55Z
5 votes
这里给出了一个更好的解决方案。
dialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
没有解决方法。 EditText表现如预期。
sulai answered 2019-07-14T19:22:25Z
2 votes
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);
Mohammed Shoeb answered 2019-07-14T19:22:42Z
1 votes
这已在这里得到解答。 使用OnFocusChangeListener为我工作。
dhaag23 answered 2019-07-14T19:23:06Z
0 votes
试试这个,它为我工作
如果要显示软键盘:
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);
Yogesh Rathi answered 2019-07-14T19:23:43Z
0 votes
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
TonnyTao answered 2019-07-14T19:24:00Z