The page layouts with input features in Android are often masked by pop-up keyboards, one of which is to listen to the keyboard pop-up, set the layout's padding, or hide some placeholder controls so that the input box is not obscured by the keyboard. A common method is when the activity is set to Android:windowsoftinputmode= "Adjustresize", the keyboard pop-up and hide will trigger the Onsizechaged event, Use the following custom realativelayout to listen to the function and determine if the keyboard pops up or is closed.
/** @Project: GZJK * @Author: CMCC * @Date: May 6, 2015 * @Copyright: 2000-2015 CMCC. All rights reserved. */ PackageCom.matrix.appsdk.widget.keyboard;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.util.AttributeSet;ImportAndroid.view.Display;Importandroid.widget.RelativeLayout;/*** @ClassName: Inputmethodrelativelayout * @Description: Monitor the reality and hide of keyboard, activity set to adjustresize, not full screen *@author* @date May 6, 2015 PM 6:48:57*/ Public classInputmethodrelativelayoutextendsRelativelayout {Private intwidth; protectedOnsizechangedlistenner Onsizechangedlistenner; Private BooleanSizeChanged =false;//Signs of Change Private intheight; Private intScreenHeight;//Screen Height Publicinputmethodrelativelayout (Context paramcontext, AttributeSet paramattributeset) {Super(Paramcontext, Paramattributeset); Display Localdisplay=(Activity) paramcontext). Getwindowmanager (). Getdefaultdisplay (); This. ScreenHeight =localdisplay.getheight (); } PublicInputmethodrelativelayout (Context paramcontext, AttributeSet Paramattributeset,intparamint) { Super(Paramcontext, Paramattributeset, Paramint); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { This. width =Widthmeasurespec; This. Height =Heightmeasurespec; Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); } @Override Public voidOnsizechanged (intWintHintOLDW,intOLDH) { //The listener is not empty, the width is constant, the current height and the historical height are not 0 if(( This. onsizechangedlistenner! =NULL) && (w = = OLDW) && (OLDW! = 0) && (OLDH! = 0)) { if(H >= oldh) | | (Math.Abs (H-OLDH) <= 1 * This. SCREENHEIGHT/4)) { if(H <= oldh) | | (Math.Abs (H-OLDH) <= 1 * This. SCREENHEIGHT/4)) return; This. sizeChanged =false; } Else { This. sizeChanged =true; } This. Onsizechangedlistenner.onsizechange ( This. sizeChanged, OLDH, h); Measure ( This. Width-w + getwidth (), This. Height-h +getheight ()); } } /*** Set Listener Events *@paramParamonsizechangedlistenner*/ Public voidSetonsizechangedlistenner (Inputmethodrelativelayout.onsizechangedlistenner paramonsizechangedlistenner) { This. Onsizechangedlistenner =Paramonsizechangedlistenner; } /*** Internal interface for size change *@authorJunjun **/ Public InterfaceOnsizechangedlistenner {voidOnsizechange (BooleanShowkeyboard,intOLDH,intNEWH); }}View Code
In this way, activity using this custom layout only needs to be handled in the callback Onsizechange.
However, when activity is set to full screen, even if the activity property is set to Adjustresize, this method will not be triggered when the keyboard is displayed or hidden, so you can listen for changes in the root view. Use the following sentence after the activity has finished setting up layout:
Androidbug5497workaround.assistactivity (this);
The code for Androidbug5497workaround is as follows:
PackageCom.matrix.appsdk.widget.keyboard;Importandroid.app.Activity;ImportAndroid.graphics.Rect;ImportAndroid.view.View;ImportAndroid.view.ViewTreeObserver;Importandroid.widget.FrameLayout;/** * @authorBMR * @ClassName: Androidbug5497workaround * @Description: Resolves activity to full screen when set to Adjustresize, Inputmethodrelativelayout detection keyboard does not trigger onsizechanged * @date 2016/1/9 21:09*///workaround to get adjustresize functionality for input methos when the fullscreen mode are on//found by Ricardo//taken fromhttp://stackoverflow.com/a/19494006 Public classAndroidbug5497workaround {//For more information, seehttps://code.google.com/p/android/issues/detail?id=5497 //to use the This class, the simply Invoke Assistactivity () on the Activity that already have its content view set. Public Static voidassistactivity (activity activity) {NewAndroidbug5497workaround (activity); } PrivateView mchildofcontent; Private intusableheightprevious; PrivateFramelayout.layoutparams Framelayoutparams; PrivateAndroidbug5497workaround (activity activity) {Framelayout content=(framelayout) Activity.findviewbyid (Android. R.id.content); Mchildofcontent= Content.getchildat (0); Mchildofcontent.getviewtreeobserver (). Addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () { Public voidongloballayout () {possiblyresizechildofcontent (); } }); Framelayoutparams=(Framelayout.layoutparams) mchildofcontent.getlayoutparams (); } Private voidpossiblyresizechildofcontent () {intUsableheightnow =computeusableheight (); if(Usableheightnow! =usableheightprevious) { intUsableheightsanskeyboard =Mchildofcontent.getrootview (). GetHeight (); intHeightdifference = Usableheightsanskeyboard-Usableheightnow; if(Heightdifference > (USABLEHEIGHTSANSKEYBOARD/4)) { //keyboard probably just became visibleFramelayoutparams.height = Usableheightsanskeyboard-heightdifference; } Else { //keyboard probably just became hiddenFramelayoutparams.height =Usableheightsanskeyboard; } mchildofcontent.requestlayout (); Usableheightprevious=Usableheightnow; } } Private intcomputeusableheight () {Rect R=NewRect (); Mchildofcontent.getwindowvisibledisplayframe (R); return(R.bottom-r.top); }}View Code
This way, you can use the Start method, and Onsizechange can handle keyboard events.
Reference Documentation:
The layout calculation problem of Android soft keyboard in fullscreen
Research on the display and hiding problem of Android soft keyboard
Android Monitor Keyboard