Hide and hide the Android keypad

Source: Internet
Author: User
Tags xml attribute

Hide and hide the Android keypad

 I. Principles of soft keyboard display
What is the essence of a software disk? The soft keyboard is actually a Dialog!
InputMethodService creates a Dialog for our input method, and sets some parameters of the Dialog Window (such as Gravity) so that it can be displayed at the bottom or in full screen. When you click the input box, the system adjusts the main activity window to free up space for the input method, and then displays the Dialog at the bottom or in full screen.

 

Ii. Adjust the main activity window
Android defines an attribute named windowSoftInputMode, which allows the program to control the adjustment method of the main activity window. We can set the Activity in AndroidManifet. xml. For example, android: windowSoftInputMode = "stateUnchanged | adjustPan"
This attribute has two optional values: the Status Control of the soft keyboard and the adjustment of the main activity window. The previous sections will not be discussed in this article. Please refer to the android documentation on your own.

Mode 1: compression mode
If the value of windowSoftInputMode is set to adjustResize, the main window of the Activity is always adjusted to reserve space for the keyboard.
Let's use a piece of code to test what the system did when we set this attribute and the input method popped up.
Rewrite Layout:

 1 public class ResizeLayout extends LinearLayout{  2     private static int count = 0;  3       4     public ResizeLayout(Context context, AttributeSet attrs) {  5         super(context, attrs);  6     }  7       8     @Override  9     protected void onSizeChanged(int w, int h, int oldw, int oldh) {     10         super.onSizeChanged(w, h, oldw, oldh); 11          12         Log.e("onSizeChanged " + count++, "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh); 13     } 14      15     @Override 16     protected void onLayout(boolean changed, int l, int t, int r, int b) { 17         super.onLayout(changed, l, t, r, b); 18         Log.e("onLayout " + count++, "=>OnLayout called! l=" + l + ", t=" + t + ",r=" + r + ",b="+b); 19     } 20      21     @Override 22     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 23         super.onMeasure(widthMeasureSpec, heightMeasureSpec); 24          25         Log.e("onMeasure " + count++, "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec); 26     } 
ResizeLayout

Xml:

<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.
In fact, when the value is set to adjustResize, when the keyboard pops up, you need to re-perform the measurement and layout for the main window layout. in layout, the size of the window is changed, so onSizeChanged is called.
We can also see from the running results that the TextView at the bottom is topped to the top of the input method.

Mode 2: translation mode

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.
From the running result, we can see that the TextView below is not pushed to the top of the input method.

In fact, when the input box is not blocked, the layout is not adjusted in this mode. However, when the input box is to be blocked, the window will be translated. That is to say, this mode always keeps the input box visible. For example, the entire window, including the title bar, is moved up to ensure that the text box is visible.

Mode 3 automatic mode
When the windowSoftInputMode attribute is set to adjustUspecified, it is not specified whether to adjust the size of the Activity main window to leave space for the soft keyboard, or whether the content in 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.
That is to say, the system automatically determines whether to adopt the translation mode or the compression mode, and determines whether the content can be rolled.

 

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.