Android system default input method Settings of input method display and hide

  • 2021-01-19 22:23:50
  • OfStack

1. Call the default input method of the display system

Method 1.


InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

imm.showSoftInput(m_receiverView(View that accepts soft keyboard input (View)),InputMethodManager.SHOW_FORCED(provide the current operation flag, SHOW_FORCED means forced display));

Method 2.


InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); ( This method can realize the input method on the window toggle display, if the input method has been shown on the window, then hide, if hidden, then show the input method to the window )

2. Call the default input method of the hidden system


((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); (WidgetSearchActivity Is the current Activity)

3. Get the state of input method open


InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isOpen=imm.isActive();
isOpen If the return true , it means that the input method is open 

1, // Hide the soft keyboard


((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(WidgetSearchActivity.this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 

2, // Display soft keyboard, control ID can be EditText,TextView


((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput( controls ID, 0); 

3, does not automatically pop up the keyboard:

The EditText control will automatically get the focus on the first display, and the keyboard will pop up. If you do not want to automatically pop up the keyboard, there are two methods:

Method 1: Set the corresponding activity in the mainfest file

android:windowSoftInputMode="stateHidden" or android:windowSoftInputMode="stateUnchanged".

Method 2: You can put a hidden TextView in the layout, and then requsetFocus at the time of onCreate.

Do not set Visiable=gone for TextView, otherwise it will fail
, you can put a hidden TextView in the layout, and then requsetFocus at the time of onCreate.
Do not set Visiable=gone for TextView, otherwise it will fail


 <TextView
     android:id="@+id/text_notuse"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:focusable="true"
 android:focusableInTouchMode="true" />
 TextView textView = (TextView)findViewById(R.id.text_notuse);
 textView.requestFocus();

Related articles: