简体   繁体   中英

Android Hide Soft-Keyboard on Dialog with EditText

I have a Custom Dialog Box that contains an EditText. Now, Whenever I show the Dialog using Dialog.show(); , the EditText immediately grabs focus and displays the Soft-Keyboard. I attempted to add this to the manifest:

android:windowSoftInputMode="stateHidden"

Based on this answer: https://stackoverflow.com/a/2611031/3011902

I also tried the following on the EditText:

EditText.setSelected(false);

And:

LinearLayout hidden = (LinearLayout) loginDialog.findViewById(R.id.hidden);
hidden.setVisibility(View.INVISIBLE);
hidden.setFocusable(true);
hidden.requestFocus();
loginDialog.show();

I also tried to manually hide the keyboard right after the dialog is shown, but that feels a bit illegitimate. Is there any easy way to only display the keyboard when the EditText of the Dialog is selected.

You can try redirecting the focus to another view or just invisible view inside your Custom Dialog Box with adding
android:focusable="true" and android:focusableInTouchMode="true" or
setFocusable(true) and setFocusableInTouchMode(true)

If you have any question about my answer feel free to ask in the comment!

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