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

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

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

    in the layout XML

    
    
    
    
    
    

    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();
    }
    

提交回复
热议问题