Spinner的三级联动

(采用ArrayAdapter)
使用entries设置显示数组数据
android:dropDownWidth:设置下拉列表的宽度
android:dropDownSelector:设置Spinner组件的下拉框被选择的动态效果属性
android:popupBackground:下拉框的背景
使用ArrayAdapter为其设置相应数据、Spinner样式及下拉窗口样式等内容
android.R.layout.simple_spinner_item:设置spinner的样式
adapter.setDropDownViewResouce(android.R.simple_spinner_dropdown_item):设置下拉框的样式
添加setOnItemSelectedListener(OnItemSelectedListener)事件监听器

package com.xspacing.spinners;

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;
/**
 * 
 * @ClassName MainActivity.java
 * @Description 级联Spinner
 * @author Smile
 * @version v1.0
 * @date 2016年9月23日 
 */
public class MainActivity extends Activity {

    private Spinner provinceSpinner;
    private Spinner citySpinner;
    private Spinner areaSpinner;

    private String province[] = { "山西", "江苏", "河南" };
    private String city[][] = { { "太原", "大同" }, { "南京", "苏州" }, { "平顶山", "郑州" } };
    private String Area[][][] = { { { "迎泽区", "小店区" }, { "市区", "矿区" } }, { { "玄武区", "中山区" }, { "姑苏区", "相城区" } },
            { { "新华区", "山区区" }, { "开发区", "高新区" } } };
    private ArrayAdapter provinceAdapter;
    private ArrayAdapter areaAdapter;
    private ArrayAdapter cityAdapter;

    private int provicePosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }

    private void initViews() {
        provinceSpinner = (Spinner) findViewById(R.id.province_sp);
        citySpinner = (Spinner) findViewById(R.id.city_sp);
        areaSpinner = (Spinner) findViewById(R.id.area_sp);
        provinceAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, province);
        cityAdapter = setCityAdapter(0);
        areaAdapter = setAreaAdapter(0, 0);
        provinceSpinner.setAdapter(provinceAdapter);
        citySpinner.setAdapter(cityAdapter);
        areaSpinner.setAdapter(areaAdapter);

        provinceSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                citySpinner.setAdapter(setCityAdapter(position));
                provicePosition = position;
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });

        citySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                areaSpinner.setAdapter(setAreaAdapter(provicePosition, position));
            }

            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });

    }

    public ArrayAdapter setCityAdapter(int position) {
        return new ArrayAdapter(this, android.R.layout.simple_list_item_1, city[position]);
    }

    public ArrayAdapter setAreaAdapter(int position, int pre) {
        return new ArrayAdapter(this, android.R.layout.simple_list_item_1, Area[position][pre]);
    }

}
<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"
    tools:context="com.xspacing.spinners.MainActivity" >

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

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

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

LinearLayout>

你可能感兴趣的:(android,android)