Prevent the keyboard from displaying on activity start

后端 未结 17 714
轮回少年
轮回少年 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:57

    To expand upon the accepted answer by @Lucas:

    Call this from your activity in one of the early life cycle events:

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    Kotlin Example:

    override fun onResume() {
      super.onResume()
    
      window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
    }
    
    0 讨论(0)
  • 2020-11-30 17:58

    You can also write these lines of code in the direct parent layout of the .xml layout file in which you have the "problem":

    android:focusable="true"
    android:focusableInTouchMode="true"
    

    For example:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        ...
        android:focusable="true"
        android:focusableInTouchMode="true" >
    
        <EditText
            android:id="@+id/myEditText"
            ...
            android:hint="@string/write_here" />
    
        <Button
            android:id="@+id/button_ok"
            ...
            android:text="@string/ok" />
    </LinearLayout>
    


    EDIT :

    Example if the EditText is contained in another layout:

    <?xml version="1.0" encoding="utf-8"?>
    <ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        ... >                                            <!--not here-->
    
        ...    <!--other elements-->
    
        <LinearLayout
            android:id="@+id/theDirectParent"
            ...
            android:focusable="true"
            android:focusableInTouchMode="true" >        <!--here-->
    
            <EditText
                android:id="@+id/myEditText"
                ...
                android:hint="@string/write_here" />
    
            <Button
                android:id="@+id/button_ok"
                ...
                android:text="@string/ok" />
        </LinearLayout>
    
    </ConstraintLayout>
    

    The key is to make sure that the EditText is not directly focusable.
    Bye! ;-)

    0 讨论(0)
  • 2020-11-30 18:00

    or You can use focusable tag in xml.

    android:focusable="false"

    set it to false.here is the snippet of the code

    0 讨论(0)
  • 2020-11-30 18:02

    I think the following may work

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    

    I've used it for this sort of thing before.

    0 讨论(0)
  • 2020-11-30 18:02

    Try this -

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

    Alternatively,

    1. you could also declare in your manifest file's activity -
    <application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              android:label="@string/app_name"
              android:windowSoftInputMode="stateHidden"
              >
    
    1. If you have already been using android:windowSoftInputMode for a value like adjustResize or adjustPan, you can combine two values like:
    <activity
            ...
            android:windowSoftInputMode="stateHidden|adjustPan"
            ...
            >
    

    This will hide the keyboard whenever appropriate but pan the activity view in case the keyboard has to be shown.

    0 讨论(0)
提交回复
热议问题