Spinner
Spinner 是从多个选项中选择一个选项的控件,
类似于桌面程序中的组合框(ComboBox)
********
android:spinnerMode="dropdown" //制定模型 下拉选项 和对话框选项模式
android:entries="@array/week_en" //用于设置Spinner的显示内容
属性值:要求传递的是一个资源字符串数组的id
布局文件:
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android"
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kn_day05_1_spinner.MainActivity" >
<TextView android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="28sp"
android:text="请选择星期:"
/>
<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:spinnerMode="dropdown"
android:entries="@array/week_en"
/>
<Spinner android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:spinnerMode="dialog"
android:entries="@array/week_cn"
/>
</RelativeLayout>
@array/week_cn 对应的定义在
res/values/strings.xml中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">KN_Day05_1_Spinner</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<!-- 通过string-array标签创建字符串数组
通过name属性给数组赋予名字
通过在string-array标签中添加item标签来添加数组元素
每一个item代表一个元素
-->
<string-array name="week_en">
<item >Monday</item>
<item >Tuesday</item>
<item >Wednesday</item>
<item >Thursday</item>
<item >Friday</item>
<item >Saturday</item>
<item >Sunday</item>
</string-array>
<string-array name="week_cn">
<item >周一</item>
<item >周二</item>
<item >周三</item>
<item >周四</item>
<item >周五</item>
<item >周六</item>
<item >周日</item>
</string-array>
</resources>
***************************************
package com.example.kn_day05_1_spinner;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
/**
* 功能:在页面中添加2个spinner,实现省市联动效果
* 一个spinner中存储省份名字,一个spinner中存储城市名字,
* 当某省份被选择后,第二个sipnner中对应显示该省份下的所有城市
*
* ArrayAdapter 适配器的绑定使用
* @author KNOWN
*
*/
public class MainActivity extends Activity {
Spinner sp1;//省
Spinner sp2;//市
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp1 = (Spinner)findViewById(R.id.spinner1);
sp2 = (Spinner)findViewById(R.id.spinner2);
ArrayList<String>
provinceList = new ArrayList<String>();
provinceList.add("江苏");
provinceList.add("浙江");
//创建适配器
ArrayAdapter<String> provinceadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
provinceList);
//设置下拉样式 为下拉
provinceadapter.
setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(provinceadapter);
ArrayList<String>
cityList1 = new ArrayList<String>();
cityList1.add("南京市");
cityList1.add("苏州市");
cityList1.add("无锡市");
ArrayList<String>
cityList2 = new ArrayList<String>();
cityList2.add("杭州市");
cityList2.add("宁波市");
cityList2.add("绍兴市");
final ArrayAdapter<String>
adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cityList1);
final ArrayAdapter<String>
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, cityList2);
/**
* 设置spinner子选项选中事件
*/
sp1.s
etOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void
onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
/
/id spinner子选项的序号 从0开始
if(id == 0){
sp2.setAdapter(adapter1);
}else if (id == 1) {
sp2.setAdapter(adapter2);
}
Log.i("==当前选中的省是:", sp1.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Log.i("==当前选中的市是:", sp2.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
********************
布局文件
activity_main.xml:
<RelativeLayout xmlns:android=" http://schemas.android.com/apk/res/android"
xmlns:tools=" http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.kn_day05_1_spinner.MainActivity" >
<TextView android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="28sp"
android:text="请选择城市:"
/>
<Spinner android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview1"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:spinnerMode="dropdown"
android:entries="@array/week_en"
/>
<Spinner android:id="@+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:spinnerMode="dropdown"
android:entries="@array/week_cn"
/>
</RelativeLayout>