1

我的 AlertDialog 弹出窗口中有一个 EditBox。我让它在 AlertDialog 弹出的那一刻弹出虚拟键盘(这样我就不需要单击那个白色字段来显示键盘),这样:

InputMethodManager imm = (InputMethodManager)
            Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);

            if (imm != null){
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
            }

但是当我完成输入后,我需要在键盘上单击“完成”,然后在 AlertDialog 中单击“确定”。问题是,用户在完成输入后直接进入“确定”按钮,单击“确定”后,虚拟键盘仍停留在屏幕上。他们现在必须单击设备上的后退按钮。按下确定按钮后如何清除键盘?

这是我的整个 AlertDialog 代码,如果有帮助的话:

case R.id.bKonacno:

                        }

                LayoutInflater layoutInflaterK = LayoutInflater.from(context);  
                View promptViewK = layoutInflaterK.inflate(R.layout.popup_answer, null);
                AlertDialog.Builder alertDialogBuilderK = new AlertDialog.Builder(context);
                // set prompts.xml to be the layout file of the alertdialog builder
                alertDialogBuilderK.setView(promptViewK);
                final EditText inputK = (EditText)promptViewK.findViewById(R.id.userInput);

                InputMethodManager imm = (InputMethodManager)
                Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);

                if (imm != null){
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
                }

                alertDialogBuilderK
                                        .setCancelable(false)
                                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog, int id) {
                                                        // get user input and set it to result

                                                        //some code of mine
                                                    }
                                                })
                                        .setNegativeButton("Cancel",
                                                new DialogInterface.OnClickListener() {
                                                    public void onClick(DialogInterface dialog, int id) {
                                                        dialog.cancel();
                                                    }
                                               });
                                // create an alert dialog

                                AlertDialog alertK = alertDialogBuilderK.create();
                                alertK.show();

                                break;

在此处输入图像描述

4

2 回答 2

1

Ok按下/Cancel按钮时,您可以执行以下操作。

InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

阅读关闭/隐藏 Android 软键盘 了解更多信息。希望能帮助到你。

于 2013-06-13T19:09:06.360 回答
0

尝试将以下内容添加到您的按钮中:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
于 2013-06-13T19:09:35.780 回答