Android 将 AlertDialog.Builder 与 EditText 一起使用时,软键盘不会弹出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3455235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
when using AlertDialog.Builder with EditText, the Soft Keyboard doesn't pop
提问by niros
I am using AlertDialog.Builderin order to create an input box, with EditText as the input method.
我正在使用AlertDialog.Builder来创建一个输入框,以 EditText 作为输入方法。
Unfortunately, the Soft Keyboard doesn't pop, although the EditTextis in focus, unless you explicitly touch it again.
不幸的是,软键盘不会弹出,尽管EditText是焦点,除非您再次明确触摸它。
Is there a way to force it to pop?
有没有办法强制它弹出?
I've tried the following, after the (AlertDialog.Builder).show();but for no avail.
在 (AlertDialog.Builder).show(); 之后,我尝试了以下方法;但无济于事。
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(input, InputMethodManager.SHOW_FORCED);
Anyone can help?
任何人都可以帮忙吗?
Thanks!!
谢谢!!
回答by grine4ka
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();
回答by Alex Fragotsis
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);
回答by Phuah Yee Keat
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();
回答by vovahost
In my case the only way I was able to show the keyboard when the Dialog was shown was by adding to my DialogFragment
:
在我的情况下,我能够在显示对话框时显示键盘的唯一方法是添加到我的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_VISIBLEinstead of SOFT_INPUT_STATE_VISIBLE.
From documentation:
注意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.
回答by user590912
When you call showDialog to show a Dialogue created using AlertDialog in onCreateDialog
当您调用 showDialog 以显示在 onCreateDialog 中使用 AlertDialog 创建的对话时
You should put the code here
你应该把代码放在这里
@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);
}
}
});
}
回答by sulai
回答by 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);
回答by dhaag23
回答by TonnyTao
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
final AlertDialog dialog = alert.show();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
回答by Yogesh Rathi
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);