繁体   English   中英

带有 EditText 的 AlertDialog,自动打开软键盘并专注于 EditText 不起作用

[英]AlertDialog with EditText, open soft keyboard automatically with focus on EditText doesn't work

我正在尝试获得一段代码工作,它应该在显示时立即将 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();

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

好的,我设法让它工作:

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 提供的代码打开软键盘。 每个按钮中的另一个代码片段会自动关闭软键盘。

您可以使用它代替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();之后调用它

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

一秒钟后尝试显示它 -

new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
    input.requestFocus();

    dialog.getWindow().
  setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

    dialog.show();  
}, 1000)

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM