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

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

    Excellent answers from Luc and Mark however a good code sample is missing. Adding the tag android:focusableInTouchMode="true" and android:focusable="true" to parent layout (e.g. LinearLayout or ConstraintLayout) like the following example will fix the problem.

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:focusable="true" 
        android:focusableInTouchMode="true"
        android:layout_width="0px" 
        android:layout_height="0px"/>
    
    <!-- :nextFocusUp and :nextFocusLeft have been set to the id of this component
    to prevent the dummy from receiving focus again -->
    <AutoCompleteTextView android:id="@+id/autotext"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:nextFocusUp="@id/autotext" 
        android:nextFocusLeft="@id/autotext"/>
    
    0 讨论(0)
  • 2020-11-21 07:30

    Is the actual problem that you just don't want it to have focus at all? Or you don't want it to show the virtual keyboard as a result of focusing the EditText? I don't really see an issue with the EditText having focus on start, but it's definitely a problem to have the softInput window open when the user did not explicitly request to focus on the EditText (and open the keyboard as a result).

    If it's the problem of the virtual keyboard, see the AndroidManifest.xml <activity> element documentation.

    android:windowSoftInputMode="stateHidden" - always hide it when entering the activity.

    or android:windowSoftInputMode="stateUnchanged" - don't change it (e.g. don't show it if it isn't already shown, but if it was open when entering the activity, leave it open).

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

    using the information provided by other posters, I used the following solution:

    in the layout XML

    <!-- Dummy item to prevent AutoCompleteTextView from receiving focus -->
    <LinearLayout
        android:id="@+id/linearLayout_focus"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_width="0px"
        android:layout_height="0px"/>
    
    <!-- AUTOCOMPLETE -->
    <AutoCompleteTextView
        android:id="@+id/autocomplete"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dip"
        android:inputType="textNoSuggestions|textVisiblePassword"/>
    

    in onCreate()

    private AutoCompleteTextView mAutoCompleteTextView;
    private LinearLayout mLinearLayout;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);
    
        //get references to UI components
        mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
        mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout_focus);
    }
    

    and finally, in onResume()

    @Override
    protected void onResume() {
        super.onResume();
    
        //do not give the editbox focus automatically when activity starts
        mAutoCompleteTextView.clearFocus();
        mLinearLayout.requestFocus();
    }
    
    0 讨论(0)
  • 2020-11-21 07:30

    The simplest thing I did is to set focus on another view in onCreate:

        myView.setFocusableInTouchMode(true);
        myView.requestFocus();
    

    This stopped the soft keyboard coming up and there was no cursor flashing in the EditText.

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

    You can just set "focusable" and "focusable in touch mode" to value true on the first TextView of the layout. In this way when the activity starts the TextView will be focused but , due to its nature, you will see nothing focused on the screen and ,of course, there will be no keyboard displayed...

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

    If you have another view on your activity like a ListView, you can also do:

    ListView.requestFocus(); 
    

    in your onResume() to grab focus from the editText.

    I know this question has been answered but just providing an alternative solution that worked for me :)

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