开发者

Hide keyboard in AlertDialog

开发者 https://www.devze.com 2023-04-01 15:55 出处:网络
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

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 DialogInt开发者_开发百科erface.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?


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)


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.


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.


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...


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

}


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

mDialogView.btn.setOnClickListener {
                
                mAlertDialog.dismiss()
                val imm = this@MainActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as 
 InputMethodManager
                imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消