Android基本控件---Spinner
2014-05-18 18:05
387 查看
一.定义表示城市的资源文件
二.定义表示颜色信息的资源文件
三.定义下拉列表框——layout/main.xml
四.定义提示信息——values/strings.xml
五.编写Activity程序——MainActivity.java
显示效果:
下拉列表框中的列表项有两种配置方式:
方式一:
通过资源文件配置,例如:定义values/city_data.xml文件,使用<string-array>元素制定数据内容。
方式二:
通过android.widget.ArrayAdapter类读取资源文件或指定具体设置的数据。
该类的两个主要功能:读取资源文件中定义的列表项或通过List集合设置列表项 。
<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 配置一个数组集合,使用android:entries="@array/city_labels"属性即可读取信息 --> <string-array name="city_labels"> <item>北京</item> <item>上海</item> <item>广州</item> </string-array> </resources>
二.定义表示颜色信息的资源文件
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="color_labels"> <item>红色</item> <item>绿色</item> <item>蓝色</item> </string-array> </resources>
三.定义下拉列表框——layout/main.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" > <!-- 定义文本显示组件,定义组件ID,组件的宽度为屏幕宽度,组件的高度为文字高度,组件的显示文字 --> <TextView android:id="@+id/info_city" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="请选择您喜欢的城市:" /> <!-- 定义下拉列表组件,定义组件的ID,提示信息,组件的宽度为文字宽度,组件的高度为文字高度,使用的文本资源 --> <Spinner android:id="@+id/mycity" android:prompt="@string/city_prompt" android:layout_width="wrap_content" 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="wrap_content" 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="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
四.定义提示信息——values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Spinner</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="city_prompt">请选择您喜欢的城市:</string> </resources>
五.编写Activity程序——MainActivity.java
package com.example.spinner; 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 MainActivity extends Activity { private Spinner spiColor = null; // 定义表示颜色的列表框 private Spinner spiEdu = null; // 定义表示学历的列表框 // 下拉列表内容适配器 private ArrayAdapter<CharSequence> adapterColor = null; private ArrayAdapter<CharSequence> adapterEdu = null; // 集合保存下拉列表选项 private List<CharSequence> dataEdu = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 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); // 设置列表显示风格 this.adapterColor .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); this.spiColor.setAdapter(this.adapterColor);// 设置下拉列表选项 this.dataEdu = new ArrayList<CharSequence>(); // 实例化List集合 // 设置选项内容 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); } }
显示效果:
下拉列表框中的列表项有两种配置方式:
方式一:
通过资源文件配置,例如:定义values/city_data.xml文件,使用<string-array>元素制定数据内容。
方式二:
通过android.widget.ArrayAdapter类读取资源文件或指定具体设置的数据。
该类的两个主要功能:读取资源文件中定义的列表项或通过List集合设置列表项 。
相关文章推荐
- Android基本控件之Spinner的使用
- Android开发笔记:基本控件之Spinner用法
- android基本控件示例两个Spinner下拉列表的关联(04)
- 【Android基础笔记04】Spinner 和 AutoCompleteTextView 基本适配器控件的使用
- <Android>控件Spinner的基本使用
- android自学第四天 Spinner 和 AutoCompleteTextView 基本适配器控件的使用
- android基本控件示例Spinner下拉列表(04)
- 一步一步学android之基本控件——Spinner组件
- Android第四讲——常用的基本控件(二)ListView、GridView、Spinner、AutoCompleteListView、Gallery、ExpandableListView
- Android Spinner控件
- Android UI控件Spinner控件的学习
- 【Android】学习笔记(3)——基本控件三
- webView控件基本使用(android)
- Android基本组件06(Spinner)
- android--Spinner(菜单下拉控件)
- Android[初级教程]第四篇 Spinner控件
- 【Android】学习笔记(1)——基本控件一
- Android控件开发之Spinner
- Android 基本控件 之 SeekBar(一) 个性你的视图
- Android 常用基本控件 ImageButton