DialogFragment并强制显示键盘 [英] DialogFragment and force to show keyboard
问题描述
我的DialogFragment有问题.因此,为了创建我的视图,我使用了android博客上描述的方法. 这是我的DialogFragment
I have a problem with my DialogFragment. So to create my view, I use the method described on the android blog. Here is my 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(),它可以工作,但是我想创建一个AlterDialog并执行以下操作,我有以下代码:
If I use onCreateView(), it works but I would like create an AlterDialog and to do this, I have the following code :
@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(),则会导致崩溃. 这是堆栈跟踪:
If I comment the code from onCreateView(), the app works but I can't force the keyboard to be shown and if I uncomment onCreateView(), I get a crash. Here is the stack trace :
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并在出现对话框时显示键盘吗?
So my question ==> Can I use the AlertDialog and show the keyboard when the dialog appears ?
推荐答案
在对话框片段中覆盖onActivityCreated
并在其中放置getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
override onActivityCreated
in your dialogfragment and put getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
in there
这篇关于DialogFragment并强制显示键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!