Andriod Spinner, andriodspinner
<? Xml version = "1.0" encoding = "UTF-8"?> <LinearLayout android: orientation = "vertical" android: layout_height = "fill_parent" android: layout_width = "fill_parent" xmlns: android: "http://schemas.android.com/apk/res/android"> <TextView android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: text = "drop-down box 1:" android: id = "@ + id/label"/> <Spinner android: layout_height = "wrap_content" android: layout_width = "150dip" android: id = "@ + id/spinner1" android: drawselectid Top = "false"/> <TextView android: layout_height = "wrap_content" android: layout_width = "fill_parent" android: text = "drop-down box 2:" android: id = "@ + id/label"/> <Spinner android: layout_height = "wrap_content" android: layout_width = "150dip" android: id = "@ + id/spinner2" android: drawselectid Top = "false"/> <Button android: layout_height = "wrap_content" android: layout_width = "wrap_content" android: text = "@ string/OK" android: id = "@ + id/OK"/> </LinearLayout>
Package com. example. yanlei. mytk; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. view. view; import android. widget. adapterView; import android. widget. arrayAdapter; import android. widget. button; import android. widget. spinner; import java. util. arrayList; import java. util. list; public class MainActivity extends AppCompatActivity {private Spinner spinner1; private Spinne R spinner2; private Button OK; private ArrayAdapter countiesAdapter; private String [] mCounties = {"beijing", "guangdong", "guangxi", "hunan "}; private List <String> allCounties = new ArrayList <String> (); private String result = "you choose:"; @ Override protected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stub super. onCreate (savedInstanceState); setContentView (R. layout. acti Vity_main); spinner1 = (Spinner) findViewById (R. id. spinner1); spinner2 = (Spinner) findViewById (R. id. spinner2); OK = (Button) findViewById (R. id. OK); for (int I = 0; I <mCounties. length; I ++) {allCounties. add (mCounties [I]);} countiesAdapter = new ArrayAdapter <String> (this, android. r. layout. simple_spinner_item, allCounties); countiesAdapter. setDropDownViewResource (android. r. layout. simple_spinner _ Dropdown_item); spinner1.setAdapter (countiesAdapter); ArrayAdapter adapter = ArrayAdapter. createFromResource (this, R. array. counties, android. r. layout. simple_spinner_item); adapter. setDropDownViewResource (android. r. layout. simple_spinner_dropdown_item); spinner2.setAdapter (adapter); // when you click the first drop-down button, the selected value is displayed. Spinner1.setOnItemSelectedListener (new AdapterView. OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView <?> Adapter, View view, int position, long id) {// TODO Auto-generated method stub String str = (String) spinner1.getAdapter (). getItem (int) id); setTitle (result + str) ;}@ Override public void onNothingSelected (AdapterView <?> Arg0) {// TODO Auto-generated method stub}); // when you click the second drop-down button, the selected value is displayed. Spinner2.setOnItemSelectedListener (new AdapterView. OnItemSelectedListener () {@ Override public void onItemSelected (AdapterView <?> Adapter, View view, int position, long id) {String str = (String) spinner2.getAdapter (). getItem (position); setTitle (result + str) ;}@ Override public void onNothingSelected (AdapterView <?> Arg0) {}}); // Click OK to extract the selected value. OK. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {setTitle (result + spinner1.getSelectedItem () + "->" + spinner2.getSelectedItem ());}});}}
Arrays. xml
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="counties"> <item>AAA</item> <item>BBB</item> <item>CCC</item> <item>DDD</item> <item>EEE</item> </string-array></resources>