In Android, it often interacts with the IME's software keyboard. In the manifest file, the system gives the activity a property-windowsoftinputmode to control how the input method is displayed.
This property provides a way for the activity's window to interact with the window of the soft keyboard. The property settings here have a double-factor effect:
1. Soft keyboard display and hide. -When the activity interface becomes the focus of the user, or is hidden or displayed.
2.
Adjust the main window form for activty. Alternatively, the Activity's window form is smaller to make room for the soft keyboard, or when the activity's part window is covered by the software, the activity's contents are moved so that the user can see the current focus.
You must choose at least one of the following property values to set it, either "state ..." or "adjust ...".
You can set multiple values. Sets a different property value. To be separated by |.
For example:
<activity Android:windowsoftinputmode = " statevisible|adjustresize " ... >
Below is a descriptive description of the value of the attribute.
does not specify the state of the software (show or hide). The system selects the corresponding state according to the settings in the theme. The default setting for this property soft keyboard. does not specify whether to adjust the activity interface. Or resize the activity form to make room for the soft keyboard or to move the contents of the form so that the current focus on the screen is visible. The system chooses one of the modes on its own, depending on whether the form includes a view that slides its contents. If you have this view, the size of the form is adjusted. In this hypothetical scenario, a very small swipe is available to use the contents of the form to be visible.
This property is the primary windowr default setting.
value |
descriptive narrative |
" stateunspecified " | TD valign= "Top" style= "width:434.0px; Border-style:solid; border-width:1.0px 1.0px 1.0px 1.0px; Border-color: #808080 #808080 #808080 #808080; padding:2.0px 2.0px 2.0px 2.0px ">
" stateunchnaged " |
always keep the state of the last soft keyboard. When activity goes to the front end, whether it's the last time it was shown or hidden. remain unchanged. |
" Statehidden " |
When the user enters the target activity. The soft keyboard remains hidden. The activity here is that the user is moving forward into the activity, rather than returning to the target activity because of exiting other activity. |
" statevisible " |
Just the right conditions (when the user advances to the Activity's main window), the keyboard |
" statealawaysvisible " |
When the user chooses to enter the target activity. The soft keyboard is set to visible. The activity here is the activity that the user goes forward to, not the target activity by exiting other activity |
" adjustunspecified " | TD valign= "Top" style= "width:434.0px; Border-style:solid; border-width:1.0px 1.0px 1.0px 1.0px; Border-color: #808080 #808080 #808080 #808080; padding:2.0px 2.0px 2.0px 2.0px ">
" adjustresize " |
activity is always resized to make room for the soft keyboard. |
"Adjustpan" |
The activity's main form is not resized to make room for the soft keyboard. Instead, the contents of the form are moved by themselves so that the current focus is not obscured by the soft keyboard, and the user can always see what he has entered. This value is typically used in cases where the user wants to resize the form very rarely. Because the user may need to turn off the soft keyboard to interact with other parts of the form. |
from the descriptive narrative of the above system, the system shows that both the activity and the soft keyboard are treated as window forms when dealing with two interactions.
very often, we all want to be able to hear the software keyboard display and shutdown status. For instance,
You have a login interface that is designed like this
You want to log in when it's this,
then you try to add a value to the activity's Windowsoftinputmode attribute when you add adjustresize. It is this
You're not reconciled. You change the adjustresize into Adjustpan, and the result is this
There is no way. Just rely on our own to monitor the keyboard display or hide to dynamically change the layout. The question now is how to monitor the dynamic change of the keyboard. The system does not provide a corresponding method. We can do that in two ways.
1. Under the Adjustresize property. The size of the activity's form changes, and the size of the layout in the form must be changed.
When the view size changes, it is bound to cause onsizechanged (int, int, int, int) callbacks. So you might define a layout yourself. To monitor the callback of the onsizechanged () function. And then in the middle of the corresponding treatment.
This method is also the most on-line transmission method.
2. With the Viewtreeoberserver class. Viewtreeoberserver can be used to register a listener. It can monitor the global changes of the view tree.
These changes include, but are not limited to, the entire view layout, drawing the source of the transfer, and changing the touch pattern.
Implementation of the first method:
Define a layout yourself. Taking Relativelayout as an example,
can be in int W, int H, int OLDW, int
Package Com.example.keyboardlistener;import Android.content.context;import Android.util.attributeset;import Android.util.log;import Android.widget.relativelayout;public class KeyboardLayout extends Relativelayout {private OnS Izechangedlistener Mchangedlistener; private static final String TAG = "Keyboardlayouttag"; Private Boolean mshowkeyboard = false; Public KeyboardLayout (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); TODO auto-generated Constructor stub} public keyboardlayout (context context, AttributeSet Attrs) {Super (context, attrs); TODO auto-generated Constructor stub} public keyboardlayout (context context) {super (context); TODO auto-generated Constructor stub} @Override protected void onmeasure (int widthmeasurespec, int height MEASURESPEC) {//TODO auto-generated Method Stub super.onmeasure (Widthmeasurespec, Heightmeasurespec); LOG.D (TAG, "onmeasure-----------"); } @Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {//TODO auto-generated met Hod stub super.onlayout (changed, L, T, R, b); LOG.D (TAG, "OnLayout-------------------"); } @Override protected void onsizechanged (int w, int h, int oldw, int oldh) {//TODO auto-generated Metho D stub super.onsizechanged (W, H, OLDW, OLDH); LOG.D (TAG, "--------------------------------------------------------------"); LOG.D (TAG, "w----" + W + "\ n" + "h-----" + H + "\ n" + "OLDW-----" + OLDW + "\NOLDH----" + OLDH); if (null! = Mchangedlistener && 0! = OLDW && 0! = OLDH) {if (H < OLDH) {MSh Owkeyboard = true; } else {mshowkeyboard = false; } mchangedlistener.onchanged (Mshowkeyboard); LOG.D (TAG, "mshowkeyboard-----" + mshowkeyboard); }} PUBlic void Setonsizechangedlistener (Onsizechangedlistener listener) {Mchangedlistener = listener; } interface onsizechangedlistener{void OnChanged (Boolean showkeyboard); } }
Create a handler in the main activity. Updates the UI through handler when the supervisor hears that the layout has changed.
Package Com.example.keyboardlistener;import Android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.os.message;import Android.util.log;import Android.view.viewtreeobserver.onpredrawlistener;import Android.widget.button;import Com.example.keyboardlistener.keyboardlayout.onsizechangedlistener;public class Mainactivity extends Activity {priv Ate static final String TAG = "Keyboardlayouttag"; Private KeyboardLayout Mroot; Private Button Mlogin; private int mloginbottom; private static final int keyboard_show = 0X10; private static final int keyboard_hide = 0X20; Private Boolean mgetbottom = true; Private Handler Mhandler = new Handler () {@Override public void Handlemessage (Message msg) {// TODO auto-generated Method Stub super.handlemessage (msg); Switch (msg.what) {case KEYBOARD_HIDE:mRoot.setPadding (0, 0, 0, 0); Break CaSe keyboard_show:int mrootbottom = mroot.getbottom (); LOG.D (TAG, "The Mloginbottom is" + mloginbottom); Mroot.setpadding (0, mrootbottom-mloginbottom, 0, 0); Break Default:break; } } }; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Getactionbar (). Hide (); Mroot = (keyboardlayout) Findviewbyid (R.id.root_view); Mlogin = (Button) Findviewbyid (R.id.login); Mroot.setonsizechangedlistener (New Onsizechangedlistener () {@Override public void Oncha Nged (Boolean Showkeyboard) {//TODO auto-generated Method stub if (Showkeyboard) { Mhandler.sendmessage (Mhandler.obtainmessage (keyboard_show)); LOG.D (TAG, "show keybOard "); } else {mhandler.sendmessage (Mhandler.obtainmessage (keyboard_hide)); } } }); Mroot.getviewtreeobserver (). Addonpredrawlistener (New Onpredrawlistener () {@Override Pub Lic Boolean Onpredraw () {//TODO auto-generated Method stub if (Mgetbottom) { Mloginbottom = Mlogin.getbottom ();//Gets the location information of the login button.} Mgetbottom = false; return true; } }); }//@Override//Public boolean Oncreateoptionsmenu (Menu menu) {/////Inflate the menu, this adds items to the Action Bar If it is present.//getmenuinflater (). Inflate (R.menu.main, menu);//Return true;//}}
another way to achieve this:
Use the Viewtreeobserver class to join the view you want to listen to Ongloballayoutlistener Listener. Then in the Ongloballayout () method The form changes to infer whether the keyboard is paged out. The following is the code for the Viewtreeobserver listening section:
Mroot.getviewtreeobserver (). Addongloballayoutlistener (New Ongloballayoutlistener () { @Override public void Ongloballayout () { int offset = Mroot.getrootview (). GetHeight ()-mroot.getheight (); Infer whether the keyboard displays if (offset >) { mhandler.sendmessage (mhandler.obtainmessage (Keyboard_show)) based on the offset value of the view; } else { mhandler.sendmessage (mhandler.obtainmessage (Keyboard_hide));}} );
The detailed implementation of the handler is the same as the handler in the above structure.
Article: Inputmethod
Sample source Code
Research on the display and hiding problem of Android soft keyboard