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:
[HTML]View PlainCopy
- <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:
[Java]View Plain copy
- 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 threshold after the software disk bounces
- private int keyheight = 0;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Activityrootview = Findviewbyid (r.id.root_layout);
- //Get screen height
- ScreenHeight = This.getwindowmanager (). Getdefaultdisplay (). GetHeight ();
- //threshold set to 1/3 of screen height
- Keyheight = screenheight/3;
- }
- @Override
- protected void Onresume () {
- Super.onresume ();
- //Add layout size change listener
- Activityrootview.addonlayoutchangelistener (this);
- }
- @Override
- public 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 sitting punctuation value, no old is changed after the left upper right bottom sitting punctuation value
- System.out.println (oldleft + "+ oldtop +" "+ OldRight +" "+ Oldbottom);
- System.out.println (left + "+ top +" "+ Right +" "+ bottom);
- //Now think that the soft keyboard bounces as long as the control pushes the activity up higher than the 1/3 screen height
- if (oldbottom! = 0 && Bottom! = 0 && (Oldbottom-bottom > Keyheight)) {
- Toast.maketext (mainactivity. This, "listen to the soft keyboard bouncing ...", toast.length_short). Show ();
- }Else if (oldbottom! = 0 && Bottom! = 0 && (Bottom-oldbottom > Keyheight)) {
- Toast.maketext (mainactivity. This, "hear the software disk close ...", Toast.length_short). Show ();
- }
- }
Android Monitor phone soft keyboard bounce up and close