android edittext with drop down list

后端 未结 4 1128
悲哀的现实
悲哀的现实 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 13:55

    activity_main.xml

      
    
          
    
          
    
              
          
    
    
    

    MainActivity.java

    import android.os.Bundle;  
    import android.app.Activity;  
    import android.graphics.Color;  
    import android.view.Menu;  
    import android.widget.ArrayAdapter;  
    import android.widget.AutoCompleteTextView;  
    
    public class MainActivity extends Activity {  
        String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};  
        @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);  
    
            //Creating the instance of ArrayAdapter containing list of language names  
               ArrayAdapter adapter = new ArrayAdapter  
                (this,android.R.layout.select_dialog_item,language);  
            //Getting the instance of AutoCompleteTextView  
               AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
               actv.setThreshold(1);//will start working from first character  
               actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView  
               actv.setTextColor(Color.RED);  
    
        }  
    
        @Override  
        public boolean onCreateOptionsMenu(Menu menu) {  
            // Inflate the menu; this adds items to the action bar if it is present.  
            getMenuInflater().inflate(R.menu.activity_main, menu);  
            return true;  
        }  
    
    }  
    

提交回复
热议问题