Android soft keyboard properties

Soft keyboard attributes in the AndroidManifest. Define the Activity in the XML node, attribute called android: windowSoftInputMode this property has two optional parts, one is the “state” beginning to specify software disk show hidden state, One is the window adjustment mode starting with “adjust” that specifies the Activity to display the soft keyboard. You can specify a, two, or specify values separated by |.

WindowSoftInputMode The values are as follows:

The state at the beginning:

  • StateVisible: indicates that the soft keyboard always pops up when entering the Activity.
  • StateAlwaysVisible: indicates that the soft keyboard will always pop up when you enter the Activity. Even if you jump to another interface, the soft keyboard state is hidden, and the soft keyboard will still pop up when you jump back.
  • StateHidden: Indicates that the soft keyboard is always hidden when entering an Activity.
  • StateAlwaysHidden: indicates that the soft keyboard is always hidden when you enter the Activity. The soft keyboard is displayed even when you jump to another interface, and the soft keyboard is still hidden when you return to the interface.
  • StateUnchanged: Indicates that the current soft keyboard remains unchanged. If the soft keyboard is displayed on other screens, it is displayed when switched to the screen, or hidden when hidden.
  • StateUnspecified: Indicates the unspecified state. The system defines the appropriate state based on the interface.

Adjust the beginning:

  • AdjustPan: This property will move the layout up to make sure that the focus is not blocked by the soft keyboard, but when there is an input box in the adjustPan mode, the user has to make the soft keyboard go away and click the input box again.
  • AdjustResize: This property always adjusts the size of the Activity’s main window to make room for the soft keyboard, which doesn’t work unless the Activity’s content can be adjusted.

Soft keyboard occlusion problem

  • Method 1: windowSoftInputMode configuration
    1. AdjustPan configuration: adjustPan layout will be jacking up, if there is input box still blocked, user experience is not good.
    2. ScrollView+adjustResize: adjustResize requires adjustable layout height, so add ScrollView to the layout.

However, it does not work when you set it to full screen or immerse the status bar.

Set up the full screen

When setting the getWindow (). SetFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN, WindowManager. LayoutParams. FLAG_FULLSCREEN), When the input box is more than one screen, the input box under the current input box can’t slide up and down to input, which is similar to adjustPan. It can only slide part, and it doesn’t adjust the interface height.

Set up an immersive status bar

Under the style to join < item name = “android: windowTranslucentStatus” > true < / item >, click on the input box software will keep out input box, Can be set by the root layout android: fitsSystemWindows = “true” solve the problem of failure.

  • Method 2: By listening to the View on the top of the Activity to determine whether the soft keyboard is up, and then redraw the interface

    public class SoftHideKeyBoardUtil { public static void assistActivity(Activity activity) { new SoftHideKeyBoardUtil(activity); } private View mChildOfContent; private int usableHeightPrevious; private FrameLayout.LayoutParams frameLayoutParams; Private SoftHideKeyBoardUtil(Activity Activity) {//1. It's actually a DecorView and the control it uses is FrameLayout FrameLayout Content = (FrameLayout) Activity.findViewById (Android.r.i. D.c ontent); //2setContentView mChildOfContent = Content.getChildAt (0); // set a View tree listener for the Activity's XML layout. When the layout changes, such as the keyboard popping up or down, Will be back here to monitor mChildOfContent. GetViewTreeObserver () addOnGlobalLayoutListener (() - > {/ / 4, the current layout changes, Layout for the Activity of XML to redraw possiblyResizeChildOfContent (); }); / / 5, access to the placement of the Activity of XML layout parameters frameLayoutParams = (FrameLayout. LayoutParams) mChildOfContent. GetLayoutParams (); } // Get the available height of the interface. If the soft keyboard is up, subtract the available height of the Activity's XML layout from the keyboard heightpossiblyResizeChildOfContentInt usableHeightNow = computeUsableHeight() {//1, computeUsableHeight(); //2. If the current available height is different from the original valueif(usableHeightNow ! Int usableHeightSansKeyboard = usableHeightSansKeyboard = usableHeightSansKeyboard = usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); Int heightDifference = usableHeightSansKeyboard - usableHeightNow; int heightDifference = usableHeightSansKeyboard - usableHeightNow; //5. When the height difference is greater than 1/4 of the screen, the keyboard pops upif(heightDifference > (usableHeightSansKeyboard / 4)) { The height of the Activity's XML layout should subtract the keyboard height framelayoutparams. height = usableHeightSansKeyboard - heightDifference; }else{ frameLayoutParams.height = usableHeightSansKeyboard; } / / 7, redraw the Activity of the XML layout mChildOfContent. RequestLayout (). usableHeightPrevious = usableHeightNow; } } private intcomputeUsableHeight() { Rect r = new Rect(); mChildOfContent.getWindowVisibleDisplayFrame(r); // In full screen mode, return r.box, which is the height of the status barreturnr.bottom; }}Copy the code