欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现三级联动下拉框 下拉列表spinner的实例代码

程序员文章站 2022-10-09 15:07:34
主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值       ...

主要实现办法:动态加载各级下拉值的适配器

在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值

Android实现三级联动下拉框 下拉列表spinner的实例代码   Android实现三级联动下拉框 下拉列表spinner的实例代码   Android实现三级联动下拉框 下拉列表spinner的实例代码

Android实现三级联动下拉框 下拉列表spinner的实例代码   Android实现三级联动下拉框 下拉列表spinner的实例代码   Android实现三级联动下拉框 下拉列表spinner的实例代码

xml布局:

复制代码 代码如下:

<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="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp" >
    <spinner
        android:id="@+id/spin_province"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <spinner
        android:id="@+id/spin_city"
        android:layout_width="112dp"
        android:layout_height="wrap_content" />

    <spinner
        android:id="@+id/spin_county"
        android:layout_width="100dp"
        android:layout_height="wrap_content" />
</linearlayout>


实现代码:

复制代码 代码如下:

package com.example.android.demo.spinner;

import android.os.bundle;
import android.app.activity;
import android.view.view;
import android.widget.adapterview;
import android.widget.arrayadapter;
import android.widget.spinner;


public class mainactivity extends activity
{
    private spinner provincespinner = null;  //省级(省、直辖市)
    private spinner cityspinner = null;     //地级市
    private spinner countyspinner = null;    //县级(区、县、县级市)
    arrayadapter<string> provinceadapter = null;  //省级适配器
    arrayadapter<string> cityadapter = null;    //地级适配器
    arrayadapter<string> countyadapter = null;    //县级适配器
    static int provinceposition = 3;

   
    //省级选项值
    private string[] province = new string[] {"北京","上海","天津","广东"};//,"重庆","黑龙江","江苏","山东","浙江","香港","澳门"};
    //地级选项值
    private string[][] city = new string[][]
            {
                    { "东城区", "西城区", "崇文区", "宣武区", "朝阳区", "海淀区", "丰台区", "石景山区", "门头沟区",
                            "房山区", "通州区", "顺义区", "大兴区", "昌平区", "平谷区", "怀柔区", "密云县",
                            "延庆县" },
                    { "长宁区", "静安区", "普陀区", "闸北区", "虹口区" },
                    { "和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区",
                            "东丽区" },
                    { "广州", "深圳", "韶关" // ,"珠海","汕头","佛山","湛江","肇庆","江门","茂名","惠州","梅州",
                    // "汕尾","河源","阳江","清远","东莞","中山","潮州","揭阳","云浮"
                    }
            };

    //县级选项值
    private string[][][] county = new string[][][]
            {
                    {   //北京
                        {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},
                        {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"}
                    },
                    {    //上海
                        {"无"},{"无"},{"无"},{"无"},{"无"}
                    },
                    {    //天津
                        {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"}
                    },
                    {    //广东
                        {"海珠区","荔湾区","越秀区","白云区","萝岗区","天河区","黄埔区","花都区","从化市","增城市","番禺区","南沙区"}, //广州
                        {"宝安区","福田区","龙岗区","罗湖区","南山区","盐田区"}, //深圳
                        {"武江区","浈江区","曲江区","乐昌市","南雄市","始兴县","仁化县","翁源县","新丰县","乳源县"}  //韶关
                    }
            };

   
    @override
    protected void oncreate(bundle savedinstancestate)
    {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        setspinner();
    }

   
    /*
     * 设置下拉框
     */
    private void setspinner()
    {       
        provincespinner = (spinner)findviewbyid(r.id.spin_province);
        cityspinner = (spinner)findviewbyid(r.id.spin_city);
        countyspinner = (spinner)findviewbyid(r.id.spin_county);

        //绑定适配器和值
        provinceadapter = new arrayadapter<string>(mainactivity.this,
                android.r.layout.simple_spinner_item, province);
        provincespinner.setadapter(provinceadapter);
        provincespinner.setselection(3,true);  //设置默认选中项,此处为默认选中第4个值

        cityadapter = new arrayadapter<string>(mainactivity.this,
                android.r.layout.simple_spinner_item, city[3]);
        cityspinner.setadapter(cityadapter);
        cityspinner.setselection(0,true);  //默认选中第0个

        countyadapter = new arrayadapter<string>(mainactivity.this,
                android.r.layout.simple_spinner_item, county[3][0]);
        countyspinner.setadapter(countyadapter);
        countyspinner.setselection(0, true);

       
        //省级下拉框监听
        provincespinner.setonitemselectedlistener(new adapterview.onitemselectedlistener()
        {

            // 表示选项被改变的时候触发此方法,主要实现办法:动态改变地级适配器的绑定值
            @override
            public void onitemselected(adapterview<?> arg0, view arg1, int position, long arg3)
            {
                //position为当前省级选中的值的序号

                //将地级适配器的值改变为city[position]中的值
                cityadapter = new arrayadapter<string>(
                        mainactivity.this, android.r.layout.simple_spinner_item, city[position]);
                // 设置二级下拉列表的选项内容适配器
                cityspinner.setadapter(cityadapter);
                provinceposition = position;    //记录当前省级序号,留给下面修改县级适配器时用
            }

            @override
            public void onnothingselected(adapterview<?> arg0)
            {

            }

        });

       
        //地级下拉监听
        cityspinner.setonitemselectedlistener(new adapterview.onitemselectedlistener()
        {

            @override
            public void onitemselected(adapterview<?> arg0, view arg1,
                    int position, long arg3)
            {
                countyadapter = new arrayadapter<string>(mainactivity.this,
                        android.r.layout.simple_spinner_item, county[provinceposition][position]);
                countyspinner.setadapter(countyadapter);
            }

            @override
            public void onnothingselected(adapterview<?> arg0)
            {

            }
        });
    }
}


要获取下拉框spinner中选中的值,用下面这方法就ok了

provincespinner.getselecteditem().tostring();

cityspinner.getselecteditem().tostring();

countyspinner.getselecteditem().tostring();


the end