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

省市区三级联动

程序员文章站 2022-06-11 20:42:38
...

针对AdapterView的拓展使用,Spinner实现省市区的三级联动。

其主要是通过使用Spinner的setOnItemSelectListener来实现。

代码示例:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.testspinnerprovince.MainActivity"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:text="@string/selection"
        android:padding="6dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:orientation="horizontal">

        <Spinner
            android:id="@+id/provinceSpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Spinner
            android:id="@+id/citySpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
        <Spinner
            android:id="@+id/countySpinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    </LinearLayout>
    <TextView
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:padding="6dp"
        android:layout_marginTop="80dp"
        android:text="@string/selected"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private TextView show;
    private Spinner provinceSpinner;//省级
    private Spinner citySpinner;//市级
    private Spinner countySpinner;//区级

    private String[] province = new String[]{"北京","上海","天津","广东"};
    private String[][] city = new String[][]{
            {"东城区","西城区","崇文区","宣武区","朝阳区","海淀区","丰台区","石景山区","门头沟区",
                    "房山区","通州区","顺义区","大兴区","昌平区","平谷区","怀柔区","密云县","延庆县"},
    {"长宁区","静安区","普陀区","闸北区","虹口区"},
    {"和平区","河东区","河西区","南开区","河北区","红桥区","塘沽区","汉沽区","大港区","东丽区"},
    {"广州","深圳","韶关"}};
    private String[][][] county = new String[][][]{{
            //北京
            {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},
            {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},},{
            //上海
            {"无"},{"无"},{"无"},{"无"},{"无"},},{
            //天津
            {"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},{"无"},},{
            //广东
            {"海珠区","荔湾区","越秀区","白云区","萝岗区","天河区","黄浦区","花都区","从化市","增城市"
                    ,"番禺区","南沙区"},
            {"宝安区","福田区","龙岗区","罗湖区","南山区","盐田区"},
            {"武江区","浈江区","曲江区","乐昌市","南雄市","始兴县","仁化县","翁源县","新丰县","乳源县"}
    }
    };

    //Spinner想要填充肯定需要适配器
    private ArrayAdapter<String> provinceAdapter;
    private ArrayAdapter<String> cityAdapter;
    private ArrayAdapter<String> countyAdapter;
    private int provicePosition;//省级选中索引
    private int cityPosition;//市级选中索引

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


    //控件初始化
    private void initViews() {
        provinceSpinner = (Spinner) findViewById(R.id.provinceSpinner);
        citySpinner = (Spinner) findViewById(R.id.citySpinner);
        countySpinner = (Spinner) findViewById(R.id.countySpinner);
        show = (TextView) findViewById(R.id.show);

        //初始化适配器及显示的内容
        provinceAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,province);
        provinceSpinner.setAdapter(provinceAdapter);


        //对省级下拉实现监听,市级下拉需要根据不同的省级显示不同内容。
        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //需要在item选中省级的时候,动态的改变市级对应的显示
                cityAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,city[position]);
                //设置二级下拉列表当中选项内容适配器
                citySpinner.setAdapter(cityAdapter);
                //记录当前的省级索引位置,留给下面修改县级进行适配使用
                provicePosition = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //市级下拉监听
        citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //需要在item选中市级的时候,动态的改变县级对应的显示
                countyAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,county[provicePosition][position]);//首先确定是哪个省的。
                //设置二级下拉列表当中选项内容适配器
                countySpinner.setAdapter(countyAdapter);
                show.setText("选中的城市为:"+ province[provicePosition] + city[provicePosition][position]);
                cityPosition = position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //区级的下拉
        countySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                show.setText("选中的城市为:"+ province[provicePosition] + city[provicePosition][cityPosition]
                        + county[provicePosition][cityPosition][position]);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }


}

运行结果:

省市区三级联动