How to keep soft keyboard from opening on activity launch in Android?

后端 未结 7 1780
Happy的楠姐
Happy的楠姐 2020-12-12 21:31

In an Android app, whenever the activity launches, the textbox gets the focus and the soft keyboard pops up automatically. I have tried to stop this by using following line

相关标签:
7条回答
  • 2020-12-12 21:45

    You can use the following line of code to make sure the keyboard only pops up when a user clicks into an EditText

    Java

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

    Kotlin

    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    

    And

    You need to add

    android:windowSoftInputMode="adjustResize"

    to your activity tag in the AndroidManifest.xml file.

    0 讨论(0)
  • 2020-12-12 21:46

    Does the following work?

    // Find editor
    EditText editWindowInstance = this.findViewById(R.id.MyEditWindow);
    
    // close soft keyboard 
    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editWindowInstance.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
    
    0 讨论(0)
  • 2020-12-12 21:51

    You can put this code in your Activity.onCreate: this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    But I found that the most reliable and clean way was to simply set the focus onto to a different view, in your activity XML Layout

    <Button
      android:id="@+id/mybutton">
      <requestFocus />
    </Button>
    
    0 讨论(0)
  • 2020-12-12 21:51

    This will work perfectly, try this

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

    And add the following to your manifest.

    android:windowSoftInputMode="stateHidden|adjustResize"
    

    cheers enjoy coding....

    0 讨论(0)
  • 2020-12-12 21:59

    The following code works for me

    ((InputMethodManager) iClockActivity
                        .getSystemService(Context.INPUT_METHOD_SERVICE))
                        .showSoftInput(textView, 0);
    
    0 讨论(0)
  • 2020-12-12 22:00

    I know this is old but maybe it will help someone in the future...

    I haven't seen anyone suggest "stateHidden"

    From the Android docs - android:windowSoftInputMode

    Your manifest file would look like:

    <activity
        ...
        android:windowSoftInputMode="stateHidden|adjustResize"
        ...
    >
    
    0 讨论(0)
提交回复
热议问题