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,
Android:windowsoftinputmode= "Statevisible|adjustresize"
Declare the variables required and the event listener for the layout in the activity,
Private Boolean mbackenable = false;
Private Boolean misbtnback = false;
private int rootbottom = Integer.min_value;
Private Ongloballayoutlistener Mongloballayoutlistener = new Ongloballayoutlistener () {
@Override
public void Ongloballayout () {
Rect r = new Rect ();
Msearchlayout.getglobalvisiblerect (R);
Enter activity will layout, first call Ongloballayout, first record start soft keyboard does not eject when the bottom of the position
if (Rootbottom = = Integer.min_value) {
Rootbottom = R.bottom;
Return
}
Adjustresize, when the soft keyboard pops up, the height becomes smaller.
if (R.bottom < Rootbottom) {
Mbackenable = false;
} else {
Mbackenable = true;
if (misbtnback) {
Finish ();
}
}
}
};
Then register the event listener in the Oncreat method.
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
@TargetApi (build.version_codes. Jelly_bean)
@Override
protected void OnDestroy () {
if (Build.VERSION.SDK_INT <= build.version_codes. Jelly_bean) {
Msearchlayout.getviewtreeobserver (). Removeglobalonlayoutlistener (Mongloballayoutlistener);
} else {
Msearchlayout.getviewtreeobserver (). Removeongloballayoutlistener (Mongloballayoutlistener);
}
Super.ondestroy ();
}
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
public class Androidadjustresizebugfix {
Private View mchildofcontent;
private int usableheightprevious;
private int statusbarheight;
Private Framelayout.layoutparams Framelayoutparams;
Private Activity mactivity;
Private Androidadjustresizebugfix (activity activity) {
Mactivity = activity;
Framelayout content = (framelayout) Activity.findviewbyid (Android. R.id.content);
mchildofcontent = Content.getchildat (0);
Statusbarheight = Getstatusbarheight ();
Mchildofcontent.getviewtreeobserver (). Addongloballayoutlistener (New Viewtreeobserver.ongloballayoutlistener () {
public void Ongloballayout () {
Possiblyresizechildofcontent ();
}
});
Framelayoutparams = (framelayout.layoutparams) mchildofcontent.getlayoutparams ();
}
public static void assistactivity (activity activity) {
New Androidadjustresizebugfix (activity);
}
private void Possiblyresizechildofcontent () {
int usableheightnow = Computeusableheight ();
if (usableheightnow! = usableheightprevious) {
int usableheightsanskeyboard = Mchildofcontent.getrootview (). GetHeight ();
int heightdifference = Usableheightsanskeyboard-usableheightnow;
if (Heightdifference > (USABLEHEIGHTSANSKEYBOARD/4)) {
Keyboard probably just became visible
If there is a height change, the mchildofcontent.requestlayout () will not be re-measured after the interface
Let's just subtract 1 from the original height here.
Framelayoutparams.height = usableHeightSansKeyboard-1;
} else {
Keyboard probably just became hidden
Framelayoutparams.height = Usableheightsanskeyboard;
}
Mchildofcontent.requestlayout ();
usableheightprevious = Usableheightnow;
}
}
private int computeusableheight () {
Rect r = new Rect ();
Mchildofcontent.getwindowvisibledisplayframe (R);
return r.bottom-r.top + statusbarheight;
}
private int getstatusbarheight () {
try {
class<?> C = class.forname ("Com.android.internal.r$dimen");
Object obj = c.newinstance ();
Field field = C.getfield ("Status_bar_height");
int x = Integer.parseint (Field.get (obj). toString ());
int dimensionpixelsize = Mactivity.getresources (). getdimensionpixelsize (x);
return dimensionpixelsize;
} catch (Exception e) {
E.printstacktrace ();
}
return 0;
}}
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:
Androidadjustresizebugfix.assistactivity (this);
The above is about listening to the soft keyboard pop-up and the contents of the collection.
Perfect solution for Android soft keyboard monitor 1