【问题标题】:AlertDialog with EditText, open soft keyboard automatically with focus on EditText doesn't work带有 EditText 的 AlertDialog,以 EditText 为焦点自动打开软键盘不起作用
【发布时间】:2012-10-11 10:15:00
【问题描述】:

我正在尝试进行一段代码工作,它应该在显示时将 EditText 聚焦在 AlertDialog 中,然后自动打开软键盘。相反,它只会让屏幕变暗。

Builder builder = new Builder(this);
final EditText input = new EditText(this);
AlertDialog dialog = builder.create();
builder
    .setTitle(R.string.dialog_title_addsubject)
    .setMessage(R.string.dialog_addsubject)
    .setView(input)
    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            String value = input.getText().toString();
            if (input.getText().toString().trim().length() == 0) {
                Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
            } else {
                db.insertSubject(value);
                getData();
            }
         }
    })
    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        }
    });
    input.requestFocus();
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    dialog.show();

我尝试了很多方法,但都没有奏效。我希望你们能在这里帮助我。提前致谢!

【问题讨论】:

    标签: android focus android-edittext android-softkeyboard android-alertdialog


    【解决方案1】:

    你可以用这个代替 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);:

    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    dialog.show();之后调用这个

    【讨论】:

    • 我的回答对您有用吗?
    • 好的,但我可以在 textView 上显示光标。有什么想法吗?
    • 实际上,它工作得很好,但使用 dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);在 dialog.show() 之前调用
    • 我的 AlertDialog 在一个单独的 DialogFragment 类中。使用InputMethodManager 方法适用于许多设备,但不是全部。使用alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 让它对我来说一切正常。
    • 这解决了我的问题,我只需要第一行代码。
    【解决方案2】:

    好的,我设法让它工作了:

    Builder builder = new Builder(this);
                final EditText input = new EditText(this);
                builder
                    .setTitle(R.string.dialog_title_addsubject)
                    .setMessage(R.string.dialog_addsubject)
                    .setView(input)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    
                        public void onClick(DialogInterface dialog, int which) {
                            String value = input.getText().toString();
                            if (input.getText().toString().trim().length() == 0) {
                                Toast.makeText(Main.this, R.string.input_empty, Toast.LENGTH_SHORT).show();
                            } else {
                                db.insertSubject(value);
                                getData();
                            }
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
    
                        public void onClick(DialogInterface dialog, int which) {
                            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
                        }
    
                    });
    
                    builder.show();
                    input.requestFocus();
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    此方法不需要对话框,因此我可以使用 builder.show() 显示对话框,Sabre 提供的代码打开软键盘。每个按钮中的另一个代码 sn -p 自动关闭软键盘。

    【讨论】:

    • 太棒了!!!一直试图让键盘在警报对话框中显示我的编辑文本,但没有任何效果。非常感谢
    • 谢谢。它对我有帮助。
    • 这行得通,但请记住,您必须使用 builder.setView(),因为如果您使用 dialog.setContentView(),键盘将出现在对话框下方并且不会捕获输入。
    • 它可以工作,但是如果用户没有按正或负按钮,而是点击空白区域,键盘在对话关闭后保持打开状态
    【解决方案3】:

    请稍后再显示 -

    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        input.requestFocus();
    
        dialog.getWindow().
      setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
    
        dialog.show();  
    }, 1000)
    

    【讨论】:

      【解决方案4】:
         public void selectContact(Context context) {
              AlertDialog.Builder builder = new AlertDialog.Builder(context);
              builder.setIcon(R.mipmap.icon);
              builder.setTitle(R.string.title);
              builder.setPositiveButton(android.R.string.ok, context);
              builder.setNegativeButton(android.R.string.cancel,context);
              builder.setView(View.inflate(context,
                      R.layout.dialog, null));
              AlertDialog alertDialog = builder.create();
      
              alertDialog.setOnShowListener(this); //Add listener
              alertDialog.show();
          }
      

      在 onShow 中打开键盘:-

          @Override
          public void onShow(DialogInterface dialog) {
              EditText editText = (EditText) ((AlertDialog) dialog).findViewById(R.id.number);
              InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
          }
      

      【讨论】:

        【解决方案5】:

        这对我有用。请求关注编辑文本,然后在其上发布延迟任务以显示键盘。

        editTextView.requestFocus()
        editTextView.postDelayed(200) {
            ContextCompat.getSystemService(requireContext(), InputMethodManager::class.java)
                ?.showSoftInput(
                    editTextView,
                    InputMethodManager.SHOW_IMPLICIT,
                )
        }
        

        使用SHOW_FORCED 将保留键盘直到用户手动关闭它,即使他们已经关闭了对话框,所以使用SHOW_IMPLICIT

        【讨论】:

          【解决方案6】:

          在尝试了不同的建议后,最终清除标志并在窗口上设置软输入模式有效。确保在 dialog.show() 之后调用它:

          dialog.window?.apply {
              clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
              clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
              setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-03-04
            • 2016-02-18
            • 2012-07-04
            • 2020-10-04
            • 1970-01-01
            • 2011-11-09
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多