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

后端 未结 30 2715
别那么骄傲
别那么骄傲 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:16

    I needed to clear focus from all fields programmatically. I just added the following two statements to my main layout definition.

    myLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
    myLayout.setFocusableInTouchMode(true);
    

    That's it. Fixed my problem instantly. Thanks, Silver, for pointing me in the right direction.

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

    This is the perfect and most easiest solution.I always use this in my app.

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    
    0 讨论(0)
  • 2020-11-21 07:17

    I had tried several answers individually but the focus is still at the EditText. I only managed to solve it by using two of the below solution together.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/mainLayout"
      android:descendantFocusability="beforeDescendants"
      android:focusableInTouchMode="true" >
    

    ( Reference from Silver https://stackoverflow.com/a/8639921/15695 )

    and remove

     <requestFocus />
    

    at EditText

    ( Reference from floydaddict https://stackoverflow.com/a/9681809 )

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

    Yeah I did the same thing - create a 'dummy' linear layout which gets initial focus. Furthermore, I set the 'next' focus IDs so the user can't focus it any more after scrolling once:

    <LinearLayout 'dummy'>
    <EditText et>
    
    dummy.setNextFocusDownId(et.getId());
    
    dummy.setNextFocusUpId(et.getId());
    
    et.setNextFocusUpId(et.getId());
    

    a lot of work just to get rid of focus on a view..

    Thanks

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

    Add android:windowSoftInputMode="stateAlwaysHidden" in the activity tag of the Manifest.xml file.

    Source

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

    At onCreate of your Activity, just add use clearFocus() on your EditText element. For example,

    edittext = (EditText) findViewById(R.id.edittext);
    edittext.clearFocus();
    

    And if you want to divert the focus to another element, use requestFocus() on that. For example,

    button = (Button) findViewById(R.id.button);
    button.requestFocus();
    
    0 讨论(0)
提交回复
热议问题