【问题标题】:DialogFragment and force to show keyboardDialogFragment 和强制显示键盘
【发布时间】:2013-06-18 17:52:43
【问题描述】:

我的 DialogFragment 有问题。因此,为了创建我的视图,我使用了 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 并在对话框出现时显示键盘吗?

【问题讨论】:

    标签: android android-dialogfragment


    【解决方案1】:

    在你的对话片段中覆盖 onActivityCreated 并将 getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 放在那里

    【讨论】:

    • wtf ?!我找了很多时间,解决方案很简单!然而,有一件事我不明白。我试图在清单中设置 android:windowSoftInputMode="stateVisible" 关于负责启动对话框的活动,但它不起作用......
    • 谢谢!! StackOverflow 上有大约十几个其他“解决方案”来解决这个问题,但没有一个对我有用……但这个可以。
    • 这似乎是我能找到的最有效的解决方案。我很好奇为什么必须在 onActivityCreated 中调用它,而不是 onResume()?我真的很喜欢听一些解释。
    • 我尝试了很多东西,这是唯一有效的方法:O
    • 我有一个 DialogFragment,它是一个自定义视图,用于扩展这个库。 github.com/MiguelCatalan/MaterialSearchView。当 MaterialSearchView 从我的 DialogFragment 启动时,我无法让键盘显示。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 这是我最后在 onCreateDialog 中结合 Purgarcita 解决方案的解决方案。
      【解决方案4】:

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

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

      【讨论】:

      • 完美! setLayout() 造成了问题
      【解决方案5】:

      如果您想在短暂延迟后显示键盘,则应使用另一种方法。 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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-09
        • 1970-01-01
        • 2011-12-28
        • 2011-08-29
        相关资源
        最近更新 更多