The Android system does not provide obvious APIs to listen for and close the soft keyboard. However, in some cases, we still have a way to detect and close the soft keyboard.
I found a good method from stackoverflow. However, this method is only applicable when Android: windowsoftinputmode = "adjustresize" is set for the target activity in manifest.
Adjustresize indicates the activity's main window is always resized to make room for the soft keyboard on screen.
Then we need to set the ID of the Root View of the activity to determine whether to close the dialog box.
Layout sample code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/root_layout" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:padding="@dimen/padding_medium" android:text="@string/hello_world" tools:context=".MainActivity" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> </RelativeLayout>
The logic code for the judgment dialog box to appear or close is as follows:
@ Override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); final view activityrootview = findviewbyid (R. id. root_layout); activityrootview. getviewtreeobserver (). addongloballayoutlistener (New ongloballayoutlistener () {private int preheight = 0; @ overridepublic void ongloballayout () {int heightdiff = activityrootview. getrootv Iew (). getheight ()-activityrootview. getheight (); system. out. println ("height differ =" + heightdiff); // reduce the number of duplicate messages sent when data is consistent. Because the ongloballayout method is called multiple times when the input method appears. If (preheight = heightdiff) {return;} preheight = heightdiff; If (heightdiff> 100) {// system. Out. println ("input method shown! "); Toast. maketext (getapplicationcontext (), "keyboard is shown", toast. length_short ). show ();} else {toast. maketext (getapplicationcontext (), "keyboard is hidden", toast. length_short ). show ();}}});}
Give your activity's Root View a known ID, say '@ + ID/activityroot', hook a globallayoutlistener into the viewtreeobserver,
And from there calculate the size diff between your activity's view root and the window size
Set an ID for the root view of your activity, for example, "@ + ID/activityroot". Set a globallayoutlistener on viewtreeobserver. Then calculate the difference between the Root View and window size of your activity
If the interpolation value is greater than 100 (this is a relatively reasonable value), it is considered as a dialog box pop-up. Otherwise, it is hidden in the dialog box.
Link: http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android