二级联动下拉列表android_android 二级联动下拉列表 - CSDN
  • android省市二级联动下拉框,及全国省市xml文件

    全国省市xml文件下载

    xml布局文件:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF"
        android:orientation="vertical" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:orientation="horizontal" >
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="省:"
                    android:textSize="20dp" />
    
                <Spinner
                    android:id="@+id/sp_province"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF"
                    android:entries="@array/languages" />
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="|  市:"
                    android:textSize="20dp" />
    
                <Spinner
                    android:id="@+id/sp_city"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="#FFFFFF" />
            </LinearLayout>      
    
    </LinearLayout>

    Activity代码:

    package com.wh;
    
    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.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        Spinner city;
        Spinner province;
        String tProvince;
        String tCity;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            province = (Spinner) findViewById(R.id.sp_province);
            city = (Spinner) findViewById(R.id.sp_city);
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.province,
                    R.layout.spinner_checked_text);
            province.setAdapter(adapter);
            province.setOnItemSelectedListener(new spinnerItemSelected());
            city.setOnItemSelectedListener(new OnItemSelectedListener() {
    
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    tCity = city.getSelectedItem().toString();
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
    
                }
            });
    
        }
    
        // 二级联动adapter
        class spinnerItemSelected implements OnItemSelectedListener {
    
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner spinner = (Spinner) parent;
                String pro = (String) spinner.getItemAtPosition(position);
                tProvince = province.getSelectedItem().toString();
                // 处理省的市的显示
                ArrayAdapter<CharSequence> cityadapter = ArrayAdapter.createFromResource(getApplicationContext(),
                        R.array.def, R.layout.spinner_checked_text);
                if (pro.equals("北京")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.北京,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("天津")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.天津,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("河北")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.河北,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("山西")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.山西,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("内蒙古")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.内蒙古,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("辽宁")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.辽宁,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("吉林")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.吉林,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("黑龙江")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.黑龙江,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("上海")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.上海,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("江苏")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.江苏,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("浙江")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.浙江,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("安徽")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.安徽,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("福建")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.福建,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("江西")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.江西,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("山东")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.山东,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("河南")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.河南,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("湖北")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.湖北,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("湖南")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.湖南,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("广东")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.广东,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("广西")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.广西,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("海南")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.海南,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("重庆")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.重庆,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("四川")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.四川,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("贵州")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.贵州,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("云南")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.云南,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("西藏")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.西藏,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("陕西")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.陕西,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("甘肃")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.甘肃,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("青海")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.青海,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("宁夏")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.宁夏,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("新疆")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.新疆,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("台湾")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.台湾,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("香港")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.香港,
                            R.layout.spinner_checked_text);
                } else if (pro.equals("澳门")) {
                    cityadapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.澳门,
                            R.layout.spinner_checked_text);
                }
    
                city.setAdapter(cityadapter);
    
            }
    
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
            }
        }
    
    }
    

    全国省市XML文件:
    arrays.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <string-array name="province">
          <item>-省份-</item>
          <item>北京</item>
          <item>天津</item>
          <item>河北</item>
          <item>山西</item>
          <item>内蒙古</item>
          <item>辽宁</item>
          <item>吉林</item>
          <item>黑龙江</item>
          <item>上海</item>
          <item>江苏</item>
          <item>浙江</item>
          <item>安徽</item>
          <item>福建</item>
          <item>江西</item>
          <item>山东</item>
          <item>河南</item>
          <item>湖北</item>
          <item>湖南</item>
          <item>广东</item>
          <item>广西</item>
          <item>海南</item>
          <item>重庆</item>
          <item>四川</item>
          <item>贵州</item>
          <item>云南</item>
          <item>西藏</item>
          <item>陕西</item>
          <item>甘肃</item>
          <item>青海</item>
          <item>宁夏</item>
          <item>新疆</item>
          <item>台湾</item>
          <item>香港</item>
          <item>澳门</item>
       </string-array>
       <string-array name="def">
          <item>-城市-</item>
       </string-array>
       <string-array name="北京">
          <item>-城市-</item>
          <item>东城</item>
          <item>西城</item>
          <item>朝阳</item>
          <item>丰台</item>
          <item>石景山</item>
          <item>海淀</item>
          <item>门头沟</item>
          <item>房山</item>
          <item>通州</item>
          <item>顺义</item>
          <item>昌平</item>
          <item>大兴</item>
          <item>平谷</item>
          <item>怀柔</item>
          <item>密云</item>
          <item>延庆</item>
       </string-array>
       <string-array name="天津">
          <item>-城市-</item>
          <item>和平</item>
          <item>河东</item>
          <item>河西</item>
          <item>南开</item>
          <item>河北</item>
          <item>红桥</item>
          <item>滨海新区</item>
          <item>东丽</item>
          <item>西青</item>
          <item>津南</item>
          <item>北辰</item>
          <item>宁河</item>
          <item>武清</item>
          <item>静海</item>
          <item>宝坻</item>
          <item>蓟县</item>
       </string-array>
       <string-array name="河北">
          <item>-城市-</item>
          <item>石家庄</item>
          <item>唐山</item>
          <item>秦皇岛</item>
          <item>邯郸</item>
          <item>邢台</item>
          <item>保定</item>
          <item>张家口</item>
          <item>承德</item>
          <item>沧州</item>
          <item>廊坊</item>
          <item>衡水</item>
       </string-array>
       <string-array name="山西">
          <item>-城市-</item>
          <item>太原</item>
          <item>大同</item>
          <item>阳泉</item>
          <item>长治</item>
          <item>晋城</item>
          <item>朔州</item>
          <item>晋中</item>
          <item>运城</item>
          <item>忻州</item>
          <item>临汾</item>
          <item>吕梁</item>
       </string-array>
       <string-array name="内蒙古">
          <item>-城市-</item>
          <item>呼和浩特</item>
          <item>包头</item>
          <item>乌海</item>
          <item>赤峰</item>
          <item>通辽</item>
          <item>鄂尔多斯</item>
          <item>呼伦贝尔</item>
          <item>巴彦淖尔</item>
          <item>乌兰察布</item>
          <item>兴安</item>
          <item>锡林郭勒</item>
          <item>阿拉善</item>
       </string-array>
       <string-array name="辽宁">
          <item>-城市-</item>
          <item>沈阳</item>
          <item>大连</item>
          <item>鞍山</item>
          <item>抚顺</item>
          <item>本溪</item>
          <item>丹东</item>
          <item>锦州</item>
          <item>营口</item>
          <item>阜新</item>
          <item>辽阳</item>
          <item>盘锦</item>
          <item>铁岭</item>
          <item>朝阳</item>
          <item>葫芦岛</item>
       </string-array>
       <string-array name="吉林">
          <item>-城市-</item>
          <item>长春</item>
          <item>吉林</item>
          <item>四平</item>
          <item>辽源</item>
          <item>通化</item>
          <item>白山</item>
          <item>松原</item>
          <item>白城</item>
          <item>延边</item>
       </string-array>
       <string-array name="黑龙江">
          <item>-城市-</item>
          <item>哈尔滨</item>
          <item>齐齐哈尔</item>
          <item>鸡西</item>
          <item>鹤岗</item>
          <item>双鸭山</item>
          <item>大庆</item>
          <item>伊春</item>
          <item>佳木斯</item>
          <item>七台河</item>
          <item>牡丹江</item>
          <item>黑河</item>
          <item>绥化</item>
          <item>大兴安岭</item>
       </string-array>
       <string-array name="上海">
          <item>-城市-</item>
          <item>黄浦</item>
          <item>卢湾</item>
          <item>徐汇</item>
          <item>长宁</item>
          <item>静安</item>
          <item>普陀</item>
          <item>闸北</item>
          <item>虹口</item>
          <item>杨浦</item>
          <item>闵行</item>
          <item>宝山</item>
          <item>嘉定</item>
          <item>浦东新区</item>
          <item>金山</item>
          <item>松江</item>
          <item>奉贤</item>
          <item>青浦</item>
          <item>崇明</item>
       </string-array>
       <string-array name="江苏">
          <item>-城市-</item>
          <item>南京</item>
          <item>无锡</item>
          <item>徐州</item>
          <item>常州</item>
          <item>苏州</item>
          <item>南通</item>
          <item>连云港</item>
          <item>淮安</item>
          <item>盐城</item>
          <item>扬州</item>
          <item>镇江</item>
          <item>泰州</item>
          <item>宿迁</item>
       </string-array>
       <string-array name="浙江">
          <item>-城市-</item>
          <item>杭州</item>
          <item>宁波</item>
          <item>温州</item>
          <item>嘉兴</item>
          <item>湖州</item>
          <item>绍兴</item>
          <item>金华</item>
          <item>衢州</item>
          <item>舟山</item>
          <item>台州</item>
          <item>丽水</item>
       </string-array>
       <string-array name="安徽">
          <item>-城市-</item>
          <item>合肥</item>
          <item>芜湖</item>
          <item>蚌埠</item>
          <item>淮南</item>
          <item>马鞍山</item>
          <item>淮北</item>
          <item>铜陵</item>
          <item>安庆</item>
          <item>黄山</item>
          <item>滁州</item>
          <item>阜阳</item>
          <item>宿州</item>
          <item>六安</item>
          <item>亳州</item>
          <item>池州</item>
          <item>宣城</item>
       </string-array>
       <string-array name="福建">
          <item>-城市-</item>
          <item>福州</item>
          <item>厦门</item>
          <item>莆田</item>
          <item>三明</item>
          <item>泉州</item>
          <item>漳州</item>
          <item>南平</item>
          <item>龙岩</item>
          <item>宁德</item>
       </string-array>
       <string-array name="江西">
          <item>-城市-</item>
          <item>南昌</item>
          <item>景德镇</item>
          <item>萍乡</item>
          <item>九江</item>
          <item>新余</item>
          <item>鹰潭</item>
          <item>赣州</item>
          <item>吉安</item>
          <item>宜春</item>
          <item>抚州</item>
          <item>上饶</item>
       </string-array>
       <string-array name="山东">
          <item>-城市-</item>
          <item>济南</item>
          <item>青岛</item>
          <item>淄博</item>
          <item>枣庄</item>
          <item>东营</item>
          <item>烟台</item>
          <item>潍坊</item>
          <item>济宁</item>
          <item>泰安</item>
          <item>威海</item>
          <item>日照</item>
          <item>莱芜</item>
          <item>临沂</item>
          <item>德州</item>
          <item>聊城</item>
          <item>滨州</item>
          <item>菏泽</item>
       </string-array>
       <string-array name="河南">
          <item>-城市-</item>
          <item>郑州</item>
          <item>开封</item>
          <item>洛阳</item>
          <item>平顶山</item>
          <item>安阳</item>
          <item>鹤壁</item>
          <item>新乡</item>
          <item>焦作</item>
          <item>濮阳</item>
          <item>许昌</item>
          <item>漯河</item>
          <item>三门峡</item>
          <item>南阳</item>
          <item>商丘</item>
          <item>信阳</item>
          <item>周口</item>
          <item>驻马店</item>
          <item>济源</item>
       </string-array>
       <string-array name="湖北">
          <item>-城市-</item>
          <item>武汉</item>
          <item>黄石</item>
          <item>十堰</item>
          <item>宜昌</item>
          <item>襄阳</item>
          <item>鄂州</item>
          <item>荆门</item>
          <item>孝感</item>
          <item>荆州</item>
          <item>黄冈</item>
          <item>咸宁</item>
          <item>随州</item>
          <item>恩施</item>
          <item>仙桃</item>
          <item>潜江</item>
          <item>天门</item>
          <item>神农架</item>
       </string-array>
       <string-array name="湖南">
          <item>-城市-</item>
          <item>长沙</item>
          <item>株洲</item>
          <item>湘潭</item>
          <item>衡阳</item>
          <item>邵阳</item>
          <item>岳阳</item>
          <item>常德</item>
          <item>张家界</item>
          <item>益阳</item>
          <item>郴州</item>
          <item>永州</item>
          <item>怀化</item>
          <item>娄底</item>
          <item>湘西</item>
       </string-array>
       <string-array name="广东">
          <item>-城市-</item>
          <item>广州</item>
          <item>韶关</item>
          <item>深圳</item>
          <item>珠海</item>
          <item>汕头</item>
          <item>佛山</item>
          <item>江门</item>
          <item>湛江</item>
          <item>茂名</item>
          <item>肇庆</item>
          <item>惠州</item>
          <item>梅州</item>
          <item>汕尾</item>
          <item>河源</item>
          <item>阳江</item>
          <item>清远</item>
          <item>东莞</item>
          <item>中山</item>
          <item>潮州</item>
          <item>揭阳</item>
          <item>云浮</item>
       </string-array>
       <string-array name="广西">
          <item>-城市-</item>
          <item>南宁</item>
          <item>柳州</item>
          <item>桂林</item>
          <item>梧州</item>
          <item>北海</item>
          <item>防城港</item>
          <item>钦州</item>
          <item>贵港</item>
          <item>玉林</item>
          <item>百色</item>
          <item>贺州</item>
          <item>河池</item>
          <item>来宾</item>
          <item>崇左</item>
       </string-array>
       <string-array name="海南">
          <item>-城市-</item>
          <item>海口</item>
          <item>三亚</item>
          <item>三沙</item>
          <item>五指山</item>
          <item>琼海</item>
          <item>儋州</item>
          <item>文昌</item>
          <item>万宁</item>
          <item>东方</item>
          <item>定安</item>
          <item>屯昌</item>
          <item>澄迈</item>
          <item>临高</item>
          <item>白沙</item>
          <item>昌江</item>
          <item>乐东</item>
          <item>陵水</item>
          <item>保亭</item>
          <item>琼中</item>
       </string-array>
       <string-array name="重庆">
          <item>-城市-</item>
          <item>万州</item>
          <item>涪陵</item>
          <item>渝中</item>
          <item>大渡口</item>
          <item>江北</item>
          <item>沙坪坝</item>
          <item>九龙坡</item>
          <item>南岸</item>
          <item>北碚</item>
          <item>两江新区</item>
          <item>万盛</item>
          <item>双桥</item>
          <item>渝北</item>
          <item>巴南</item>
          <item>长寿</item>
          <item>綦江</item>
          <item>潼南</item>
          <item>铜梁</item>
          <item>大足</item>
          <item>荣昌</item>
          <item>璧山</item>
          <item>梁平</item>
          <item>城口</item>
          <item>丰都</item>
          <item>垫江</item>
          <item>武隆</item>
          <item>忠县</item>
          <item>开县</item>
          <item>云阳</item>
          <item>奉节</item>
          <item>巫山</item>
          <item>巫溪</item>
          <item>黔江</item>
          <item>石柱</item>
          <item>秀山</item>
          <item>酉阳</item>
          <item>彭水</item>
          <item>江津</item>
          <item>合川</item>
          <item>永川</item>
          <item>南川</item>
       </string-array>
       <string-array name="四川">
          <item>-城市-</item>
          <item>成都</item>
          <item>自贡</item>
          <item>攀枝花</item>
          <item>泸州</item>
          <item>德阳</item>
          <item>绵阳</item>
          <item>广元</item>
          <item>遂宁</item>
          <item>内江</item>
          <item>乐山</item>
          <item>南充</item>
          <item>眉山</item>
          <item>宜宾</item>
          <item>广安</item>
          <item>达州</item>
          <item>雅安</item>
          <item>巴中</item>
          <item>资阳</item>
          <item>阿坝</item>
          <item>甘孜</item>
          <item>凉山</item>
       </string-array>
       <string-array name="贵州">
          <item>-城市-</item>
          <item>贵阳</item>
          <item>六盘水</item>
          <item>遵义</item>
          <item>安顺</item>
          <item>铜仁</item>
          <item>黔西南</item>
          <item>毕节</item>
          <item>黔东南</item>
          <item>黔南</item>
       </string-array>
       <string-array name="云南">
          <item>-城市-</item>
          <item>昆明</item>
          <item>曲靖</item>
          <item>玉溪</item>
          <item>保山</item>
          <item>昭通</item>
          <item>丽江</item>
          <item>普洱</item>
          <item>临沧</item>
          <item>楚雄</item>
          <item>红河</item>
          <item>文山</item>
          <item>西双版纳</item>
          <item>大理</item>
          <item>德宏</item>
          <item>怒江</item>
          <item>迪庆</item>
       </string-array>
       <string-array name="西藏">
          <item>-城市-</item>
          <item>拉萨</item>
          <item>昌都</item>
          <item>山南</item>
          <item>日喀则</item>
          <item>那曲</item>
          <item>阿里</item>
          <item>林芝</item>
       </string-array>
       <string-array name="陕西">
          <item>-城市-</item>
          <item>西安</item>
          <item>铜川</item>
          <item>宝鸡</item>
          <item>咸阳</item>
          <item>渭南</item>
          <item>延安</item>
          <item>汉中</item>
          <item>榆林</item>
          <item>安康</item>
          <item>商洛</item>
       </string-array>
       <string-array name="甘肃">
          <item>-城市-</item>
          <item>兰州市</item>
          <item>嘉峪关</item>
          <item>金昌</item>
          <item>白银</item>
          <item>天水</item>
          <item>武威</item>
          <item>张掖</item>
          <item>平凉</item>
          <item>酒泉</item>
          <item>庆阳</item>
          <item>定西</item>
          <item>陇南</item>
          <item>临夏</item>
          <item>甘南</item>
       </string-array>
       <string-array name="青海">
          <item>-城市-</item>
          <item>西宁</item>
          <item>海东</item>
          <item>海北</item>
          <item>黄南</item>
          <item>海南</item>
          <item>果洛</item>
          <item>玉树</item>
          <item>海西</item>
       </string-array>
       <string-array name="宁夏">
          <item>-城市-</item>
          <item>银川</item>
          <item>石嘴山</item>
          <item>吴忠</item>
          <item>固原</item>
          <item>中卫</item>
       </string-array>
       <string-array name="新疆">
          <item>-城市-</item>
          <item>乌鲁木齐</item>
          <item>克拉玛依</item>
          <item>吐鲁番</item>
          <item>哈密</item>
          <item>昌吉</item>
          <item>博尔塔拉</item>
          <item>巴音郭楞</item>
          <item>阿克苏</item>
          <item>克孜勒苏</item>
          <item>喀什</item>
          <item>和田</item>
          <item>伊犁</item>
          <item>塔城</item>
          <item>阿勒泰</item>
          <item>石河子</item>
          <item>阿拉尔</item>
          <item>图木舒克</item>
          <item>五家渠</item>
          <item>北屯</item>
       </string-array>
       <string-array name="台湾">
          <item>-城市-</item>
          <item>台北市</item>
          <item>高雄市</item>
          <item>基隆市</item>
          <item>台中市</item>
          <item>台南市</item>
          <item>新竹市</item>
          <item>嘉义市</item>
          <item>台北县</item>
          <item>宜兰县</item>
          <item>桃园县</item>
          <item>新竹县</item>
          <item>苗栗县</item>
          <item>台中县</item>
          <item>彰化县</item>
          <item>南投县</item>
          <item>云林县</item>
          <item>嘉义县</item>
          <item>台南县</item>
          <item>高雄县</item>
          <item>屏东县</item>
          <item>台东县</item>
          <item>花莲县</item>
          <item>澎湖县</item>
       </string-array>
       <string-array name="香港">
          <item>-城市-</item>
          <item>中西区</item>
          <item>东区</item>
          <item>九龙城区</item>
          <item>观塘区</item>
          <item>南区</item>
          <item>深水埗区</item>
          <item>黄大仙区</item>
          <item>湾仔区</item>
          <item>油尖旺区</item>
          <item>离岛区</item>
          <item>葵青区</item>
          <item>北区</item>
          <item>西贡区</item>
          <item>沙田区</item>
          <item>屯门区</item>
          <item>大埔区</item>
          <item>荃湾区</item>
          <item>元朗区</item>
       </string-array>
       <string-array name="澳门">
          <item>-城市-</item>
          <item>花地玛堂区</item>
          <item>圣安多尼堂区</item>
          <item>大堂区</item>
          <item>望德堂区</item>
          <item>风顺堂区</item>
          <item>氹仔</item>
          <item>路环</item>
       </string-array>
    </resources>
    
    展开全文
  • android下拉二级联动效果实现,即一个下拉选项的选择改变另一下拉选项的内容。
  • Android开发实现二级联动下拉列表 http://android.tgbus.com/Android/tutorial/201107/361334.shtml 2011年7月28日10:52来源:Android中文网我有话说(0人参与)  代码如下:  Java ...
    

    Android开发实现二级联动下拉列表


    http://android.tgbus.com/Android/tutorial/201107/361334.shtml

    • 2011年7月28日10:52
    • 来源:Android中文网
    • 我有话说(0人参与)

      代码如下:

      Java code:

      package zye.client.Client;

      import android.app.Activity;
      import android.content.Context;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.AdapterView;
      import android.widget.ArrayAdapter; import android.widget.Spinner;
      import android.widget.AdapterView.OnItemSelectedListener;

      public class cityset extends Activity{
          private String[] province = new String[] {"直辖市", "特别行政区","黑龙江"};
          private String[] city = new String[]{"北京","上海","天津","重庆"};
          private String[][] pandc = new String[][]{{"北京","上海","天津","重庆"},{"香港","澳

      门"},{"哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"}};
          private Spinner sp;
          private Spinner sp2;
          private Context context;

          ArrayAdapter<String> adapter ;

          ArrayAdapter<String> adapter2;
       
           public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.cityset);

      context = this;
            
                  adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, province);
                  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  sp = (Spinner) findViewById(R.id.province);
                  sp.setAdapter(adapter);
                  sp.setOnItemSelectedListener(selectListener);
                
                  adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
                adapter2.setDropDownViewResource

      (android.R.layout.simple_spinner_dropdown_item);
                  sp2 = (Spinner) findViewById(R.id.city);
                  sp2.setAdapter(adapter2);
               
          }
        
           private OnItemSelectedListener selectListener = new OnItemSelectedListener(){
               public void onItemSelected(AdapterView parent, View v, int position,long id){
                   int pos = sp.getSelectedItemPosition();
                   adapter2 = new ArrayAdapter<String>

      (context,android.R.layout.simple_spinner_item, pandc[pos]);
                   sp2.setAdapter(adapter2);
               }
          
               public void onNothingSelected(AdapterView arg0){

             }

         };

    }

      xml code:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#FFFFFF"
          >
          <Spinner android:id="@+id/province"
              android:layout_width="fill_parent"
                android:layout_height="45px"
                android:layout_marginLeft="75px"
                android:layout_marginRight="20px"
              android:layout_marginTop="7px"
               android:drawSelectorOnTop="true"
               android:layout_alignParentRight="true"
          />
             <Spinner android:id="@+id/city"
              android:layout_width="fill_parent"
                android:layout_height="45px"
                android:layout_marginLeft="75px"
                android:layout_marginRight="20px"
              android:layout_marginTop="3px"
               android:drawSelectorOnTop="true"
               android:layout_alignParentRight="true"
               android:layout_below="@id/province"
              />
         
      </RelativeLayout>

    展开全文
  • 主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值       XML布局: LinearLayout xmlns:android=...

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

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

           

          

     

    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();

     

    转载请注明出处:

    http://www.goteny.com/articles/2013/11/46.html

    http://www.cnblogs.com/zjjne/p/3350107.html

    THE END

    展开全文
  • 运用面向对象的思想,通过XML或其它方式,灵活的调整省份和城市的数据,以此实现联动下拉框的数据装配
  • 大家可以自行前去下载这里写链接内容3、下面是我对该资料,及省市二级联动实现的分析二级联动思路整理这个json数据太大了,不能每次都循环遍历所有数据1首先要获得省的列表 在json数据中,所有

    因为工作的关系,需要做一个省市二级联动。
    1、首先,实现这个功能所用到的控件是android 的spinner
    2、要做一个省市二级联动,首先我们要有所有省及其主要城市的数据,这里我给一个我的百度云的链接。大家可以自行前去下载

    这里写链接内容

    3、下面是我对该资料,及省市二级联动实现的分析

    二级联动思路整理

    这个json数据太大了,不能每次都循环遍历所有数据

    1首先要获得省的列表
    在json数据中,所有fGuid的值都为0,可以根据这个得到所有的省的列表。可以只解析前34个json数据的值(省的信息排在该json的前面,34已经足够将所有的省的信息获取到),可以有效的减少程序运行
    2将省的信息放入spinner中
    3省被选择后要获得城市的信息
    城市的fGuid的值都不为空,fGuid的值代表的是城市所属的省,他的值是省的Guid的值。所以,所有省的Guid值都必须被保存
    这时判断json中的fGuid就可以得出所有该省城市的数据
    同一个省的城市在该json数据中都是连续的。当找到一个属于该省的程序后,如果连续出现五个不属于该省的城市,查找停止。
    4用户点击所选城市时,所需要的最终数据是cityNo其他都是不用的。所以,要抽取出来的数据就是cityName(用于spinner显示),cityNo 实际要用的数据
    总结: 省需要获取的信息是省的cityName(用于spinner显示)、guid(用于查找该省下的城市)
    城市需要获得的信息是cityName(用于spinner显示)、cityNo

    4以下是我实现二级联动的代码

    package com.zhiyinqing;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.util.HashMap;
     
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.Spinner;
    import android.widget.Toast;
     
    public class MainActivity extends Activity {
     
        HashMap<String, String> provinceHash = new HashMap<String, String>();
        String[] provinceString = new String[34];
     
        HashMap<String, String> cityHash = new HashMap<String, String>();
        String[] cityString;
     
        String file;
     
        String cityNo = null;// 最重要的参数,选中的城市的cityNo
     
        private ArrayAdapter<String> proviceAdapter;
        private ArrayAdapter<String> cityAdapter;
        Spinner provinceSpinner;
        Spinner citySpinner;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            provinceSpinner = (Spinner) findViewById(R.id.spinnerprovince);
            citySpinner = (Spinner) findViewById(R.id.spinnercity);
     
            file = readFile(); // 读取txt文件
            getProvinces(file); // 得到省的列表
     
            // 设置spinner,不用管什么作用
            proviceAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, provinceString);
            proviceAdapter
                    .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设置下拉风格
            provinceSpinner.setAdapter(proviceAdapter); // 将adapter 添加到spinner中
            provinceSpinner.setOnItemSelectedListener(new ProvinceSelectedListener(
                    MainActivity.this));// 添加监听
            provinceSpinner.setVisibility(View.VISIBLE);// 设置默认值
     
        }
     
        public String readFile() {
     
            /*
             * 读取文件中数据的方法
             */
     
            InputStream myFile = null;
            myFile = getResources().openRawResource(R.raw.ub_city);
            BufferedReader br = null;
     
            try {
                br = new BufferedReader(new InputStreamReader(myFile, "utf-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.e("debug", e.toString());
            }
            String temp;
            String str = "";
            try {
                while ((temp = br.readLine()) != null) {
                    str = str + temp;
                    // Log.i("zhiyinqing", "断点3"+temp);
                }
                br.close();
                myFile.close();
                return str;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return "error";
            }
        }
     
        public void getProvinces(String jsonData) {
     
            /*
             * 从json字符串中得到省的信息
             */
     
            JSONArray jsonArray = null;
            try {
                jsonArray = new JSONArray(jsonData);
                for (int i = 0; i < 34; i++) {
                    // 获取了34个省市区信息
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String guid = jsonObject.getString("guid");
                    String cityName = jsonObject.getString("cityName");
                    // Log.i("zhiyinqing", i+guid+cityName);
                    provinceHash.put(cityName, guid);
                    provinceString[i] = cityName;
     
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
     
        public String[] getCitys(String guid, String jsonData) {
            /*
             * 此方法用于查找一个省下的所有城市
             */
            // 初始化hashmap
            cityHash.clear();
            // 暂时存放城市的数组
            String[] cityBuffer = new String[21];
            int j = 0;
     
            // 解析数据
            JSONArray jsonArray;
            try {
                jsonArray = new JSONArray(jsonData);
                int length = jsonArray.length();
                int i = 33;
                int continuous = 0;// 这个变量用于判断是否连续几次没有找到,如果是,则该省信息全部找到了
                boolean isFind = false;
                 
                while (i < length) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String fGuid = jsonObject.getString("fGuid");
                    String cityName = jsonObject.getString("cityName");
                    String cityNo = jsonObject.getString("cityNo");
                    if (fGuid.equals(guid)) {
                        isFind = true;
                        cityHash.put(cityName, cityNo);
                        cityBuffer[j] = cityName;
                        j++;
                        // Log.i("zhiyinqing", cityName);
                    } else {
                        if (isFind == true) {
                            continuous += 1;
     
                            if (continuous > 5) {
                                Log.i("zhiyinqing", "城市数:" + j);
                                break;
                            }
                        }
                    }
                    i++;
                }
                 
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String[] citys = new String[j];
            for(int i=0;i<j;i++){
                citys[i] = cityBuffer[i];
            }
            return citys;
        }
     
        class ProvinceSelectedListener implements OnItemSelectedListener {
     
            Context context;
     
            // 省被选择的监听器
     
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
     
                String provinceName = provinceString[arg2];
                Toast.makeText(context, provinceName, Toast.LENGTH_LONG).show();
                String guid = provinceHash.get(provinceName);
                cityString = getCitys(guid, file);
     
                // 省被选中后,初始化城市Spinner
                cityAdapter = new ArrayAdapter<String>(context,
                        android.R.layout.simple_spinner_item, cityString);
                cityAdapter
                        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// 设置下拉风格
                citySpinner.setAdapter(cityAdapter); // 将adapter 添加到spinner中
                citySpinner.setOnItemSelectedListener(new CitySelectedListener());// 添加监听
                citySpinner.setVisibility(View.VISIBLE);// 设置默认
     
            }
     
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
     
            }
     
            public ProvinceSelectedListener(Context context) {
                this.context = context;
            }
     
        }
     
        class CitySelectedListener implements OnItemSelectedListener {
     
            // 城市被点击的监听事件
     
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                String cityName = cityString[arg2];
                if (cityName.equals("") || cityName == null) {
                    cityName = cityString[0];
                    cityNo = cityHash.get(cityName);
     
                } else {
                    cityNo = cityHash.get(cityName);
                    Log.i("zhiyinqing", "cityNo" + cityNo);
                }
     
            }
     
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
     
            }
     
        }
     
         
     
    }
    

    如果有帮到您,打个赏呗
    在这里插入图片描述

    展开全文
  • 实现一个二级联动下拉列表,就是选定省份后,城市的下拉列表出现相应变化Java code:package zye.client.Client;import android.app.Activity;import android.content.Context;import android.os.Bundle;import ...

    本文来自:安卓航班网

    实现一个二级联动的下拉列表,就是选定省份后,城市的下拉列表出现相应变化

    Java code:

    package zye.client.Client;

    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.ArrayAdapter; import android.widget.Spinner;
    import android.widget.AdapterView.OnItemSelectedListener;

    public class cityset extends Activity{
        private String[] province = new String[] {"直辖市", "特别行政区","黑龙江"};
        private String[] city = new String[]{"北京","上海","天津","重庆"};
        private String[][] pandc = new String[][]{{"北京","上海","天津","重庆"},{"香港","澳门"},{"哈尔滨","齐齐哈尔","牡丹江","大庆","伊春","双鸭山","鹤岗","鸡西","佳木斯","七台河","黑河","绥化","大兴安岭"}};
        private Spinner sp;
        private Spinner sp2;
        private Context context;

        ArrayAdapter<String> adapter ;

        ArrayAdapter<String> adapter2;
       
         public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.cityset);
             
             context = this;
             
                adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, province);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                sp = (Spinner) findViewById(R.id.province);
                sp.setAdapter(adapter);
                sp.setOnItemSelectedListener(selectListener);
                
                
                adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
                adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                sp2 = (Spinner) findViewById(R.id.city);
                sp2.setAdapter(adapter2);
                
        }
         
         private OnItemSelectedListener selectListener = new OnItemSelectedListener(){
             public void onItemSelected(AdapterView parent, View v, int position,long id){
                 int pos = sp.getSelectedItemPosition();
                 adapter2 = new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item, pandc[pos]);
                 sp2.setAdapter(adapter2);
             }
          
             public void onNothingSelected(AdapterView arg0){

             }

         };

    }


    xml code:


    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        >
        <Spinner android:id="@+id/province"
            android:layout_width="fill_parent"
              android:layout_height="45px"
              android:layout_marginLeft="75px"
              android:layout_marginRight="20px"
            android:layout_marginTop="7px"
             android:drawSelectorOnTop="true"
             android:layout_alignParentRight="true"
        />
           <Spinner android:id="@+id/city"
            android:layout_width="fill_parent"
              android:layout_height="45px"
              android:layout_marginLeft="75px"
              android:layout_marginRight="20px"
            android:layout_marginTop="3px"
             android:drawSelectorOnTop="true"
             android:layout_alignParentRight="true"
             android:layout_below="@id/province"
             />
          
    </RelativeLayout>

    原文地址:http://www.apkway.com/forum.php?mod=viewthread&tid=2178&extra=page%3D2

    转载于:https://www.cnblogs.com/yyq-quan/archive/2011/07/27/2118599.html

    展开全文
  • 省市二级联动菜单Demo 省市二级联动菜单Demo 点击下拉菜单选择省市 点击按钮自定义选择省市 点我选择“上海”,“金山” js文件linkage.js内容如下: /** * 省市二级联动菜单 * ...
  • Android实现二级联动

    2019-01-19 11:59:11
    Android实现二级联动二级联动是非常常见实用的功能,用的比较多的就是地址选择了,今天就给大家讲讲如何实现:   效果图:   布局文件:     &lt;?xml version="1.0" encoding=&...
  • 对于Android 开发的时候我们肯定会碰到需要Spinner下拉菜单的时候,这里我们先来介绍一下如何实现三个下拉菜单的简单应用 Spinner的三请看我下一篇博客吧 一. 首先我们需要设置一下activity_main.xml, 这里...
  • 项目Github托管地址:https://github.com/zhouzhuo810/ZzSecondaryLinkage效果图How to use it ?准备工作①第一步,添加布局 android:id="@+id/zz_linkage" android:
  • 这里我们在来说说我们当三个或三个以上的Spinner如何进行联动,就是当我们选择湖南的时候第个城市自己会跳转到张家界的功能。 实现逻辑: 这里我们只要实现当我们点击湖南的对应事件就可以,所以这里我们可以用到...
  • 我们在常用的电商或者旅游APP中,例如美团,手机淘宝等等,都能够看的到有那种下拉式的二级列表菜单。具体如图所示: 上面两张图就是美团的一个二级列表菜单的一个展示。我相信很多人都想开发一个跟它一样...
  • 实现一个二级联动下拉列表,就是选定省份后,城市的下拉列表出现相应变化 mport android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import ...
  • Android App应用中,二级联动是应用极为广泛的,我们在上一章的基础上来学习一下如何实现。  基本知识点:OnItemSelectedListener事件 一、界面  1、新建province.xml件。  在“res/values”位置新建...
  • 在做的过程中遇到了一个下拉菜单联动的问题,以前没做过,突然做还真有点不知所措了,在网上找了一大堆发现数据都是本地的应该构造好了,没卵用,不过也给了我很多想发,一开始想到用树去解决,可是每个树的节点个数...
1 2 3 4 5 ... 20
收藏数 1,157
精华内容 462
关键字:

二级联动下拉列表android