Many developers who write the login interface encounter a problem: that is, when you click on the input box, the bottom button is sometimes blocked by the input box, which is not conducive to the user's experience, so many people want the soft keyboard pop-up, you can also push the button up. Many developers want to listen to the state of the keyboard, which is undoubtedly a very troublesome approach.
We can set properties in androidmanifest.xml activity: Android:windowsoftinputmode = "Adjustresize", when the soft keyboard pops up, to re-layout the main window layout, and call the Onsizechanged method, remember that when we set to "Adjustresize", our interface is not set to full-screen mode, otherwise setting this property will not have any effect. When we set Android:windowsoftinputmode = "Adjustpan", the main window will not call the Onsizechanged method, part of the interface will be covered by the soft keyboard, will not be squeezed onto the soft keyboard.
We test it with a piece of code, and when we set that property, the system does what it does when it pops up:
To rewrite layout layouts:
- [Java]View Plaincopyprint?
- Public class Resizelayout extends linearlayout{
- private static int count = 0;
- Public resizelayout (context context, AttributeSet attrs) {
- Super (context, attrs);
- }
- @Override
- protected void onsizechanged (int W, int h, int oldw, int oldh) {
- super.onsizechanged (W, H, OLDW, OLDH);
- LOG.E ("onsizechanged" + count++, "=>onresize called! w= "+w + ", h= "+h+", oldw= "+oldw+", oldh= "+OLDH";
- }
- @Override
- protected void OnLayout (boolean changed, int L, int T, int R, int b) { /c2>
- super.onlayout (changed, L, T, R, b);
- LOG.E ("onlayout" + count++, "=>onlayout called! L= "+ L + ", t= "+ t + ", r= "+ R + ", b= "+b";
- }
- @Override
- protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
- super.onmeasure (Widthmeasurespec, Heightmeasurespec);
- LOG.E ("onmeasure" + count++, "=>onmeasure called! Widthmeasurespec= "+ Widthmeasurespec + ", heightmeasurespec= "+ heightmeasurespec);
- }
Our layout is set to:
- [HTML]View Plaincopyprint?
- <com.winuxxan.inputMethodTest.ResizeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/root_layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <EditText
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <linearlayout
- android:id="@+id/bottom_layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:gravity="Bottom">s
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- android:background="#77777777"
- />
- </linearlayout>
- </com.winuxxan.inputMethodTest.ResizeLayout>
Androidmanifest.xml Activity Settings properties: Android:windowsoftinputmode = "Adjustresize"
Run the program, click the text box , view debug information:
e/onmeasure 6 (7960): =>onmeasure called! widthmeasurespec=1073742144, Heightmeasurespec = 1073742024
e/onmeasure 7 (7960): =>onmeasure called! widthmeasurespec= 1073742144, Heightmeasurespec = 1073742025
e/onsizechanged 8 (7960): =>onsizechanged called! w=320,h=201,oldw=320,oldh=377
e/onlayout 9 (7960): =>onlayout called! l=0, t=0,r=320,b=201
From the debug results we can see that when we click on the text box, the root layout calls onmeasure,onsizechanged and OnLayout.
If the value of Windowsoftinputmode is set to Adjustpan, the Activity main window does not resize the screen to allow space on the soft keyboard. Instead, the contents of the current window are automatically moved so that the current focus is never covered by the keyboard and the user can always see the part of the input. This is usually not expected than resizing, because the user may turn off the soft keyboard in order to get an interactive operation with the covered content.
In the example above, we will change the properties of androidmanifest.xml: Android:windowsoftinputmode = "Adjustpan"
Rerun and click the text box to view the debug information:
E/onmeasure 6 (8378): =>onmeasure called! widthmeasurespec=1073742144, heightmeasurespec=1073742200
E/onmeasure 7 (8378): =>onmeasure called! widthmeasurespec=1073742144, heightmeasurespec=1073742201
E/onlayout 8 (8378): =>onlayout called! L=0, t=0,r=320,b=377
We see: The system also re-measrue and layout, but we found that the layout process is not called onsizechanged, which indicates that the input method before and after the popup has not changed the size of the original layout.
Of course there are other properties that can be set:
"Stateunspecified"
The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will select an appropriate state or a theme-dependent setting.
This is the default setting for the software disk behavior.
"Stateunchanged"
The soft keyboard is persisted regardless of its last state, whether visible or hidden, when the main window appears in front.
"Statehidden"
When the user chooses the activity, the soft keyboard is hidden-that is, when the user determines that the activity is navigated to, instead of returning to it due to leaving another activity.
"Statealwayshidden"
The soft keyboard is always hidden when the activity main window gets focus.
"Statevisible"
The soft keyboard is visible when that is normal (when the user navigates to the Activity main window).
"Statealwaysvisible"
When the user chooses this activity, the soft keyboard is visible-that is, when the user determines that the activity is navigated to, rather than returning to it due to leaving another activity.
"Adjustunspecified"
It is not specified whether the Activity main window is resized to allow space on the soft keyboard, or whether the contents of the window are visible on the screen where the current focus is. The system will automatically select one of these modes primarily depending on whether the contents of the window have any layout view that can scroll their content. If there is such a view, the window will be resized so that the contents of the scrolling window can be visible in a smaller area. This is the default behavior setting for the main window.
"Adjustresize"
The Activity main window is always resized on the screen to allow space for the soft keyboard
"Adjustpan"
The Activity main window does not adjust the size of the screen to allow space for the soft keyboard. Instead, the contents of the current window are automatically moved so that the current focus is never covered by the keyboard and the user can always see the part of the input. This is usually not expected than resizing, because the user may turn off the soft keyboard in order to get an interactive operation with the covered content.