When registering an account on a website, the website will ask us to provide information such as gender, birthday, and city. for convenience, a drop-down list is provided for us to choose from, this function is also available in Android. this is the Spinner drop-down list. When an account is registered on a website, the website will ask us to provide information such as gender, birthday, and city. for convenience, A drop-down list is provided for us to choose from. This function is also available in Android, which is the Spinner drop-down list.
At the same time of encoding, you must first set the Spinner component in the layout, then connect the optional content through the ArrayAdapter and the drop-down list, and finally obtain the selected options, we need to design the event listener setOnItemSelectedListener and implement onItemSelected to get the content selected by the user. Finally, set the current display item through setVisibility.
SpinnerTest. java
Package org. hualang. spinner; import android. app. activity; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. spinner; import android. widget. textView; public class SpinnerTest extends Activity {/** Called when the activity is first created. */private static final String [] citys = {"Hangzhou", "Beijing", "Chengdu", "Dalian", "Shenzhen", "Nanjing"}; private TextView text; private Spinner spinner; private ArrayAdapter
Adapter; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); text = (TextView) findViewById (R. id. text); spinner = (Spinner) findViewById (R. id. spinner); // connect the available content to the ArrayAdapter adapter. adapter = new ArrayAdapter
(This, android. r. layout. simple_spinner_item, citys); // sets the style adapter of the drop-down list. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); // add the adapter to the spinner. setAdapter (adapter); // add a Spinner event to listen to the spinner. setOnItemSelectedListener (new Spinner. onItemSelectedListener () {@ Override public void onItemSelected (AdapterView
Arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stub text. setText ("your city is:" + citys [arg2]); // you can set arg0.setVisibility (View. VISIBLE) ;}@ Override public void onNothingSelected (AdapterView
Arg0) {// TODO Auto-generated method stub }});}}
Main. xml
Running result:
The above is the content of the Android UI control series: Spinner (drop-down list). For more information, see PHP Chinese network (www.php1.cn )!