Spinner的用法_1
1 package com.example.spinner_project; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.view.Menu; 6 import android.view.View; 7 import android.widget.AdapterView; 8 import android.widget.AdapterView.OnItemSelectedListener; 9 import android.widget.Spinner; 10 import android.widget.Toast; 11 12 public class MainActivity extends Activity { 13 14 //创建spinner对象 15 Spinner sp1; 16 Spinner sp2; 17 @Override 18 protected void onCreate(Bundle savedInstanceState) { 19 super.onCreate(savedInstanceState); 20 setContentView(R.layout.activity_main); 21 sp1=(Spinner) findViewById(R.id.sp_1); 22 sp2=(Spinner) findViewById(R.id.sp_2); 23 /*@Param String[] str1 24 * 获取spinner1中的选项数组,注意getresource()方法的使用范围 25 * String[] str2 26 * 获取spinner1中的选项数组 27 *获取sp1的setOnItemSelectedListener监听对象以此来获取spinner选择的内容 28 * */ 29 final String[] str1=this.getResources().getStringArray(R.array.spinner_array1); 30 final String[] str2=this.getResources().getStringArray(R.array.spinner_array2); 31 sp1.setOnItemSelectedListener(new OnItemSelectedListener() { 32 33 /* 34 * @Param AdapterView<?> parent 是Spinnner对象 35 * View view 是视图对象可以直接获取选择的内容 36 * int position, long id 均是所选内容的位置或者id 37 * */ 38 @Override 39 public void onItemSelected(AdapterView<?> parent, View view, 40 int position, long id) { 41 Toast.makeText(MainActivity.this,"选择的班级是"+str1[(int) id], Toast.LENGTH_LONG).show(); 42 43 } 44 45 @Override 46 public void onNothingSelected(AdapterView<?> parent) { 47 // TODO Auto-generated method stub 48 49 } 50 51 }); 52 sp2.setOnItemSelectedListener(new OnItemSelectedListener() { 53 54 /* 55 * @Param AdapterView<?> parent 是Spinnner对象 56 * View view 是视图对象可以直接获取选择的内容 57 * int position, long id 均是所选内容的位置或者id 58 * */ 59 @Override 60 public void onItemSelected(AdapterView<?> parent, View view, 61 int position, long id) { 62 Toast.makeText(MainActivity.this,"选择的人数是"+str2[(int) id], Toast.LENGTH_LONG).show(); 63 64 } 65 66 @Override 67 public void onNothingSelected(AdapterView<?> parent) { 68 // TODO Auto-generated method stub 69 70 } 71 72 }); 73 } 74 75 76 77 } 78 //xml文件、、、、、、、、、、、、 79 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 80 xmlns:tools="http://schemas.android.com/tools" 81 android:layout_width="match_parent" 82 android:layout_height="match_parent" 83 android:orientation="horizontal" 84 android:gravity="center"> 85 86 <TextView 87 android:layout_width="wrap_content" 88 android:layout_height="wrap_content" 89 android:text="班级" 90 /> 91 <Spinner 92 android:id="@+id/sp_1" 93 android:layout_width="100dp" 94 android:layout_height="wrap_content" 95 android:entries="@array/spinner_array1" /> 96 <TextView 97 android:layout_width="wrap_content" 98 android:layout_height="wrap_content" 99 android:text="人数" 100 /> 101 <Spinner 102 android:id="@+id/sp_2" 103 android:layout_width="100dp" 104 android:layout_height="wrap_content" 105 android:entries="@array/spinner_array2" /> 106 107 108 </LinearLayout> 109 ////////////////////////////string 110 <?xml version="1.0" encoding="utf-8"?> 111 <resources> 112 113 <string name="app_name">Spinner_Project</string> 114 <string name="action_settings">Settings</string> 115 <string name="hello_world">Hello world!</string> 116 117 <string-array name="spinner_array1"> 118 <item >安卓1期</item> 119 <item >安卓2期</item> 120 <item >安卓3期</item> 121 <item >安卓4期</item> 122 <item >安卓5期</item> 123 <item >安卓6期</item> 124 <item >安卓7期</item> 125 <item >安卓8期</item> 126 <item >安卓9期</item> 127 <item >安卓10期</item> 128 </string-array> 129 <string-array name="spinner_array2"> 130 <item >20人</item> 131 <item >20-30人</item> 132 <item >30-40人</item> 133 <item >40-50人</item> 134 <item >50-60人</item> 135 136 </string-array> 137 138 </resources>