Background:
In many app development processes need to listen to the Android device in the activity of the soft keyboard bounce and close, but Android does not seem to provide the relevant listening API to call us, this article provides a practical way to listen to the soft keyboard bounce and close.
Pre-Knowledge:
The Android:windowsoftinputmode property of the activity can be set in the manifest file, and the common settings for this property value are as follows:
Android:windowsoftinputmode= "Statealwayshidden|adjustpan"
So here's a list of the meanings of the values below:
"A" stateunspecified: the state of the soft keyboard is not specified and the system will select an appropriate state or a theme-dependent setting
"B" stateunchanged: When this activity appears, the soft keyboard will remain in the previous activity, whether it is hidden or displayed
"C" Statehidden: When the user chooses activity, the soft keyboard is always hidden
"D" Statealwayshidden: When the Activity main window gets the focus, the soft keyboard is always hidden
"E" statevisible: Soft keyboard is usually visible
"F" statealwaysvisible: The state that the soft keyboard always displays when the user chooses activity
"G" adjustunspecified: The default setting, which is usually determined by the system to hide or show itself
"H" adjustresize: The activity always adjusts the size of the screen to allow space for the soft keyboard
"I" Adjustpan: The contents of the current window will automatically move so that the current focus is never covered by the keyboard and the user can always see the part of the input content
Example:
(1) First we need to set the activity of the listener in the manifest file to the following form:
<activity android:name= "com.bear.softkeyboardlistener.MainActivity" android:label= "@string/app_name " android:windowsoftinputmode=" statealwayshidden|adjustresize "> <intent-filter> < Action android:name= "Android.intent.action.MAIN"/> <category android:name= " Android.intent.category.LAUNCHER "/> </intent-filter> </activity>
When this is set, the activity's layout size will be compressed when a soft keyboard is bouncing, but you can still swipe through all of them.
(2) We need to set up a Onlayoutchangelistener listener for the outermost layout of the activity:
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >import Com.bear.bearbroadcastreceiver.r;import Android.app.activity;import Android.os.Bundle;import Android.view.view;import Android.view.view.onlayoutchangelistener;import Android.widget.toast;public Class Mainactivity extends Activity implements onlayoutchangelistener{//activity outermost layout view Private view Activityrootview ;//Screen height private int screenheight = 0;//the height of the software disk after the threshold value of the valve private int keyheight = 0; @Overrideprotected void OnCreate (Bundle saved Instancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ActivityRootView = Findviewbyid (r.id.root_layout);//Get screen height screenheight = This.getwindowmanager (). Getdefaultdisplay (). GetHeight ();// The threshold is set to the screen height of 1/3keyheight = SCREENHEIGHT/3;} @Overrideprotected void Onresume () {super.onresume ();// Add layout size Change listener activityrootview.addonlayoutchangelistener (this);} @Overridepublic void Onlayoutchange (View v, int left, int top, int right,int bottom, int. oldleft, int oldtop, inT oldright, int oldbottom) {//old is changed before the left upper right bottom of the punctuation value, no old is changed after the left upper right bottom sitting punctuation value//system.out.println (OldLeft + "" + OldTop + "" + Oldri Ght + "" + Oldbottom)//system.out.println (left + "" + Top + "+ right +" + bottom);//Now think as long as the control pushes activity up more than 1/3 screens Screen height, just think of the soft keyboard bouncing up if (oldbottom! = 0 && Bottom! = 0 && (Oldbottom-bottom > Keyheight)) {Toast.maketext (mainact Ivity.this, "Listening to the soft keyboard bouncing ...", toast.length_short). Show (); else if (oldbottom! = 0 && Bottom! = 0 && (Bottom-oldbottom > Keyheight)) {Toast.maketext (mainactivity.t His, "Hear the software disk close ...", Toast.length_short). Show ();}} </span>
here is the entire demo source download link:
Android SoftKeyboard Listener Demo
Android Monitor phone soft keyboard bounce up and close