Spinner即下来列表,有点类似于Html中的<select>标签,对于Android来说,在有限的屏幕上,使用Spinner,可以极大提高用户的体验。Spinner位于 android.widget包下,每次只显示用户选中的元素,当用户再次点击时,会弹出选择列表供用户选择,而选择列表中的元素同样来自适配器。

下面我们同样通过一个例子来演示Spinner的使用。在这个例子中,我们有两个Spinner,一个供用户选择国家,另一个供用户选择城市,当国家变化时,城市也相应的变为选择的国家所对应的城市。

SpinnerActivity.java

package com.liuzhichao.widget;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class SpinnerActivity extends Activity {

	private Spinner countrySpinner;  //国家下拉框
	private Spinner citySpinner;    //城市下拉框
	private ArrayAdapter<CharSequence> cityAdapter;  //城市选项适配器
	private String countryString;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner);

		countrySpinner = (Spinner)findViewById(R.id.sp_country);
		citySpinner = (Spinner)findViewById(R.id.sp_city);

		//为下拉框设置事件
		countrySpinner.setOnItemSelectedListener(countrySpinnerListener);
		citySpinner.setOnItemSelectedListener(citySpinnerClickListener);

	}

	//国家下拉框事件处理
	private OnItemSelectedListener countrySpinnerListener = new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			int textArrayResId = R.array.array_city_cn;
			switch (position) {
			case 0:
				textArrayResId = R.array.array_city_cn;
				break;
			case 1:
				textArrayResId = R.array.array_city_us;
				break;
			case 2:
				textArrayResId = R.array.array_city_uk;
				break;
			default:
				break;
			}

			//将可选内容与ArrayAdapter连接起来
			cityAdapter = ArrayAdapter.createFromResource(getApplicationContext(), textArrayResId,
					android.R.layout.simple_spinner_item);

			 //设置下拉列表的风格
			cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			//将adapter 添加到spinner中
			citySpinner.setAdapter(cityAdapter);

			//保存选择的国家
			Spinner mSpinner = (Spinner)parent;
			countryString = mSpinner.getSelectedItem().toString();
		}

		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub
		}
	};

	//城市下拉框事件处理
	private OnItemSelectedListener citySpinnerClickListener = new OnItemSelectedListener() {

		public void onItemSelected(AdapterView<?> parent, View view,
				int position, long id) {
			Toast.makeText(getApplicationContext(), "您选择的国家为:"+countryString+"\n你选择的城市为:"+cityAdapter.getItem(position), Toast.LENGTH_LONG).show();
		}

		public void onNothingSelected(AdapterView<?> parent) {
			// TODO Auto-generated method stub

		}
	};

}

布局文件:

<?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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/spinner_country" />
    <!-- 城市下拉框,已经通过xml文件配置下拉选项数据源(android:entries) -->
    <Spinner
        android:id="@+id/sp_country"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/array_country"
        android:prompt="@string/spinner_country"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/spinner_city" />
    <!-- 城市下拉框  -->
    <Spinner
        android:id="@+id/sp_city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:prompt="@string/spinner_city"/>

</LinearLayout>

运行效果如下:

Spinner
Spinner

Spinner
Spinner