SoFunction
Updated on 2025-03-11

Implementation method of hiding and displaying soft keyboards and not automatically popping keyboards in Android

1、//Hide soft keyboard   

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);   

  

2、//Show soft keyboard,ControlsIDCan beEditText,TextView   

((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE)).showSoftInput(ControlsID, 0); 

 

3. The keyboard does not pop up automatically:

WithEditTextThe control will be automatically obtained when it is displayed for the first timefocusand the keyboard will pop up if you don't want the keyboard to pop up automaticallyThere are two ways:

Method 1: Set the corresponding activity in the mainfest file

android:windowSoftInputMode="stateHidden" orandroid:windowSoftInputMode="stateUnchanged"

Method 2: You can put a hidden TextView in the layout and then requestFocus when onCreate.

NoticeTextViewDon't set itVisiable=gone, otherwise it will fail

, can put a hidden one in the layoutTextView, and thenonCreateWhenrequsetFocus

NoticeTextViewDon't set itVisiable=gone, otherwise it will fail

<TextView

        android:

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:focusable="true"

android:focusableInTouchMode="true"

        />

 

TextView textView = (TextView)findViewById(.text_notuse);

            ();