参考:http://imshare.iteye.com/blog/770950
获得被选中的选项的文本,Index, ID
1 2 3 |
String value = spinner.getSelectedItem().toString(); int index = spinner.getSelectedItemPosition(); long id = spinner.getSelectedItemId(); |
第一个下拉框是在java里面设定数据
第二个是在xml提供数据:res/values/arryas.xml
第三个 Spinner添加了OnItemSelectedListener事件,当单击下拉菜单后,将值带到上方的Text- View。上一个范例在new adapter时传入String数组,这次因为要添加及删除adapter,所以要传入的是ArrayList,否则,在添加删除时会出现错误。
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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>其他</item> </string-array> </resources> |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/spinnerText" android:layout_width="fill_parent" android:layout_height="wrap_content" > </TextView> <Spinner android:id="@+id/Spinner01" android:layout_width="fill_parent" android:layout_height="wrap_content" > </Spinner> <Spinner android:id="@+id/spinner02" android:layout_width="fill_parent" android:layout_height="wrap_content" > </Spinner> <Spinner android:id="@+id/spinner03" android:layout_width="fill_parent" android:layout_height="wrap_content" > </Spinner> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AddItem" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RemoverItem" /> </LinearLayout> |
java代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
package com.pandy; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class UI4Activity extends Activity { /** Called when the activity is first created. */ private static final String[] m={"A型","B型","O型","AB型","其他"}; private TextView view ; private Spinner spinner; private ArrayAdapter<String> adapter; private Spinner spinner2; private ArrayAdapter adapter2; private Button button1; private Button button2; private static List<String> list; private ArrayAdapter<String> adapter3; private Spinner spinner3; private int count = 1; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); view = (TextView) findViewById(R.id.spinnerText); spinner = (Spinner) findViewById(R.id.Spinner01); //将可选内容与ArrayAdapter连接起来 adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m); //设置下拉列表的风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //将adapter 添加到spinner中 spinner.setAdapter(adapter); //添加事件Spinner事件监听 spinner.setOnItemSelectedListener(new SpinnerSelectedListener()); //设置默认值 spinner.setVisibility(View.VISIBLE); spinner2 = (Spinner)findViewById(R.id.spinner02); adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner2.setAdapter(adapter2); spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener()); spinner2.setVisibility(View.VISIBLE); list = new ArrayList<String>(); for(String s : getResources().getStringArray(R.array.plantes)){ list.add(s); } adapter3 = new ArrayAdapter(this,android.R.layout.simple_spinner_item,list); adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner3 = (Spinner)findViewById(R.id.spinner03); spinner3.setAdapter(adapter3); spinner3.setVisibility(View.VISIBLE); button1 = (Button)findViewById(R.id.button1); button2 = (Button)findViewById(R.id.button2); button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub try { adapter3.add("Add Item "+(count++)); } catch (Exception e) { // TODO: handle exception Toast.makeText(UI4Activity.this, "Error.", Toast.LENGTH_LONG).show(); } } }); button2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub try { adapter3.remove(spinner3.getSelectedItem().toString()); } catch (Exception e) { // TODO: handle exception Toast.makeText(UI4Activity.this, "Error.", Toast.LENGTH_LONG).show(); } } }); } //使用数组形式操作 class SpinnerSelectedListener implements OnItemSelectedListener{ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { view.setText("你的血型是:"+m[arg2]); } public void onNothingSelected(AdapterView<?> arg0) { } } class SpinnerXMLSelectedListener implements OnItemSelectedListener{ public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { view.setText("你使用什么样的手机:"+adapter2.getItem(arg2)); } public void onNothingSelected(AdapterView<?> arg0) { } } } |