Android listening: the mobile phone's soft keyboard pops up and down, android listening

Source: Internet
Author: User

Android listening: the mobile phone's soft keyboard pops up and down, android listening

Background:

In many App development processes, you need to listen to the soft keyboard of the Android device in the Activity, but Android does not seem to provide the relevant listening API for us to call, this article provides a feasible way to listen to the boot and close of the soft keyboard.


Prerequisites:

In the manifest file, you can set the android: windowSoftInputMode attribute of the Activity. The common settings of this attribute value are as follows:

Android: windowSoftInputMode = "stateAlwaysHidden | adjustPan"

The meanings of the values here are as follows:

[A] stateUnspecified: the soft keyboard status is not specified. The system selects an appropriate status or depends on the topic settings.

[B] stateUnchanged: When this activity appears, the soft keyboard will remain in the status of the previous activity, whether hidden or displayed.

[C] stateHidden: When you select activity, the keyboard is always hidden.

[D] stateAlwaysHidden: when the Activity's Main Window gets the focus, the keyboard is always hidden.

[E] stateVisible: the soft keyboard is usually visible.

[F] stateAlwaysVisible: when the activity is selected, the soft keyboard always displays the status

[G] adjustUnspecified: the default setting. It is usually determined by the system whether to hide or display it.

[H] adjustResize: This Activity always adjusts the screen size to reserve space for the soft keyboard.

[I] adjustPan: the content in the current window will be automatically moved so that the current focus is not overwritten by the keyboard and the user can always see the input content.



Example:

(1) first, we need to set the Activity where the listener is located in the Manifest file as follows:

        <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>


After this setting, when a soft keyboard pops up, the layout of the Activity will be compressed, but you can still scroll through all.


(2) We need to set an 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 {// Layout View on the outermost layer of the Activity private View activityRootView; // screen height private int scree NHeight = 0; // private int keyHeight = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); activityRootView = findViewById (R. id. root_layout); // obtain the screen height screenHeight = this. getWindowManager (). getdefadisplay display (). getHeight (); // threshold value is set to 1/3 keyHeight = screenHeight/3;} @ Overrideprotected void onResume () {super. OnResume (); // Adding layout changes the 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 refers to the upper left and lower right coordinate points before the change, and the upper left and lower right coordinate points without old. // System. out. println (oldLeft + "" + oldTop + "" + oldRight + "" + oldBottom); // System. out. println (left + "" + top + "" + right + "" + bot Tom); // now we think that as long as the height of the Activity pushed up by the control exceeds the screen height of 1/3, we think that the soft keyboard pops up if (oldBottom! = 0 & bottom! = 0 & (oldBottom-bottom> keyHeight) {Toast. makeText (MainActivity. this, "Listening to the soft keyboard pops up... ", Toast. LENGTH_SHORT ). show ();} else if (oldBottom! = 0 & bottom! = 0 & (bottom-oldBottom> keyHeight) {Toast. makeText (MainActivity. this, "Listening to the software disk to close... ", Toast. LENGTH_SHORT ). show () ;}</span>


The download link of the entire demo source code is as follows:

Android SoftKeyboard Listener Demo

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.