This article describes the usage of the trash menu under Android spinner. Share it for your reference, as follows:
1. Use arrays as data source
1. In the layout layout file: declare a TextView control and a Spinner control, the code is as follows
<Spinner android: android:layout_width="fill_parent" android:layout_height="wrap_content"></Spinner>
2. Add the following code to the Activity file
import ; import ; import ; import ; import ; import ; import ; import ; public class SpinnerActivity extends Activity { private static final String[] m={"Type A","Type B","O-type","Type AB","other"}; private TextView view ; private Spinner spinner; private ArrayAdapter<String> adapter; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub (savedInstanceState); setContentView(); view = (TextView) findViewById(); spinner = (Spinner) findViewById(.Spinner01); //Connect optional content with ArrayAdapter adapter = new ArrayAdapter<String>(this,.simple_spinner_item,m); //Set the style of the drop-down list (.simple_spinner_dropdown_item); //Add adapter to spinner (adapter); //Add event Spinner event listening (new SpinnerSelectedListener()); //Set the default value (); } //Operate in array form class SpinnerSelectedListener implements OnItemSelectedListener{ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ("Your blood type is:"+m[arg2]); } public void onNothingSelected(AdapterView<?> arg0) { } } }
2. Use XML as the data source
1. Create a new file in the values folder: the code is as follows
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="plantes"> <item>NOKIA</item> <item>MOTO</item> <item>HTC</item> <item>LG</item> <item>other</item> </string-array> </resources>
2. Add the following code to the Activity file
import ; import ; import ; import ; import ; import ; import ; import ; public class SpinnerActivity extends Activity { private TextView view2; private Spinner spinner2; private ArrayAdapter adapter2; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub (savedInstanceState); setContentView(); spinner2 = (Spinner) findViewById(.spinner02); view2 = (TextView) findViewById(.spinnerText02); //Connect optional content with ArrayAdapter adapter2 = (this, , .simple_spinner_item); //Set the style of the drop-down list (.simple_spinner_dropdown_item); //Add adapter2 to spinner (adapter2); //Add event Spinner event listening (new SpinnerXMLSelectedListener()); //Set the default value (); } //Operate in XML class SpinnerXMLSelectedListener implements OnItemSelectedListener{ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { ("What kind of phone do you use:"+(arg2)); } public void onNothingSelected(AdapterView<?> arg0) { } } }
For more information about Android related content, please check out the topic of this site:Android programming activity operation skills summary》、《Android resource operation skills summary》、《Android file operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.