1.strings.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">S02_E01_SpinnerEtc</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="action_settings">Settings</string> 7 8 <string-array name="planets"> 9 <item>地球</item> 10 <item>火星</item> 11 <item>水星</item> 12 <item>木星</item> 13 </string-array> 14 </resources>
2.activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.s02_e01_spinneretc.MainActivity" > 10 11 <Spinner 12 android:id="@+id/mSpinner" 13 android:layout_width="match_parent" 14 android:layout_height="wrap_content"/> 15 16 </RelativeLayout>
3.item.xml自定义下拉项用
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:id="@+id/textViewId" 9 android:layout_width="fill_parent" 10 android:layout_height="wrap_content" 11 /> 12 </LinearLayout>
4.java
1 package com.example.s02_e01_spinneretc; 2 3 import android.annotation.SuppressLint; 4 import android.os.Bundle; 5 import android.support.v7.app.ActionBarActivity; 6 import android.view.Menu; 7 import android.view.MenuItem; 8 import android.view.View; 9 import android.widget.AdapterView; 10 import android.widget.AdapterView.OnItemSelectedListener; 11 import android.widget.ArrayAdapter; 12 import android.widget.Spinner; 13 14 @SuppressLint("NewApi") 15 public class MainActivity extends ActionBarActivity implements OnItemSelectedListener{ 16 17 private Spinner mSpinner; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 mSpinner = (Spinner) findViewById(R.id.mSpinner); 23 // Create an ArrayAdapter using the string array and a default spinner layout 24 ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets, android.R.layout.simple_spinner_item); 25 // Specify the layout to use when the list of choices appears 26 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 27 // Apply the adapter to the spinner 28 mSpinner.setAdapter(adapter); 29 mSpinner.setPrompt("测试"); 30 mSpinner.setOnItemSelectedListener(this); 31 } 32 33 @Override 34 public boolean onCreateOptionsMenu(Menu menu) { 35 // Inflate the menu; this adds items to the action bar if it is present. 36 getMenuInflater().inflate(R.menu.main, menu); 37 return true; 38 } 39 40 @Override 41 public boolean onOptionsItemSelected(MenuItem item) { 42 // Handle action bar item clicks here. The action bar will 43 // automatically handle clicks on the Home/Up button, so long 44 // as you specify a parent activity in AndroidManifest.xml. 45 int id = item.getItemId(); 46 if (id == R.id.action_settings) { 47 return true; 48 } 49 return super.onOptionsItemSelected(item); 50 } 51 52 @Override 53 public void onItemSelected(AdapterView<?> parent, View view, int position, 54 long id) { 55 System.out.println(parent.getItemAtPosition(position)); 56 57 } 58 59 @Override 60 public void onNothingSelected(AdapterView<?> parent) { 61 System.out.println(parent); 62 63 } 64 }
5.自定义下拉源
1 package mars.spinner; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import android.app.Activity; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.widget.AdapterView; 10 import android.widget.ArrayAdapter; 11 import android.widget.Spinner; 12 import android.widget.AdapterView.OnItemSelectedListener; 13 14 public class MainActivity extends Activity { 15 /** Called when the activity is first created. */ 16 //声明Spinner对象 17 private Spinner spinner = null; 18 19 @Override 20 public void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.main); 23 //通过ID,得到Spinner对象 24 spinner = (Spinner) findViewById(R.id.spinnerId); 25 List<String> list = new ArrayList<String>(); 26 list.add("test1"); 27 list.add("test2"); 28 //调用ArrayAdapter的构造函数来创建ArrayAdapter对象 29 //第一个参数是指上下文对象 30 //第二个参数指定了下拉菜单当中每一个条目的样式 31 //第三个参数指定了TextView控件的ID 32 //第四个参数为整个列表提供数据 33 ArrayAdapter adapter = new ArrayAdapter(this,R.layout.item,R.id.textViewId,list); 34 //通过createFromResource方法创建一个ArrayAdapter对象 35 //第一个参数是指上下文对象 36 //第二参数引用了在strings.xml文件当中定义的String数组 37 //第三个参数是用来指定Spinner的样式,是一个布局文件ID,该布局文件由Android系统提供,也可替换为自己定义的布局文件 38 /*ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( 39 this, R.array.planets_array, 40 android.R.layout.simple_spinner_item); 41 */ //设置Spinner当中每个条目的样式,同样是引用一个Android系统提供的布局文件 42 //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 43 44 spinner.setAdapter(adapter); 45 spinner.setPrompt("测试"); 46 //为spinner对象绑定监听器 47 spinner.setOnItemSelectedListener(new SpinnerOnSelectedListener()); 48 } 49 //这个监听器主要用来监听用户选择列表的动作 50 class SpinnerOnSelectedListener implements OnItemSelectedListener{ 51 52 //当用户选定了一个条目时,就会调用该方法 53 @Override 54 public void onItemSelected(AdapterView<?> adapterView, View view, int position, 55 long id) { 56 String selected = adapterView.getItemAtPosition(position).toString(); 57 System.out.println(selected); 58 } 59 60 @Override 61 public void onNothingSelected(AdapterView<?> adapterView) { 62 // TODO Auto-generated method stub 63 System.out.println("nothingSelected"); 64 } 65 66 } 67 }