简体   繁体   中英

Hide soft keyboard when on new page

I have an application with an EditText element on the next view. This means that when my application is loaded the soft keyboard appears per default.

What code do I use to hide this keyboard on IntelliJ?

UPDATE

For Hiding keyboard, you can have two ways:

Either: (This will hide the keyboard on app/activity launch)

put this android:windowSoftInputMode="adjustPan" in activity tag of related activity in manifest.xml as:

<activity
        android:name=".MainActivity"
        android:windowSoftInputMode="adjustPan" />

Or:

put this method in your activity and call it whenever you have to hide the keyboard.

    @SuppressWarnings("ConstantConditions")
    public void hideKeyBoard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }

and call it as hideKeyBoard(view); .

Remember, you have to pass the view to hide the keyboard as above.

Easiest way

in your onCreate()

   this.getWindow().setSoftInputMode
    (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Sometimes this won't work. So what you can do is, make your editText -> focusable as false inside your XML .

This is my method for showing and hiding keyboard, called from an activity or fragment

public void hideKeyboard(final boolean hide) {
    // Check if no view has focus:
    View view = this.getCurrentFocus();
    if (view == null) {
        return;
    }
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    if (hide) {
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } else {
        new Handler().postDelayed(() -> imm.showSoftInput(view, 0), 50);
    }
}

In Manifest put android:windowSoftInputMode="adjustPan" under your activity on which you want to hide soft keyboard like:

    <activity android:name=".Viewschedule"
          android:screenOrientation="portrait"
          android:windowSoftInputMode="adjustPan"></activity>

OR

   InputMethodManager imgr = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  imgr.showSoftInput(view, 0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM