Android实现省市区三级联动效果Spinner

来源:互联网 发布:淘宝卖otc药品的 编辑:程序博客网 时间:2024/06/05 18:25

主要目的是使用Spinner实现省市县的三级联动

一、布局的设计

<?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="wrap_content"    android:orientation="vertical">    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="省"        android:textColor="#000000"        />    <Spinner        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/sheng" />    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="市"        android:textColor="#000000"        />    <Spinner        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/shi" />    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="县"        android:textColor="#000000"        />    <Spinner        android:id="@+id/xian"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <TextView        android:id="@+id/t_v"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>
使用线性布局完成简单的界面,主要包含选择和显示选择的结果

二、MainActivity

public class MainActivity extends AppCompatActivity {    Spinner s_sheng;    ArrayAdapter<String> adapter_sheng;    ArrayAdapter<String> adapter_shi;    ArrayAdapter<String> adapter_xian;    Spinner s_shi;    Spinner s_xian;    int sheng_postion;    int shi_postion;    String seletced_sheng;    String seletced_shi;    String seletced_xian;    TextView t_v;    String[] arr_sheng={"北京市","天津市","上海市","广东省","河南省","重庆市","河北省","山西省","辽宁省","吉林省","黑龙江省","江苏省","浙江省", "安徽省","福建省",            "江西省","山东省","湖北省","湖南省","海南省","四川省","贵州省","云南省","陕西省", "甘肃省","青海省","台湾省"};    String [][]arr_shi=new String[][] {            {"北京市"},// 北京市            {"天津市"},//天津市            {"上海市"},//上海市            {"广州", "深圳", "韶关", "珠海", "汕头", "佛山", "湛江", "肇庆", "江门", "茂名", "惠州", "梅州",                    "汕尾", "河源", "阳江", "清远", "东莞", "中山", "潮州", "揭阳", "云浮"},//广州市            {"郑州市","开封市","洛阳市","平顶山市","许昌市"            },//河南省    };    String[][][]arr_xian= new String [][][]{            {//北京市                    {"东城区", "西城区", "崇文区", "宣武区", "朝阳区", "海淀区", "丰台区", "石景山区", "门头沟区",                            "房山区", "通州区", "顺义区", "大兴区", "昌平区", "平谷区", "怀柔区", "密云县", "延庆县"}            },            {//深圳                    {"和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区", "东丽区"}            },            {//上海                    {"长宁区", "静安区", "普陀区", "闸北区", "虹口区"}            },            {//广东                    {"海珠区", "荔湾区", "越秀区", "白云区", "萝岗区", "天河区", "黄埔区", "花都区", "从化市", "增城市", "番禺区", "南沙区"}, //广州                    {"宝安区", "福田区", "龙岗区", "罗湖区", "南山区", "盐田区"}, //深圳                    {"武江区", "浈江区", "曲江区", "乐昌市", "南雄市", "始兴县", "仁化县", "翁源县", "新丰县", "乳源县"},//韶关                    {"无"}            },            {//河南                    {"中原区","二七区","管城区","金水区","上街区","惠济区","巩义市","荥阳市","新密市","新郑市" ,"登封市","中牟县" },                    {"鼓楼区","龙亭区","顺河区","禹王台","金明区", "杞县","通许县", "尉氏县", "开封县" ,"兰考县"},                    {"西工区","老城区","瀍河区","涧西区","吉利区","洛龙区","偃师市","孟津县","新安县","栾川县","嵩县","汝阳县", "宜阳县","洛宁县","伊川县"},                    {"新华区","卫东区","湛河区","石龙区","舞钢市","汝州市","宝丰县","叶 县","鲁山县","郏县"},                    {"魏都区","禹州市","长葛市","许昌县","鄢陵县", "襄城县"}            },    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.layout);        t_v=(TextView) findViewById(R.id.t_v);        s_sheng = (Spinner) findViewById(R.id.sheng);        s_shi = (Spinner) findViewById(R.id.shi);        s_xian = (Spinner) findViewById(R.id.xian);        adapter_sheng = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_sheng);        adapter_shi = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_shi[0]);        adapter_xian = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arr_xian[0][0]);        s_sheng.setAdapter(adapter_sheng);        s_sheng.setSelection(0);        s_shi.setAdapter(adapter_shi);        s_shi.setSelection(0);        s_xian.setAdapter(adapter_xian);        s_xian.setSelection(0);        s_sheng.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {                sheng_postion=i;                adapter_shi=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arr_shi[i]);                s_shi.setAdapter(adapter_shi);                seletced_sheng=arr_sheng[i];            }            @Override            public void onNothingSelected(AdapterView<?> parent) {            }        });        s_shi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {                shi_postion=i;                adapter_xian=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, arr_xian[sheng_postion][shi_postion]);                s_xian.setAdapter(adapter_xian);                seletced_shi=arr_shi[sheng_postion][i];            }            @Override            public void onNothingSelected(AdapterView<?> parent) {            }        });        s_xian.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {                seletced_xian=arr_xian[sheng_postion][shi_postion][i];                t_v.setText(seletced_sheng+"-"+seletced_shi+"-"+seletced_xian);            }            @Override            public void onNothingSelected(AdapterView<?> parent) {            }        });    }}

设计思想:

1.所有的数据暂时保存在数组里,一次定义了3个数组分别为一维二维三位,来存储省市县乡的数据
2.使用Spinner组件实现mvc三层的控制效果
3.包括ArrayAdapter创建以及Spinner设置装配数据

三、成果演示



四、资源链接

如有需要请参考:Android实现三级联动代码

原创粉丝点击