Android中Spinner控件及级联Spinner的使用

本文介绍了如何在Android中使用Spinner控件,并实现级联效果。通过设置entries、dropDownWidth、dropDownSelector和popupBackground属性来定制Spinner。内容包括将省份、城市、地区数据存入数组,并实现当选择某一省时,只显示对应城市的级联选择。示例展示了布局文件和级联选择的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下拉框是Android中一个较为常用的控件,用于从下拉选项中选出一个选项,这种方式可以很好的节省控件,如中国32个省市和地区,如果挨个放在屏幕里不美观且占位置。

它有如下属性

entries:设置显示数组数据
dropDownWidth:设置下拉列表的宽度
dropDownSelector:设置Spinner组件的下拉框被选择的动态效果
popupBackground:下拉框的背景

一般不采用entries属性来为它准备可选内容,内容条目较少可以使用。内容较多或数据是从本地和网络来就需要使用适配器,通过适配器将其中的条目添加进去,同时在Java代码中也易于控制,比如我下面要写的级联Spinner。

首先将三组数据存放到三个数组中,一维数组存放省份,二维数组存放城市,三维数组(Java中没有三维的称谓,一般叫多维,这里正好三维)存放地区,这三个数组的数字对应的,省、市、区是相互对应的,如某某省某某市某某区,很明显选了某个省就不能选择其他省的城市,选了某个城市就不能选其他城市的地区。下面是一个简单的实现。

布局文件如下

<LinearLayout 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:orientation="vertical"
    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.briup.spinner.MainActivity" >

    <Spinner
        android:id="@+id/shen_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/cheng_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />

    <Spinner
        android:id="@+id/qu_sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:textSize="30sp" />

</LinearLayout>
MainActivity.class类文件如下

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.TextView;

public class MainActivity extends Activity {
	private Spinner shenSp,chengSp,quSp;
	private TextView text;
	//数组适配器,可以将简单的数组添加到列表控件中
	private ArrayAdapter<String> shenAdapter,chengAdapter,quAdapter;
	private String[] sheng = {"山西","河南","江苏"};
	private String[][] cheng = {{"太原","大同"},{"郑州","平顶山"},{"南京","苏州"}};
	private String[][][] qu = {
			{{"战争学院","祖安"},{"皮尔吉沃特","暗影岛"}},
			{{"班德尔","皮尔特洛夫"},{"黑色玫瑰","诺克萨斯"}},
			{{"德玛西亚","艾欧尼亚"},{"影流","皮城"}},
	};
	private int shengPosition,chengPosition;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		text = (TextView) findViewById(R.id.text);
		shenSp = (Spinner) findViewById(R.id.shen_sp);
		chengSp = (Spinner) findViewById(R.id.cheng_sp);
		quSp = (Spinner) findViewById(R.id.qu_sp);
		//将数组中的数据添加到适配器,布局为Android自带的一种布局,当然数据是挨个存放的
		//Android为Spinner自带的布局是simple_spinner_item,比较窄,这里不用
		//省份,默认为第一个数据
		shenAdapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_selectable_list_item, sheng);
		//这条语句是为下拉框设置样式,Android自带的样式比较窄,方便观察暂时不用
		//shenAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		//城市,默认为第一组数据
		chengAdapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_selectable_list_item, cheng[0]);
		//地区,默认为第一组的第一组数据
		quAdapter = new ArrayAdapter<String>(this, 
				android.R.layout.simple_selectable_list_item, qu[0][0]);
		//为所有Spinner添加适配器
		shenSp.setAdapter(shenAdapter);
		chengSp.setAdapter(chengAdapter);
		quSp.setAdapter(quAdapter);
		
		shenSp.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				shengPosition = arg2;//记录第一个Spinner即省份选择的位置
				//选中了省份,就得限制城市,重新设定适配器,改变其中显示的数据
				chengAdapter = new ArrayAdapter<String>(MainActivity.this, 
						android.R.layout.simple_selectable_list_item, cheng[arg2]);
				chengSp.setAdapter(chengAdapter);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
		chengSp.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				chengPosition = arg2;//记录第二Spinner即城市选中的位置
				//同理,选中了城市,就得限制地区
				quAdapter = new ArrayAdapter<String>(MainActivity.this, 
						android.R.layout.simple_selectable_list_item, qu[shengPosition][arg2]);
				quSp.setAdapter(quAdapter);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
		quSp.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				text.setText("省份:"+sheng[shengPosition]+
						"\n城市:"+cheng[shengPosition][chengPosition]+
						"\n地区:"+qu[shengPosition][chengPosition][arg2]);
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
	}
}
效果如下

未选择省市区也能将默认选项输出


点击下拉框,出现可选项


选中某个省,还未选择市区就已经改变,这就是级联


同样,选择其他两项结果也是有变化的,都是按照预设输出信息


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值