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

后端 未结 7 1798
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.

提交回复
热议问题