The ultimate solution to all kinds of discomfort caused by the android soft keyboard pop-up

Source: Internet
Author: User
Tags xml attribute

Many developers may encounter a problem: When you click the input box on the logon interface, the buttons below are sometimes blocked by the input box, which is not conducive to user experience, so many people want to squeeze the buttons when the keyboard pops up. Many developers want to listen to the keyboard status, which is undoubtedly a very troublesome practice.

We can go to androidmanifest. XML activity setting attribute: Android: windowsoftinputmode = "adjustresize". When the keyboard pops up, you must re-layout the main window layout and call the onsizechanged method, remember that when we set it to "adjustresize", we should not set it to full screen mode. Otherwise, setting this attribute will not work. When we set Android: windowsoftinputmode = "adjustpan", the onsizechanged method will not be called in the main window, and part of the interface will be overwritten by the soft keyboard, so it will not be squeezed onto the soft keyboard.

Let's test through a piece of code. When we set this attribute, the input method is displayed. What does the system do:

Rewrite layout:

 
 
  1.     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) {             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:

  
  
  1.     <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> 

Activity setting attribute of androidmanifest. xml: Android: windowsoftinputmode = "adjustresize"
Run the program and click the text box to view the debugging 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 debugging results, we can see that after clicking the text box, the root layout calls onmeasure, onsizechanged and onlayout.

If the value of windowsoftinputmode is set to adjustpan, the screen size of the activity main window is not adjusted to reserve space for the soft keyboard. On the contrary, the content of the current window is automatically moved so that the current focus is not overwritten by the keyboard and the user can always see the part of the input content. This is usually not expected to be adjusted because the user may close the keyboard to obtain interaction with the covered content.
In the above example, we will change the androidmanifest. XML Attribute: Android: windowsoftinputmode = "adjustpan"

Run the command again and click the text box to view the debugging 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 can see that the system has re-implemented measrue and layout, but we found that onsizechanged was not called during layout, which means that the size of the original layout has not changed before and after the input method pop-up.

You can also set other attributes:

"Stateunspecified"

The keyboard status (whether it is hidden or visible) is not specified. The system selects an appropriate status or topic-dependent setting.

This is to set the default behavior of the software disk.

"Stateunchanged"

The keypad remains visible or hidden no matter what the last time it was, when the main window appears in front of it.

"Statehidden"

When the user selects the activity, the soft keyboard is hidden-that is, when the user is sure to navigate to the activity, instead of returning it because it leaves another activity.

"Statealwayshidden"

The keyboard is always hidden when the activity Main Window gets the focus.

"Statevisible"

The soft keyboard is visible when it is appropriate (when you navigate to the activity main window ).

"Statealwaysvisible"

When you select this activity, the soft keyboard is visible-that is, when you are sure to navigate to this activity, instead of returning it because it leaves another activity.

"Adjustunspecified"

It is not specified whether the activity main window is adjusted to set aside space for the soft keyboard, or whether the content of the window is visible to the current focus on the screen. The system automatically selects one of these modes, depending on whether the content of the window has any layout view that can scroll their content. If there is such a view, the window will be adjusted. This assumption can make the content of the scrolling window visible in a small area. This is the default behavior settings for the main window.

"Adjustresize"

The screen size of the main activity window is always adjusted to reserve space for the keyboard.

"Adjustpan"

The main window of the activity does not adjust the screen size to allow space on the keyboard. On the contrary, the content of the current window is automatically moved so that the current focus is not overwritten by the keyboard and the user can always see the part of the input content. This is usually not expected to be adjusted because the user may close the keyboard to obtain interaction with the covered content.

Related Article

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.