Android Monitor phone soft keyboard bounce up and close

Source: Internet
Author: User

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
  1. <activity
  2. android:name="com.bear.softkeyboardlistener.MainActivity"
  3. android:label="@string/app_name"
  4. android:windowsoftinputmode="statealwayshidden|adjustresize" >
  5. <intent-filter>
  6. <action android:name="Android.intent.action.MAIN" />
  7. <category android:name="Android.intent.category.LAUNCHER" />
  8. </intent-filter>
  9. </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
    1. Import COM.BEAR.BEARBROADCASTRECEIVER.R;
    2. Import android.app.Activity;
    3. Import Android.os.Bundle;
    4. Import Android.view.View;
    5. Import Android.view.View.OnLayoutChangeListener;
    6. Import Android.widget.Toast;
    7. Public class Mainactivity extends Activity implements onlayoutchangelistener{
    8. //activity Outermost layout view
    9. private View Activityrootview;
    10. //Screen height
    11. private int screenheight = 0;
    12. //The height threshold after the software disk bounces
    13. private int keyheight = 0;
    14. @Override
    15. protected void OnCreate (Bundle savedinstancestate) {
    16. super.oncreate (savedinstancestate);
    17. Setcontentview (R.layout.activity_main);
    18. Activityrootview = Findviewbyid (r.id.root_layout);
    19. //Get screen height
    20. ScreenHeight = This.getwindowmanager (). Getdefaultdisplay (). GetHeight ();
    21. //threshold set to 1/3 of screen height
    22. Keyheight = screenheight/3;
    23. }
    24. @Override
    25. protected void Onresume () {
    26. Super.onresume ();
    27. //Add layout size change listener
    28. Activityrootview.addonlayoutchangelistener (this);
    29. }
    30. @Override
    31. public void Onlayoutchange (View V, int. left, int top, int. Right,
    32. int bottom, int oldleft, int oldtop, int oldright, int oldbottom) {
    33. //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
    34. System.out.println (oldleft + "+ oldtop +" "+ OldRight +" "+ Oldbottom);
    35. System.out.println (left + "+ top +" "+ Right +" "+ bottom);
    36. //Now think that the soft keyboard bounces as long as the control pushes the activity up higher than the 1/3 screen height
    37. if (oldbottom! = 0 && Bottom! = 0 && (Oldbottom-bottom > Keyheight)) {
    38. Toast.maketext (mainactivity.   This, "listen to the soft keyboard bouncing ...", toast.length_short). Show ();
    39. }Else if (oldbottom! = 0 && Bottom! = 0 && (Bottom-oldbottom > Keyheight)) {
    40. Toast.maketext (mainactivity.   This, "hear the software disk close ...", Toast.length_short). Show ();
    41. }
    42. }

Android Monitor phone soft keyboard bounce up and close

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.