gpt4 book ai didi

android - DialogFragment 和强制显示键盘

转载 作者:IT王子 更新时间:2023-10-28 23:57:46 26 4
gpt4 key购买 nike

我的 DialogFragment 有问题。所以为了创建我的 View ,我使用了 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(),它可以工作,但我想创建一个 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 并在对话框出现时显示键盘吗?

最佳答案

在您的对话框 fragment 中覆盖 onActivityCreated 并将 getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 放在那里

关于android - DialogFragment 和强制显示键盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17237952/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com