09 - android Spinner 下拉列表
目标:
? ? ? 掌握下拉列表Spinner的使用;
? ? ? 可以通过程序配置Spinner显示内容;
? ? ? 可以通过配置文件配置Spinner显示内容
通过本程序就应该发现,Spinner的核心问题就在于下啦数据内容的显示上。
下拉列表框也是一种常见的图形组件,它可以为用户提供列表的选择方式,与复选框或单选按钮相比还可以节省手机屏幕空间,在android中可以使用android.widget.Spinner类实现:
<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: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_name"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:entries="@array/city_labels"
? ? ? ? />
</LinearLayout>
values包下面增加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>
?
方法二:通过android.widget.ArrayAdapter类
ArrayAdapter类的功能:有两个主要的功能:读取资源文件中定义的列表项,或者通过List集合设置列表项,此类中定义了如下几个常用的方法:
即:如果要使用ArrayAdapter配置下拉列表的内容,可以采用配置文件完成,为了演示以上两种实现形式,下面先采用配置文件读取方式完成。
例如:定义表示颜色下拉框:
? ? ? ?新建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>
这个配置文件将在ArrayAdapter类中进行读取。为了能够使用ArrayAdapter还需要定义spinner;
main.xml
?
?
? ? <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"/>
现在文件中不再添加任何信息,只是定义空的下拉列表框,而后在Activity程序中,要动态的配置
?
ArrayAdapterextends BaseAdapter<!-- end header -->
java.lang.Object????android.widget.BaseAdapter????android.widget.ArrayAdapter<T>
?
?
?
?
Creates a new ArrayAdapter from external resources. The content of the array is obtained through getTextArray(int).
?
用该方法读取所需要的信息
Activity类操作:
?
package com.sun.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class HelloWorld extends Activity {
private Spinner spiColor = null;// 表示要读取的颜色列表框
private ArrayAdapter<CharSequence> adapterColor = null;// 所有的数据都是String
/** Called when the activity is first created. */
@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.city_labels, android.R.layout.simple_spinner_item);// 实例化ArrayAdapter
this.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//换个风格
this.spiColor.setAdapter(this.adapterColor);//设置显示信息
}
}
?
? ? 可以对于ArrayAdapter而言,除了读取资源文件之外,还可能需要通过程序动态生成,所以现在可以使用ArrayAdapter的另一种形式
? ?部分信息之后再慢慢写
?
?
?
?
?