简体   繁体   中英

How to keep the softkeyboard hidden in a custom alertdialog?

I have been trying to find a solution that works for my specific use case, but I have yet to find the proper setup. I have a custom AlertDialog with an EditText . I am handling the text input without the system keyboard, so having it pop up when I show the dialog on screen is an issue.

I have already looked at a number of different solutions, but none of them have worked for me. This Stack Overflow question as well as this one , this Medium article , and this tutorial .

public void showAlert() {
    alert.show();
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);

    // using this view to hide the keyboard causes a NPE
    View view = ((AppCompatActivity) context).getCurrentFocus();

    // I have also passed in a 0 for this one.
    imm.hideSoftInputFromWindow(ttsAlert.getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

    // This is the editText in the view
    imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

    // This is the rootView of the custom layout
    imm.hideSoftInputFromWindow(rootView.getWindowToken(), 0);

    editText.setShowSoftInputOnFocus(false);
}

If anyone can determine what small detail (or large, obvious code block) I have missed to keep the keyboard from showing, I would deeply appreciate it.

After much more digging, I found the solution I needed for my use case. The keyboard does not appear, but the cursor stays.

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

The method is now simply this:

public void showAlert() {
    alert.show();
    alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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