Android练习——Spinner二级联动_城市选择

时间:2023-02-13 12:07:37

样式部分xml

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <TextView
12         android:id="@+id/textView1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="请选择城市" />
16 
17     <Spinner
18         android:id="@+id/spinner1"
19         android:layout_width="wrap_content"
20         android:layout_height="wrap_content"
21         android:layout_alignLeft="@+id/textView1"
22         android:layout_below="@+id/textView1" />
23 
24     <Spinner
25         android:id="@+id/spinner2"
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:layout_alignLeft="@+id/spinner1"
29         android:layout_below="@+id/spinner1" />
30 
31 </RelativeLayout>

arrays部分xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3    <string-array name="province">
 4         <item>-省份-</item>
 5         <item>北京市</item>
 6         <item>河北省</item>
 7     </string-array>
 8     <string-array name="hb">
 9         <item>-城市-</item>
10         <item>承德市</item>
11         <item>石家庄</item>
12         <item>邯郸市</item>
13         <item>邢台市</item>
14         <item>廊坊市</item>
15     </string-array>
16     <string-array name="bj">
17         <item>-城市-</item>
18         <item>海淀区</item>
19         <item>朝阳区</item>
20         <item>崇文区</item>
21         <item>昌平区</item>
22     </string-array>
23 </resources>            

Activity 部分

 1 package com.example.android_spinner;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 import android.view.View;
 7 import android.widget.AdapterView;
 8 import android.widget.ArrayAdapter;
 9 import android.widget.Spinner;
10 
11 public class MainActivity extends Activity {
12     // 声明组件
13     private Spinner sr1;
14     private Spinner sr2;
15 
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.activity_main);
20         // 获取组件
21         sr1 = (Spinner) this.findViewById(R.id.spinner1);
22         sr2 = (Spinner) this.findViewById(R.id.spinner2);
23         // 添加String数组
24         String[] province = getResources().getStringArray(R.array.province);
25         ;
26         // 将数组配置器链接上预置的文件
27         // ArrayAdapter<CharSequence> aa = new ArrayAdapter<CharSequence>(this,
28         // android.R.layout.simple_spinner_item,province);
29 
30         // 这样就可以直接获取xml中的数组了
31         ArrayAdapter<CharSequence> aa = ArrayAdapter.createFromResource(this,
32                 R.array.province, android.R.layout.simple_spinner_item);
33 
34         // 设置下拉菜单的样式
35         aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
36         // 将数据绑定到spinner上
37         sr1.setAdapter(aa);
38 
39         // 添加监听事件
40 
41         sr1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
42 
43             @Override
44             public void onItemSelected(AdapterView<?> arg0, View arg1,
45                     int arg2, long arg3) {
46                 // 得到事件中选中的值
47                 Spinner s = (Spinner) arg0;
48                 String pro = (String) s.getItemAtPosition(arg2);
49                 ArrayAdapter<CharSequence> cityAdapter = null;
50                 // 获取所在省含有哪些市(从资源数组文件中获取数据)
51                 if (pro.equals("河北省")) {
52 
53                     cityAdapter = ArrayAdapter.createFromResource(
54                             MainActivity.this, R.array.hb,
55                             android.R.layout.simple_spinner_item);
56                 } else if (pro.equals("北京市")) {
57 
58                     cityAdapter = ArrayAdapter.createFromResource(
59                             MainActivity.this, R.array.bj,
60                             android.R.layout.simple_spinner_item);
61                 } 
62                 sr2.setAdapter(cityAdapter);
63             }
64 
65             @Override
66             public void onNothingSelected(AdapterView<?> arg0) {
67                 // TODO Auto-generated method stub
68 
69             }
70 
71         });
72 
73     }
74 
75     @Override
76     public boolean onCreateOptionsMenu(Menu menu) {
77         // Inflate the menu; this adds items to the action bar if it is present.
78         getMenuInflater().inflate(R.menu.main, menu);
79         return true;
80     }
81 
82 }

实现效果:

Android练习——Spinner二级联动_城市选择