How to stop EditText from gaining focus at Activity startup in Android

后端 未结 30 2700
别那么骄傲
别那么骄傲 2020-11-21 06:40

I have an Activity in Android, with two elements:

  1. EditText
  2. ListView

When my Activity

相关标签:
30条回答
  • 2020-11-21 07:19

    Add following in onCreate method:

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    
    0 讨论(0)
  • 2020-11-21 07:20
    <TextView
        android:id="@+id/TextView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        style="@android:style/Widget.EditText"/>
    
    0 讨论(0)
  • 2020-11-21 07:22

    For me, what worked on all devices is this:

        <!-- fake first focusable view, to allow stealing the focus to itself when clearing the focus from others -->
    
        <View
        android:layout_width="0px"
        android:layout_height="0px"
        android:focusable="true"
        android:focusableInTouchMode="true" />
    

    Just put this as a view before the problematic focused view, and that's it.

    0 讨论(0)
  • 2020-11-21 07:26

    Simple solution: In AndroidManifest in Activity tag use

    android:windowSoftInputMode="stateAlwaysHidden"
    
    0 讨论(0)
  • 2020-11-21 07:27

    The following worked for me in Manifest. Write ,

    <activity
    android:name=".MyActivity"
    android:windowSoftInputMode="stateAlwaysHidden"/>
    
    0 讨论(0)
  • 2020-11-21 07:28

    The only solution I've found is:

    • Create a LinearLayout (I dunno if other kinds of Layout's will work)
    • Set the attributes android:focusable="true" and android:focusableInTouchMode="true"

    And the EditText won't get the focus after starting the activity

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