我正在使用AlertDialog
显示一个输入框。 当我调用AlertDialog.show()
,对话框内部的EditText
会自动聚焦,但软键盘不会自动显示。 android
如何在显示对话框时自动显示软键盘? (而且没有物理/硬件键盘)。 与按下“搜索”按钮调用全局搜索的方式相似,将自动显示软键盘。 ide
若是有人获得: ui
没法从类型Activity对静态方法getSystemService(String)进行静态引用 spa
尝试向getSystemService调用添加上下文 。 code
因此 对象
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
是的,你能够用setOnFocusChangeListener
来帮助你。 get
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); } } });
用于显示键盘用途: input
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
隐藏键盘使用: it
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(),0);
让我指出一些额外的信息到yuku的解决方案,由于我发现很难让这个工做! 如何从AlertDialog.Builder获取AlertDialog对象? 好吧,这是个人alert.show()
执行的结果: List
final AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); final EditText input = new EditText(getActivity()); alert.setView(input); // do what you need, like setting positive and negative buttons... final AlertDialog dialog = alert.show(); input.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus) { dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); } } });
<activity ... android:windowSoftInputMode="stateVisible" > </activity>
要么
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);