The article directories

  • The principle of soft keyboard display
  • Soft keyboard display adjustment
  • Chestnut 1: unscrollable page, pop-up keyboard page is top up
  • Chestnut 2: scrollable page, keyboard pop-up content is top up
  • Automatic pop-up soft keyboard
  • Does not automatically pop up the soft keyboard
  • Close the page and the keyboard does not disappear solution

The principle of soft keyboard display

What is the essence of software disk? The soft keyboard is actually a Dialog

The InputMethodService creates a Dialog for our input method and sets some parameters of the Dialog’s Window, such as Gravity, to display at the bottom or in full screen. When we click on the input box, the system adjusts the active main window to make room for the input method, and then displays the Dialog at the bottom or in full screen

Soft keyboard display adjustment

Android defines a property called windowSoftInputMode that sets the mode in which the Activity main window interacts with the soft keyboard to avoid the problem of the soft keyboard blocking content. We can set the Activity in androidmanifet.xml. Such as: android: windowSoftInputMode = “stateUnchanged | adjustPan”

The optional value of this property has two parts, one is the state control of the soft keyboard, which controls whether the soft keyboard is hidden or displayed, and the other is the adjustment of the Activity window to make room for displaying the soft keyboard

Android: windowSoftInputMode attribute set must be a value in the following, or a “state” value plus a “adjust” combination, each separated by | between values

  • StateUnspecified – not specified state: when we are not set android: windowSoftInputMode properties, software is the interactive methods used by default, the system will be according to the corresponding soft keyboard interface display mode
  • StateUnchanged – Unchanged state: The current screen’s soft keyboard state depends on the previous screen’s soft keyboard state, whether hidden or displayed
  • StateHidden – Hidden State: When this state is set, the soft keyboard is always hidden, regardless of whether there is a need for input
  • StateAlwaysHidden – AlwaysHidden state: When this state is set, the soft keyboard is always hidden. Unlike stateHidden, when we jump to the next screen, if the soft keyboard is displayed on the next page and we come back, the soft keyboard is hidden
  • StateVisible – Visible State: When set to this state, the soft keyboard is always visible and can be forced to pop out even when there is no input field on the interface
  • StateAlwaysVisible – Always Show state: When set to this state, the soft keyboard is always visible, unlike stateVisible, when we jump to the next screen, if the soft keyboard is hidden on the next page and we come back, the soft keyboard is visible
  • AdjustUnspecified – Unspecified mode: Sets the display relationship between the soft keyboard and the software. This option is also the default setting when we do not set this value. In this case, the system selects a different mode based on the interface
  • AdjustResize – Adjust mode: In this mode, the window always adjusts the screen size to accommodate the soft keyboard. AdjustPan this option can’t be used in conjunction with adjustPan, if neither of these properties is set, the system will automatically select one of them based on the layout in the window
  • AdjustPan – Default mode: adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan) – adjustPan (adjustPan

Chestnut 1: unscrollable page, pop-up keyboard page is top up

AdjustPan = adjustPan (adjustPan) adjustPan = adjustPan (adjustPan) adjustPan (adjustPan

Chestnut 2: scrollable page, keyboard pop-up content is top up

For a layout with scroll controls, use adjustResize. When you click the adjustResize Edittext covered by the keyboard, the top text box is lifted and you can scroll to see what is lifted

According to this principle, we can use ScrollView as the root layout and adjustResize mode to solve the problem that the soft keyboard blocks the page

Automatic pop-up soft keyboard

Timer timer=new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); }}, 1000); // Automatically ejects after secondsCopy the code

or

Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { InputMethodManager inputManager = (InputMethodManager) et1.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(et1, 0); }}, 1000); // Automatically eject after 1 secondCopy the code

Does not automatically pop up the soft keyboard

AndroidManifest set in the Activity of windowSoftInputMode properties for adjustUnspecified | stateHidden

android:windowSoftInputMode="adjustUnspecified|stateHidden"
Copy the code

or

edit.clearFocus();
Copy the code

If the setting is invalid, see clearFocus() for reasons and solutions

or

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edit.getWindowToken(),0);
Copy the code

Also need to set the parent layout of the android: focusableInTouchMode to true

Close the page and the keyboard does not disappear solution

The keyboard is not displayed on page A. If you jump from page A to page B, the keyboard is automatically displayed on page B

After page B is closed, the keyboard is not closed when you return to page A

(mSearchOrderEditText is an EditText). (mSearchOrderEditText is an EditText.

       @Override
    public void onPause(a) {
      InputMethodManager inputMethodManager = (InputMethodManager) mSearchOrderEditText.getContext().getSystemService(
                Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(mSearchOrderEditText.getWindowToken(), 0);
        super.onPause();
    }
Copy the code

Do you have any good ideas, please leave a message…