android DialogFragment和强制显示键盘

mefy6pfw  于 2023-06-04  发布在  Android
关注(0)|答案(6)|浏览(254)

我的DialogFragment有问题。因此,为了创建我的视图,我使用了android博客上描述的方法。这是我的对话框片段

@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并在对话框出现时显示键盘吗?

rqmkfv5c

rqmkfv5c1#

覆盖对话框片段中的onActivityCreated,并将getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);放入其中

mwg9r5ms

mwg9r5ms2#

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;
}
ewm0tg9j

ewm0tg9j3#

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

wi3ka0sx

wi3ka0sx4#

如果使用setLayout()调用,请注意它。我花了一段时间才意识到它可能会覆盖窗口的属性。在将其 Package 成处理程序后,接受的解决方案对我有效。

@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);
        }
    });
}
ruoxqz4g

ruoxqz4g5#

如果你想在短暂的延迟后显示键盘,你应该使用另一种方法。使用onActivityCreatedonCreateDialog的解决方案工作正常,但用于即时键盘显示。如果你有EditText,你应该在它上面打开键盘,而不是在一个对话框上。图片来源:https://stackoverflow.com/a/65007148/2914140

fun showKeyboard(view: EditText) {
    val imm = view.context.getSystemService(
        Context.INPUT_METHOD_SERVICE) as InputMethodManager?
    imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    super.onCreateDialog(savedInstanceState)

    val view = activity?.layoutInflater?.inflate(R.layout.your_layout, null)

    view?.edit_text?.run {
        requestFocus() // Required for showing a keyboard.
        setText("Some text")
        setSelection(text.length)
    }

    val dialogBuilder = MaterialAlertDialogBuilder(requireContext()).apply {
        setView(view)
        // Set message, buttons.
        setCancelable(false)
    }
    val dialog = dialogBuilder.create()

    Handler(Looper.getMainLooper()).postDelayed({
        view?.edit_text?.let { showKeyboard(it) }
    }, 100)
//    Instead of Handler you can use coroutines:
//    lifecycleScope.launch {
//        delay(100)
//        view?.edit_text?.let { showKeyboard(it) }
//    }

    return dialog
}
ruoxqz4g

ruoxqz4g6#

出于某种原因,沿着onCreateView中的myEditText.requestFocus()之外,没有任何东西对我有用:

override fun onStart() {
    dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
    super.onStart()
}

相关问题