Android学习之下拉列表

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string name="hello">Hello World, MySpinnerDemo!</string>
	<string name="app_name">下拉列表</string>
	<string name="city_prompt">请选择您喜欢的城市:</string>
</resources>

 

city_data.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="city_labels">
		<item>北京</item>
		<item>上海</item>
		<item>南京</item>
	</string-array>
</resources>

 

color_data.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="color_labels">
		<item>红色</item>
		<item>绿色</item>
		<item>蓝色</item>
	</string-array>
</resources>

 

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<TextView 
		android:id="@+id/info_city"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="请选择您喜欢的城市:" />
	<Spinner 
		android:id="@+id/mycity"
		android:prompt="@string/city_prompt"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:entries="@array/city_labels"/>
	<TextView 
		android:id="@+id/info_color"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="请选择您喜欢的颜色:" />
	<Spinner 
		android:id="@+id/mycolor"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
	<TextView 
		android:id="@+id/info_edu"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="请选择您的学历:" />
	<Spinner 
		android:id="@+id/myedu"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
</LinearLayout>

 

 MySpinnerDemo.java:

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MySpinnerDemo extends Activity {
	private Spinner spiColor = null; // 表示要读取的颜色列表框
	private Spinner spiEdu = null; // 定义下拉列表
	private ArrayAdapter<CharSequence> adapterColor = null; // 所有的数据都是String
	private ArrayAdapter<CharSequence> adapterEdu = null; // 所有的数据肯定是字符串
	private List<CharSequence> dataEdu = null; // 定义一个集合数据

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		this.spiColor = (Spinner) super.findViewById(R.id.mycolor); // 取得颜色的下拉框
		this.spiColor.setPrompt("请选择您喜欢的颜色:");
		this.adapterColor = ArrayAdapter.createFromResource(this,
				R.array.color_labels, android.R.layout.simple_spinner_item); // 实例化了ArrayAdapter
		this.adapterColor
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 换个风格
		this.spiColor.setAdapter(this.adapterColor); // 设置显示信息

		// 配置List集合包装的下拉框内容
		this.dataEdu = new ArrayList<CharSequence>();
		this.dataEdu.add("大学");
		this.dataEdu.add("研究生");
		this.dataEdu.add("高中");

		this.spiEdu = (Spinner) super.findViewById(R.id.myedu); // 取得下拉框
		this.spiEdu.setPrompt("请选择您喜欢的学历:");
		this.adapterEdu = new ArrayAdapter<CharSequence>(this,
				android.R.layout.simple_spinner_item, this.dataEdu); // 准备好下拉列表框的内容
		this.adapterEdu 
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 换个风格
		this.spiEdu.setAdapter(this.adapterEdu);

	}
}

你可能感兴趣的:(android,Spinner)