Linkage control of Android input box

Source: Internet
Author: User


* For some pages containing two input boxes and a confirmation button, such as login registration page, it is usually an account entry box, a password input box, and a confirmation button.
* When you click the second input box to eject the keyboard, the keyboard will often block the confirmation button, and we often ask for the confirmation button to be moved above the input box to avoid blocking the confirmation button.
* The following three classes implement this function, the code is independent, the usage is simple and quick
*
* 1. When an input box is entered:
* New Softinputoneedtctrl (your fragmentactivity). Register (view viewbaseline, view Viewtomove, EditText EditText);

* 2. Two input boxes:
* New Softinputtwoedtctrl (your fragmentactivity) register (view viewbaseline, view Viewtomove, EditText edt1, EditText EDIT2);
*/
ImportAndroid.graphics.Rect;ImportAndroid.os.Handler;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.view.View;ImportAndroid.view.View.OnLayoutChangeListener;ImportAndroid.view.ViewTreeObserver.OnGlobalLayoutListener;ImportAndroid.widget.EditText; Public Abstract classSoftinputctrlabsImplementsOnlayoutchangelistener, Ongloballayoutlistener, Runnable {protectedfragmentactivity mactivity; Private volatile intMnewbottom; Private volatile intMoldbottom; protectedView Mviewbaseline; protectedView Mviewtomove; protected volatile BooleanMhasmovedup =false; PrivateHandler Mhandler =NewHandler (); PublicSoftinputctrlabs (fragmentactivity activity) {mactivity=activity; View Rootview=Mactivity.getwindow (). Findviewbyid (Android. R.id.content); Rootview.addonlayoutchangelistener ( This); Rootview.getviewtreeobserver (). Addongloballayoutlistener ( This); } Public voidregister (view viewbaseline, view Viewtomove, EditText ... edittexts) {Mviewtomove=Viewtomove; Mviewbaseline=Viewbaseline; } Public voidUnregister () {if(Mactivity! =NULL) {View Rootview=Mactivity.getwindow (). Findviewbyid (Android. R.id.content); Rootview.removeonlayoutchangelistener ( This); Rootview.getviewtreeobserver (). Removeglobalonlayoutlistener ( This); } reset (); } Public voidPostmove () {if(Mhandler! =NULL) {Mhandler.post ( This); }} @Override Public voidrun () {moveUp (); } @Override Public voidOnlayoutchange (View V,intLeftintTopintRightintBottomintOldLeft,intOldTop,intOldRight,intOldbottom) {Mnewbottom=Bottom; Moldbottom=Oldbottom; } Public intGetnewbottom () {returnMnewbottom; } Public intGetoldbottom () {returnMoldbottom; } protected synchronized voidmoveUp () {if(Mviewtomove = =NULL|| Mviewbaseline = =NULL)return; int[] Locationviewbaseline =New int[2]; Mviewbaseline.getlocationonscreen (Locationviewbaseline); int[] Locationviewtomove =New int[2]; Mviewtomove.getlocationonscreen (Locationviewtomove); Rect Rectviewtomove=NewRect (); Mviewtomove.getglobalvisiblerect (Rectviewtomove); intMovetopdistance = rectviewtomove.bottom-locationviewbaseline[1]-mviewbaseline. getheight (); if(Movetopdistance > 0 | | mhasmovedup)return; if(Movetopdistance < 0) {Mhasmovedup=true; } mviewtomove.setpadding (Mviewtomove.getpaddingleft (), Movetopdistance, Mviewtomove.getpaddingright ( ), Mviewtomove.getpaddingbottom ()); } Public voidReset () {Mviewtomove=NULL; Mviewbaseline=NULL; Mnewbottom= 0; Moldbottom= 0; Mhasmovedup=false; Mactivity=NULL; } Public voidresetposition () {if(Mviewtomove! =NULL) {mviewtomove.setpadding (Mviewtomove.getpaddingleft (),0, Mviewtomove.getpaddingright (), Mviewtomove.getpaddingbottom ()); Mhasmovedup=false; } }}
Importandroid.support.v4.app.FragmentActivity;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classSoftinputoneedtctrlextendsSoftinputctrlabs { PublicSoftinputoneedtctrl (fragmentactivity activity) {Super(activity); } @Override Public voidregister (view viewbaseline, view Viewtomove, EditText ... edittexts) {Super. Register (Viewbaseline, Viewtomove, edittexts); } @Override Public voidUnregister () {Super. Unregister (); } @Override Public voidongloballayout () {intBottom =Getnewbottom (); intOldbottom =Getoldbottom (); if(Bottom <Oldbottom)        {Postmove (); } Else if(Bottom >Oldbottom)        {resetposition (); }    }}
Importandroid.support.v4.app.FragmentActivity;Importandroid.view.KeyEvent;ImportAndroid.view.View;ImportAndroid.view.View.OnFocusChangeListener;ImportAndroid.view.inputmethod.EditorInfo;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.TextView.OnEditorActionListener; Public classSoftinputtwoedtctrlextendssoftinputctrlabs{Private StaticString TAG = "Softinputtwoedtctrl"; PrivateEditText Medtfirst; PrivateEditText Medtsecond; Private BooleanMedtactionnext =false; Private BooleanMedtsecondhasfocus =false; PrivateOneditoractionlistener Moneditoractionlistener =NewOneditoractionlistener () {@Override Public BooleanOneditoraction (TextView V,intActionId, KeyEvent event) {            if(ActionId = =Editorinfo.ime_action_next)            {Performactionnextedtfirst (); }            return false;    }    };  PublicSoftinputtwoedtctrl (fragmentactivity activity) {Super(activity); }    Private synchronized voidPerformactionnextedtfirst () {Medtactionnext=true;    Postmove (); } @Override Public voidregister (view viewbaseline, view Viewtomove, EditText ... edittexts) {Super. Register (Viewbaseline, Viewtomove, edittexts); if(Edittexts = =NULL|| Edittexts.length! = 2)return; Medtfirst= Edittexts[0]; Medtsecond= Edittexts[1]; Medtactionnext=false; Medtsecondhasfocus=false; Medtsecond.setonfocuschangelistener (NewOnfocuschangelistener () {@Override Public voidOnfocuschange (View V,BooleanHasfocus) {Medtsecondhasfocus=Hasfocus;            Postmove ();        }        });    Medtfirst.setoneditoractionlistener (Moneditoractionlistener); } @Override Public voidongloballayout () {intBottom =Getnewbottom (); intOldbottom =Getoldbottom (); if(Bottom <Oldbottom) {            if(!medtsecondhasfocus)return;        Postmove (); } Else if(Bottom >Oldbottom)        {resetposition (); } Else if(Medtactionnext &&Medtsecondhasfocus) {Medtactionnext=false; }} @Override Public voidmoveUp () {Super. MoveUp (); } @Override Public voidReset () {Super. Reset (); Medtfirst=NULL; Medtsecond=NULL; Medtactionnext=false; Medtsecondhasfocus=false; }}

Linkage control of Android input box

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.