Spinner
Spinner 是从多个选项中选择一个选项的控件,
类似于桌面程序中的组合框(ComboBox)
********
android:spinnerMode="dropdown" //制定模型 下拉选项 和对话框选项模式
android:entries="@array/week_en" //用于设置Spinner的显示内容
属性值:要求传递的是一个资源字符串数组的id
布局文件:
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" >
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="请选择星期:"
/>
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"
/>
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"
/>
@array/week_cn 对应的定义在
res/values/strings.xml中
KN_Day05_1_Spinner
Hello world!
Settings
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
- Sunday
- 周一
- 周二
- 周三
- 周四
- 周五
- 周六
- 周日
***************************************
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
provinceList = new ArrayList();
provinceList.add("江苏");
provinceList.add("浙江");
//创建适配器
ArrayAdapter provinceadapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item,
provinceList);
//设置下拉样式 为下拉
provinceadapter.
setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(provinceadapter);
ArrayList
cityList1 = new ArrayList();
cityList1.add("南京市");
cityList1.add("苏州市");
cityList1.add("无锡市");
ArrayList
cityList2 = new ArrayList();
cityList2.add("杭州市");
cityList2.add("宁波市");
cityList2.add("绍兴市");
final ArrayAdapter
adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, cityList1);
final ArrayAdapter
adapter2 = new ArrayAdapter(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:
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" >
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="请选择城市:"
/>
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"
/>
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"
/>