当前位置:  编程技术>移动开发

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

    来源: 互联网  发布时间:2014-10-21

    本文导语:  主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值              XML布局: 代码如下:            实现代码: 代码如下:package com.example....

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

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

      

     

XML布局:

代码如下:


   

   

   


实现代码:

代码如下:

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 provinceAdapter = null;  //省级适配器
    ArrayAdapter cityAdapter = null;    //地级适配器
    ArrayAdapter 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(MainActivity.this,
                android.R.layout.simple_spinner_item, province);
        provinceSpinner.setAdapter(provinceAdapter);
        provinceSpinner.setSelection(3,true);  //设置默认选中项,此处为默认选中第4个值

        cityAdapter = new ArrayAdapter(MainActivity.this,
                android.R.layout.simple_spinner_item, city[3]);
        citySpinner.setAdapter(cityAdapter);
        citySpinner.setSelection(0,true);  //默认选中第0个

        countyAdapter = new ArrayAdapter(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(
                        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(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


    
 
 
 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • Android瀑布流实例 android_waterfall
  • Android的OpenGL编程实例 Android-GL
  • android 简单图片动画播放的实例代码
  • android WakeLock使用方法代码实例
  • android自动安装apk代码实例(不使用apk安装器安装)
  • oracle iis7站长之家
  • 控制Android LED灯颜色的代码实例
  • Android中AnimationDrawable使用的简单实例
  • Android中将View的内容保存为图像的简单实例
  • Android入门之LinearLayout、AbsoluteLayout的用法实例讲解
  • android中Bitmap的放大和缩小实例代码
  • android中写一个内部类来选择文件夹中指定的图片类型实例说明
  • 怎样删除android的gallery中的图片实例说明
  • 在Android中 获取正在运行的Service 实例
  • Android根据电话号码获得联系人头像实例代码
  • Android调用默认浏览器打开指定Url的方法实例
  • android双缓冲技术实例详解
  • ANDROID 完美退出APP的实例代码
  • Android对sdcard扩展卡文件操作实例详解
  • Android 清除SharedPreferences 产生的数据(实例代码)
  • 申请Android Map 的API Key(v2)的最新申请方式(SHA1密钥)
  • Android系统自带样式 (android:theme)
  • Android开发需要的几点注意事项总结
  • Android网络共享软件 Android Wifi Tether
  • android 4.0 托管进程介绍及优先级和回收机制
  • Android 图标库 Android GraphView
  • Android访问与手机通讯相关类的介绍
  • 轻量级Android开发工具 Android Tools
  • Android及andriod无线网络Wifi开发的几点注意事项
  • Android 开发环境 Android Studio
  • Android 2.3 下StrictMode介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3