---
title: "Andorid中的Spinner编写实例"
author: "keeponmoving"
date: "2012-05-09T23:09:52Z"
tags: []
categories: ["移动开发"]
slug: "andorid-spinner"
draft: false
lastmod: "2012-05-09T23:09:52Z"
id: 1521006
url: "https://keeponmoving.iteye.com/blog/1521006"
---

Spinner在android开发中也是用的比较多的一个控件，提供了类似于Web中一个下拉列表样式的输入控件。下面先给出一个比较简单的实例，这里我们没有自己去定义下来列表的布局，而是采用android系统提供的布局。效果图如下：

![](http://dl.iteye.com/upload/attachment/0067/8501/2f29fd7f-872c-3455-9630-20cf47e5216d.jpg)

android给Spinner控件提供了这样一个属性：android:entries="@array/books" 它可以指定Spinner的下拉选项，我们可以看出来它的资源是一个array数组。

layout的xml内容：

 

```
<?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" >

    <Spinner
        android:id="@+id/my_spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/books" />

</LinearLayout>
```

数组资源的xml代码：

```
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="books">
        <item >Windows</item>
        <item >mac os</item>
        <item >ubuntu</item>
        <item >aix</item>
        <item >Linux</item>
    </string-array>
    
</resources>
```

  下面activity代码，

 

```
public class SpinnerActivity extends Activity {
	private Spinner mySpinner;
	private myOnItemSelectedListener listener;
	// 注意String[]类型的数组资源不能在UI上定义String[],而是定义一个CharSequence[]
	private CharSequence[] os;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mySpinner = (Spinner) findViewById(R.id.my_spinner);
		listener = new myOnItemSelectedListener();
		// 定义一个CharSequence[] 去拿到String类型的数组
		os = getResources().getTextArray(R.array.books);
		mySpinner.setOnItemSelectedListener(listener);
	}

	private final class myOnItemSelectedListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
			Toast.makeText(getApplicationContext(), os[position], 1).show();
		}

		@Override
		public void onNothingSelected(AdapterView<?> parent) {
		}
	}
}
```

  这个spinner很简单的实现，在写代码的时候发现定义的数组资源在activity中要用CharSequence\[]类型，没有自己去定义下拉列表的，后面将会写出一个自定义item的实例。

下面我又重新自己写了一个自定义下拉列表的Spinner，因为实际开发中一般都是要自己定义下拉item才能满足需求的，上图先看效果：

![](http://dl.iteye.com/upload/attachment/0067/8499/87a58f57-9acd-3844-8de3-b124df5b575d.jpg)

 

因为程序代码很简单，所以下面只给出下拉列表item的布局和activity代码，首先是下拉item的xml：

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/spinner_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/check"
        android:contentDescription="@string/what_description" />

    <TextView
        android:id="@+id/tv_os"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>
```

 activity代码，这里我是采用一个继承自BaseAdapter的内部来自定义下拉item的布局的：

```
public class MySpinnerActivity extends Activity {
	private Spinner mySpinner;
	private myBaseAdapter adapter;
	private String[] os = new String[] { "windows", "mac os", "ubuntu", "linux", "android", "ios" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mySpinner = (Spinner) findViewById(R.id.myspinner);
		adapter = new myBaseAdapter();
		//mySpinner.setBackgroundColor(android.R.color.background_light);
		mySpinner.setAdapter(adapter);
	}

	private final class myBaseAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public myBaseAdapter() {
			super();
			mInflater = getLayoutInflater();
		}

		@Override
		public int getCount() {
			return os.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = mInflater.inflate(R.layout.spinner_item, null);
			ImageView iv_spinner = (ImageView) view.findViewById(R.id.spinner_image);
			iv_spinner.setImageResource(R.drawable.check);
			TextView tv_os = (TextView) view.findViewById(R.id.tv_os);
			tv_os.setText(os[position]);
			return view;
		}
	}
}
```

 

如上N多废话，只是自己学习的总结。