Recently in the application performance tuning, found in an activity containing the input box, when the soft keyboard pops up, if directly finish off the activity, then return to the previous activity, the interface rendering will be due to the soft keyboard does not have a timely collection of the situation.
Very unfriendly.
So, in the spirit of geek, do it to the extreme, try to optimize this piece.
The use of online knowledge sharing, combined with their own understanding, and ultimately applied to the project.
Directly on the code:
First, declare the Windowsoftinputmode property of this activity in the manifest file,
1 android:windowsoftinputmode= "Statevisible|adjustresize"
Declare the variables required and the event listener for the layout in the activity,
1 Private BooleanMbackenable =false;2 Private BooleanMisbtnback =false;3 Private intRootbottom =Integer.min_value;4 PrivateOngloballayoutlistener Mongloballayoutlistener =NewOngloballayoutlistener () {5 @Override6 Public voidongloballayout () {7Rect r =NewRect ();8 Msearchlayout.getglobalvisiblerect (r);9 //Enter activity will layout, first call Ongloballayout, first record start soft keyboard does not eject when the bottom of the positionTen if(Rootbottom = =integer.min_value) { OneRootbottom =R.bottom; A return; - } - //adjustresize, when the soft keyboard pops up, the height becomes smaller. the if(R.bottom <Rootbottom) { -Mbackenable =false; -}Else { -Mbackenable =true; + if(misbtnback) { - finish (); + } A } at } -};
Then register the event listener in the Oncreat method.
1 msearchlayout.getviewtreeobserver (). Addongloballayoutlistener (Mongloballayoutlistener);
Note: This msearchlayout is the layout reference for your entire activity
Finally, don't forget to recall the interception of events in the OnDestroy method
1 @TargetApi (build.version_codes. Jelly_bean)2 @Override3 protected voidOnDestroy () {4 if(Build.VERSION.SDK_INT <=Build.version_codes. Jelly_bean) {5 msearchlayout.getviewtreeobserver (). Removeglobalonlayoutlistener (Mongloballayoutlistener);6}Else {7 msearchlayout.getviewtreeobserver (). Removeongloballayoutlistener (Mongloballayoutlistener);8 }9 Super. OnDestroy ();Ten}
Here, the Android soft keyboard monitoring is over.
* * what? You say full screen time, no effect??? **
Well, I also met ...
I implemented all the interface in the project immersive effect, I thought the activity set Adjustresize,ongloballayoutlistener still can listen to the changes in the soft keyboard, the results are not repeated test ...
Helpless, can only ask for help. Looked up some information, found that this was originally a bug in Android. The following is a specific bug fix code snippet
1 Public classAndroidadjustresizebugfix {2 PrivateView mchildofcontent;3 Private intusableheightprevious;4 Private intStatusbarheight;5 Privateframelayout.layoutparams Framelayoutparams;6 PrivateActivity mactivity;7 8 PrivateAndroidadjustresizebugfix (activity activity) {9Mactivity =activity;TenFramelayout content =(framelayout) Activity.findviewbyid (Android. r.id.content); Onemchildofcontent = Content.getchildat (0); AStatusbarheight =getstatusbarheight (); -Mchildofcontent.getviewtreeobserver (). Addongloballayoutlistener (NewViewtreeobserver.ongloballayoutlistener () { - Public voidongloballayout () { the possiblyresizechildofcontent (); - } - }); -Framelayoutparams =(Framelayout.layoutparams) mchildofcontent.getlayoutparams (); + } - Public Static voidassistactivity (activity activity) { + NewAndroidadjustresizebugfix (activity); A } at Private voidpossiblyresizechildofcontent () { - intUsableheightnow =computeusableheight (); - if(Usableheightnow! =usableheightprevious) { - intUsableheightsanskeyboard =Mchildofcontent.getrootview (). GetHeight (); - intHeightdifference = Usableheightsanskeyboard-Usableheightnow; - if(Heightdifference > (USABLEHEIGHTSANSKEYBOARD/4)) { in //keyboard probably just became visible - //If there is a height change, the mchildofcontent.requestlayout () will not be re-measured after the interface to //let's just subtract 1 from the original height here. +Framelayoutparams.height = usableHeightSansKeyboard-1; -}Else { the //keyboard probably just became hidden *Framelayoutparams.height =Usableheightsanskeyboard; $ }Panax Notoginseng mchildofcontent.requestlayout (); -Usableheightprevious =Usableheightnow; the } + } A Private intcomputeusableheight () { theRect r =NewRect (); + Mchildofcontent.getwindowvisibledisplayframe (r); - returnR.bottom-r.top +Statusbarheight; $ } $ Private intgetstatusbarheight () { - Try { -class<?> C = class.forname ("Com.android.internal.r$dimen"); theObject obj =c.newinstance (); -Field field = C.getfield ("Status_bar_height");Wuyi intx =integer.parseint (Field.get (obj). toString ()); the intDimensionpixelsize =mactivity.getresources (). getdimensionpixelsize (x); - returndimensionpixelsize; Wu}Catch(Exception e) { - e.printstacktrace (); About } $ return0; - } -}
Add this class as a tool class to the project, and then register the activity in the OnCreate method of the activity that needs to be optimized, such as:
1 androidadjustresizebugfix.assistactivity (this);
The above is about listening to the soft keyboard pop-up and the contents of the collection.
Original is not easy, I hope you support more! If you want to reprint, please indicate the source! http://www.cnblogs.com/shelly-li/
Perfect solution for Android soft keyboard monitoring