在 AlertDialog 中隐藏键盘

发布于 2024-12-02 05:58:16 字数 1893 浏览 2 评论 0原文

我有一个带有编辑文本的警报对话框。对于此 Edittext,我让键盘出现,并且我希望当用户按确定或取消时隐藏键盘。奇怪的问题是,当用户选择“确定”时,键盘会隐藏,但是当选择“取消”时,键盘不会隐藏,我在这两种情况下使用相同的代码。

这是我的代码:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle(data);
        final EditText input = new EditText(this);
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(25);
        input.setFilters(FilterArray);
        input.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager keyboard = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(input, 0); 
            }
        },200);



        alert.setView(input);

        alert.setPositiveButton(ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                text = input.getText().toString().trim();
                Canvas c = new Canvas(bitmapResult);
                drawTextImage(bitmapResult);
                saveimage();
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
            }
        });

        alert.setNegativeButton(cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                        saveimage();
                        InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        im.hideSoftInputFromWindow(input.getWindowToken(), 0);
                    }
                });

        alert.show();

我的赌注在哪里?谁能帮助我吗?

I have an alertdialog with an editext. For this Edittext I make keyboard appear and I want that when user press ok or cancel to hide the keyboard. The strange problem is that when user choose ok, the keyboard is hide, but when choose cancel the keyboard doesn't hide an I'm using the same code for both cases.

Here is my code :

final AlertDialog.Builder alert = new AlertDialog.Builder(this);

        alert.setTitle(data);
        final EditText input = new EditText(this);
        InputFilter[] FilterArray = new InputFilter[1];
        FilterArray[0] = new InputFilter.LengthFilter(25);
        input.setFilters(FilterArray);
        input.postDelayed(new Runnable() {
            @Override
            public void run() {
                InputMethodManager keyboard = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(input, 0); 
            }
        },200);



        alert.setView(input);

        alert.setPositiveButton(ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                text = input.getText().toString().trim();
                Canvas c = new Canvas(bitmapResult);
                drawTextImage(bitmapResult);
                saveimage();
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(input.getWindowToken(), 0);
            }
        });

        alert.setNegativeButton(cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        dialog.cancel();
                        saveimage();
                        InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                        im.hideSoftInputFromWindow(input.getWindowToken(), 0);
                    }
                });

        alert.show();

where is my mystake? Can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

坦然微笑 2024-12-09 05:58:16

我找到了解决方案:

alert.setNegativeButton(cancel,
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            saveimage();
            InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(input.getWindowToken(), 0);
            dialog.cancel();
        }
    });

我应该在隐藏键盘后放置dialog.cancel()。

KOTLIN 更新:

val im: InputMethodManager =
    getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
im.hideSoftInputFromWindow(editText.windowToken, 0)

I found the solution :

alert.setNegativeButton(cancel,
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            saveimage();
            InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(input.getWindowToken(), 0);
            dialog.cancel();
        }
    });

I should've put dialog.cancel() after I hide the keyboard.

UPDATE IN KOTLIN:

val im: InputMethodManager =
    getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
im.hideSoftInputFromWindow(editText.windowToken, 0)
一曲琵琶半遮面シ 2024-12-09 05:58:16

我也一直在努力解决这个问题,并且对几乎所有发布的“解决方案”都敲了敲头,但该死的键盘仍然无法关闭。然后我有了一个充满咖啡因的愿景:

            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
        }

请注意 HIDE_IMPLICIT_ONLY

希望可以帮助其他遇到此问题的人。

I too was struggling with this and bonked my head on just about every "solution" which was posted yet the damn keyboard would still not close. Then I had a caffenated vision:

            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(dialog.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
        }

Note the HIDE_IMPLICIT_ONLY

hope that helps anyone else struggling with this problem.

我是有多爱你 2024-12-09 05:58:16

在使用 dialog.cancel(); 之前使用以下方法

public static void hideSoftKeyboardUsingView(Context context,View view) { 

    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

}

Use following method before you use dialog.cancel();

public static void hideSoftKeyboardUsingView(Context context,View view) { 

    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

}
入怼 2024-12-09 05:58:16

就我而言,我希望仅在显示对话框时打开键盘
我尝试了很多解决方案,但最终我通过添加清单文件的内部标签成功实现了

 android:windowSoftInputMode="stateAlwaysHidden"

In my case i wanted keyboard to be open only when the dialog shown
i have tried many solutions but finally i have succeeded to achieve by adding

 android:windowSoftInputMode="stateAlwaysHidden"

inside tag of manifiest file.

往日 2024-12-09 05:58:16

不确定,但您可以尝试添加此内容:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

我用它来避免在我的应用程序启动时首次显示键盘...当我单击该字段时,键盘仍然打开...

所以,也许,它可以与您的代码一起使用:

keyboard.showSoftInput(input, 0); 

然后自动关闭它...

Not sure, but you can try with adding this:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

I'm using it to avoid the first display of the keyboard when my app starts... when I click in the field, the keyboard is still opened...

So, maybe, it could work with your code:

keyboard.showSoftInput(input, 0); 

and then automatically close it...

大海や 2024-12-09 05:58:16

尝试了以上所有方法,但最后这对我有用......在失去了一些头发之后 -_-

mDialogView.btn.setOnClickListener {
                
                mAlertDialog.dismiss()
                val imm = [email protected](Context.INPUT_METHOD_SERVICE) as 
 InputMethodManager
                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
            }

Tried all of the above but finally this works for me... after a few lost hair -_-

mDialogView.btn.setOnClickListener {
                
                mAlertDialog.dismiss()
                val imm = [email protected](Context.INPUT_METHOD_SERVICE) as 
 InputMethodManager
                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文