省市级三级联动

Posted z-cg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了省市级三级联动相关的知识,希望对你有一定的参考价值。

前言:

在我们输入信息是经常会让我们输入户籍所在地,此时省市级三级联动就很有必要了

正文:

首先在xml文件中添加如下代码

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10 
11 
12     tools:context="com.example.aaaaa.sp.MainActivity">
13 
14     <TextView
15         android:id="@+id/textview1"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="请选择您所在的城市"
19         android:textSize="20sp" />
20 
21     <LinearLayout
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content"
24         android:layout_marginTop="30dp"
25 
26         >
27 
28         <Spinner
29 
30             android:id="@+id/provicespinner"
31             android:layout_width="match_parent"
32             android:layout_height="wrap_content"
33             android:layout_weight="1">
34 
35         </Spinner>
36 
37         <Spinner
38             android:id="@+id/cityspinner"
39             android:layout_width="match_parent"
40             android:layout_height="wrap_content"
41             android:layout_toRightOf="@id/provicespinner"
42             android:layout_weight="1">
43 
44         </Spinner>
45 
46         <Spinner
47             android:id="@+id/countyspinner"
48             android:layout_width="match_parent"
49             android:layout_height="wrap_content"
50             android:layout_toRightOf="@id/cityspinner"
51             android:layout_weight="1">
52 
53         </Spinner>
54     </LinearLayout>
55 
56     <TextView
57         android:id="@+id/textview"
58         android:layout_width="match_parent"
59         android:layout_height="wrap_content"
60         android:layout_alignParentLeft="true"
61         android:layout_alignParentStart="true"
62         android:layout_centerVertical="true"
63         android:gravity="center_horizontal"
64         android:text="显示城市"
65         android:textSize="20sp" />
66 </RelativeLayout>

Java代码如:

 1 import android.support.v7.app.AppCompatActivity;
 2 import android.os.Bundle;
 3 import android.view.View;
 4 import android.widget.AdapterView;
 5 import android.widget.ArrayAdapter;
 6 import android.widget.Spinner;
 7 import android.widget.TextView;
 8 
 9 import java.util.ArrayList;
10 import java.util.List;
11 
12 public class MainActivity extends AppCompatActivity {
13     private Spinner provicespinner;
14     private Spinner cityspinner;
15     private Spinner countyspinner;
16     private ArrayAdapter<String> priviceAdapter;
17     private ArrayAdapter cityAdapter;
18     private ArrayAdapter contyAdapter;
19     private int privicePosition;
20     private int cityPosition;
21     private int countyPosition;
22     private String[] provice = {"山西", "江苏", "安徽"};
23     private String[][] city = {{"太原", "大同", "阳泉"}, {"徐州", "南京", "苏州"}, {"宿州", "合肥", "淮北"}};
24     private String[][][] county = {{{"小店区", "迎泽区", "尖草坪区"}, {"阳高县", "天镇县", "浑源县"}, {"城区",
25             "郊区", "矿区"}}, {{"沛县", "丰县", "临空新城"}, {"玄武", "鼓楼", "下关"}, {"虎丘区", "吴中区",
26             "吴江区"}}, {{"砀山", "萧县", "涌泉区"}, {"肥东县", "肥西县", "长丰县"}, {"杜集区", "相山区", "烈山区"}}};
27     private TextView textView;
28 
29     @Override
30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.activity_main);
33         textView = (TextView) findViewById(R.id.textview);
34         provicespinner = (Spinner) findViewById(R.id.provicespinner);
35         cityspinner = (Spinner) findViewById(R.id.cityspinner);
36         countyspinner = (Spinner) findViewById(R.id.countyspinner);
37         //显示省
38         priviceAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, provice);
39         provicespinner.setAdapter(priviceAdapter);
40         //设置省级的监听器--目的是在你选择省后直接显示对应的市,最主要的是应用position
41         provicespinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
42             @Override
43             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
44                 cityAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, city[position]);
45                 cityspinner.setAdapter(cityAdapter);
46                 privicePosition = position;
47 
48             }
49 
50             @Override
51             public void onNothingSelected(AdapterView<?> parent) {
52 
53             }
54         });
55         //给市spinner设置监听器
56         cityspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
57             @Override
58             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
59                 contyAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, county[privicePosition][position]);
60                 countyspinner.setAdapter(contyAdapter);
61                 //为能把此时的position传出去
62                 cityPosition = position;
63 
64 
65             }
66 
67             @Override
68             public void onNothingSelected(AdapterView<?> parent) {
69 
70             }
71         });
72         //为县设置适配器目的是为了显示你算选的省市县
73         countyspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
74             @Override
75             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
76                 //在下面所选内容显示
77                 textView.setText("选择的城市为" + " " + provice[privicePosition] + " " + city[privicePosition][cityPosition] + " " + county[privicePosition][cityPosition][position]);
78             }
79 
80             @Override
81             public void onNothingSelected(AdapterView<?> parent) {
82 
83             }
84         });
85     }
86 }

这样实现其实就是练练手,在实际的操作中可以说用到的东西复杂的多,上面注释解释的很详细就不一步步的解释,重要的就是运用spinner的监听器里面的参数position,从而提取出上面定义的几个数组中数据

 

以上是关于省市级三级联动的主要内容,如果未能解决你的问题,请参考以下文章

省市级三级联动

ajax省市区三级联动

三级联动 省市 ajax的代码

三级联动 省市 ajax的代码

省市区三级菜单联动插件

省市区三级联动