DialogFragment并强制显示键盘

我的DialogFragment有问题。 所以要创build我的视图,我使用在Android博客上描述的方法。 这是我的DialogFragment

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View myLayout = inflater.inflate(R.layout.dialog_connect, null); edit = (EditText) myLayout.findViewById(R.id.password_edit); edit.requestFocus(); getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); return myLayout; } 

如果我使用onCreateView(),它的工作原理,但我想创build一个AlterDialog并做到这一点,我有以下代码:

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { callback.onYesConnectClick(edit.getText().toString()); } }) .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { callback.onNoConnectClick(); } }); return builder.create(); } 

如果我从onCreateView()注释代码,该应用程序的作品,但我不能强制键盘显示,如果我取消onCreateView(),我得到一个崩溃。 这是堆栈跟踪:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.ProfileActivity_}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content AndroidRuntime at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2312) AndroidRuntime at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2362) AndroidRuntime at android.app.ActivityThread.access$600(ActivityThread.java:156) AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250) AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:99) AndroidRuntime at android.os.Looper.loop(Looper.java:137) AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5229) AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method) AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:525) AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) AndroidRuntime at dalvik.system.NativeStart.main(Native Method) AndroidRuntime Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 

所以我的问题==>我可以使用AlertDialog并显示对话框时出现的键盘?

重写onActivityCreated在你的dialogfragment并把getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 在那里

tyczj的答案不适合我。

解决方法是,在onCreateDialog

 Dialog d = builder.create(); d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); return d; 

最后,代码会是这样的

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // Inflate and set the layout for the dialog // Pass null as the parent view because its going in the dialog layout builder .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { callback.onYesConnectClick(edit.getText().toString()); } }) .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { callback.onNoConnectClick(); } }); Dialog d = builder.create(); d.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); return d; } 

onActivityCreatedonCreateDialog方法中使用"SOFT_INPUT_STATE_ALWAYS_VISIBLE"而不是"SOFT_INPUT_STATE_VISIBLE"

注意setLayout()调用,如果你使用它。 我花了一段时间才意识到它可能会覆盖你的窗口的属性。 包装成处理程序后,接受的解决scheme为我工作。

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); return dialog; } @Override public void onStart() { super.onStart(); // without a handler, the window sizes itself correctly // but the keyboard does not show up new Handler().post(new Runnable() { @Override public void run() { getDialog().getWindow().setLayout(DIALOG_WIDTH, DIALOG_HEIGHT); } }); }