Prevent the keyboard from displaying on activity start

后端 未结 17 713
轮回少年
轮回少年 2020-11-30 17:26

I have an activity with an Edit Text input. When the activity is initialized, the Android keyboard is shown. How can the keyboard remain hidden until the user

相关标签:
17条回答
  • 2020-11-30 17:37

    just add this on your Activity:

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
          if (getCurrentFocus() != null) {
               InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
               imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
          }
          return super.dispatchTouchEvent(ev);
    }
    
    0 讨论(0)
  • 2020-11-30 17:39

    Try to declare it in menifest file

    <activity android:name=".HomeActivity"
          android:label="@string/app_name"
          android:windowSoftInputMode="stateAlwaysHidden"
          >
    
    0 讨论(0)
  • 2020-11-30 17:40

    Function to hide the keyboard.

    public static void hideKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
    
        if (view != null) {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
    
            inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }
    

    Hide keyboard in AndroidManifext.xml file.

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        android:windowSoftInputMode="stateHidden">
    
    0 讨论(0)
  • 2020-11-30 17:42

    Just add this in your manifest.xml file

    <activity android:name=".MainActivity"
                android:windowSoftInputMode="stateHidden">
    

    You are all done.

    0 讨论(0)
  • 2020-11-30 17:43

    Hide it for all activities using the theme

    <style name="MyTheme" parent="Theme">
        <item name="android:windowSoftInputMode">stateHidden</item>
    </style>
    

    set the theme

    <application android:theme="@style/MyTheme">
    
    0 讨论(0)
  • 2020-11-30 17:44

    declare this code( android:windowSoftInputMode="stateAlwaysHidden") in manifest inside your activity tag .

    like this :

    <activity android:name=".MainActivity"
      android:windowSoftInputMode="stateAlwaysHidden">
    
    0 讨论(0)
提交回复
热议问题