当我们在Android界面上需要从多个选项中选择出一项的时候,通常由于手机屏幕尺寸的限制,我们不可能也不应该将所有的选项都罗列在手机屏幕上。这个时候就需要用到Android组件——下拉列表(Spinner)。
1.Spinner的配置
首先需要在activity_main.xml中配置Spinner,通过<Spinner>结点配置。
<?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="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<!--添加的Spinner组件-->
<Spinner
android:id="@+id/mycity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
但是在activity_main.xml不能直接设置其显示的列表项。关于下来列表项,有以下两种方式。
(1) 直接通过资源文件配置。
(2)通过android.widget.ArrayAdapter类读取资源文件或者是指定具体设置的数据。
1.直接在资源文件张配置。
可以直接在values文件夹中定义若干个资源文件。如果要定义一个天气信息的下拉列表选此,就可以定义一个weather.xml。
weathe.xml文件,需要定义一个string-array,Spinner可以通过string-array找到weather。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="weather">
<item >晴</item>
<item >阴</item>
<item >雨</item>
</string-array>
</resources>
此处就配置完成了一个下拉选项的内容,如果想要把这个下拉内容加入到UI中,可以通过<Spinner>结点配置。
<Spinner
android:id="@+id/mycity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:entries="@array/weather"
/>
下拉列表文件是通过
android:entries属性的值,加载相应的下拉列表内容。下图为运行后的图形显示。
2.通过ArrayAdapter类读取资源文件。
配置文件将在ArrayAdapter中读取。
在main_activity.xml中定义一个空的Spinner(不用为其配置android:entries属性),在MainActivity.java中读取配置信息。
<TextView
android:id="@+id/info_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择您喜欢的颜色。" />
<Spinner
android:id="@+id/mycolor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
在values文件夹下定义一个color.xml文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="color">
<item >红</item>
<item >绿</item>
<item >蓝</item>
</string-array>
</resources>
在MainActivity.java中可以通过ArrayAdapter类来配置Spinner。其代码后面将一并给出。
在MainActivity中还有第三种的配置方式,在activi_main.xml中配置一个空的Spinner,同于上面的操作。将下拉列表都写在MainActivity.java代码中。
<TextView
android:id="@+id/info_edu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择你的学历。" />
<!-- 定义了一个空的Spinner -->
<Spinner
android:id="@+id/myedu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
其列表内容设置将在MainActivity.java中配置。
package com.example.spinnerproject;
import java.util.ArrayList;
import java.util.List;
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 MainActivity extends Activity {
private Spinner spinColor = null; //定义下拉列表框
private Spinner spinEdu = null;
private ArrayAdapter<CharSequence> adapterColor = null; //所有的数据都是String类型
private ArrayAdapter<CharSequence> adapterEdu = null;
private List<CharSequence> dataEdu = null; //定义一个数据集合
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.spinColor = (Spinner)super.findViewById(R.id.mycolor); //取得颜色的下拉框。
this.spinColor.setPrompt("请选择您喜欢的颜色:");//设置提示信息。
//实例化了adapterColor,加载了color.xml中的内容,发现string-array中的内容<string-array name="color">出现在了R.array中。
this.adapterColor = ArrayAdapter.createFromResource(this,
R.array.color,android.R.layout.simple_spinner_item);
// 换个风格,使每行之间的空隙增加。通过ArrayAdapter来控制下拉列表的样式。
this.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//将Spinner和ArrayAdapter联系起来了。
this.spinColor.setAdapter(this.adapterColor); //设置显示信息。
//用集合的方式类配置下拉列表的各项内容。
this.spinEdu = (Spinner) super.findViewById(R.id.myedu);//取得下拉框。
//配置List集合包装的下拉框内容。
this.dataEdu = new ArrayList<CharSequence>();
this.dataEdu.add("大学生");
this.dataEdu.add("研究生");
this.dataEdu.add("高中");
//设置下拉列表的提示信息。
this.spinEdu.setPrompt("请选择你的学历");
//将数据集合和Spinner联系起来,准备好下拉列表款的内容。
this.adapterEdu = new ArrayAdapter<CharSequence>(this,
android.R.layout.simple_spinner_item,this.dataEdu);//simple_spinner_item按照简单的方式显示出来
this.adapterEdu.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//换个风格,使每行之间的空隙增加
//设置显示信息。
this.spinEdu.setAdapter(this.adapterEdu);
this.spinEdu.setOnItemSelectedListener(new OnItemSelectedListener() { //对spinEdu下拉列表设置监听事件。
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String str = parent.getItemAtPosition(position).toString(); //返回点击的下拉选项的内容。
Toast.makeText(MainActivity.this, "你点击的是:"+str, 2000).show(); //点击后,持续显示2秒
}
@Override
public void onNothingSelected(AdapterView<?> arg0) { //不用实现。
// TODO Auto-generated method stub
}
});
}
}
下面是该代码运行时的截图:
整个程序实现了Spinner的三种操作方式,并对下拉列表的单击事件进行了监听。