android edittext with drop down list

后端 未结 4 1129
悲哀的现实
悲哀的现实 2021-02-02 13:21

I have an edittext which gets values from the user. I want to add an option which allows the user to choose from different options via drop down list when edittext is clicked. D

4条回答
  •  [愿得一人]
    2021-02-02 14:13

    I think this is your requirement:

    just follow this example: Xml:-

    
       
    
    
    

    Activity:-

    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    
    public class MainActivity extends Activity {
    
        String[] languages = { "C","C++","Java","C#","PHP","JavaScript","jQuery","AJAX","JSON" };
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
                    //Create Array Adapter
            ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_singlechoice, languages);
                    //Find TextView control
            AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.languages);
            //Set the number of characters the user must type before the drop down list is shown
                    acTextView.setThreshold(1);
                    //Set the adapter
            acTextView.setAdapter(adapter);
        }
    }
    

    Demo:-

    enter image description here

提交回复
热议问题