多层下拉选择框的实现。

  1 package com.lixu.duocengxiala;
  2
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5
  6 import android.app.Activity;
  7 import android.content.Context;
  8 import android.os.Bundle;
  9 import android.view.LayoutInflater;
 10
 11 import android.view.View;
 12 import android.view.ViewGroup;
 13 import android.widget.AdapterView;
 14 import android.widget.AdapterView.OnItemSelectedListener;
 15 import android.widget.ArrayAdapter;
 16 import android.widget.Spinner;
 17 import android.widget.TextView;
 18
 19 public class MainActivity extends Activity {
 20     private static final String CITY = "city";
 21     ArrayList<String> citys = null;
 22     ArrayList<String> qu = null;
 23     int p = 0;
 24
 25     @Override
 26     protected void onCreate(Bundle savedInstanceState) {
 27         super.onCreate(savedInstanceState);
 28         setContentView(R.layout.activity_main);
 29
 30         String[] sheng = this.getResources().getStringArray(R.array.sheng);
 31         final String[] city_sichuan = this.getResources().getStringArray(R.array.city_sichuan);
 32         final String[] city_guangdong = this.getResources().getStringArray(R.array.city_guangdong);
 33         final String[] city_shanxi = this.getResources().getStringArray(R.array.city_shanxi);
 34         final String[] qu_chengdu = this.getResources().getStringArray(R.array.qu_chengdu);
 35         final String[] qu_mianyang = this.getResources().getStringArray(R.array.qu_mianyang);
 36         final String[] qu_guangyuan = this.getResources().getStringArray(R.array.qu_guangyuan);
 37         final String[] qu_taiyuan = this.getResources().getStringArray(R.array.qu_taiyuan);
 38         final String[] qu_guangzhou = this.getResources().getStringArray(R.array.qu_guangzhou);
 39
 40         citys = new ArrayList<String>();
 41         qu = new ArrayList<String>();
 42
 43         Spinner sp1 = (Spinner) findViewById(R.id.sp1);
 44         Spinner sp2 = (Spinner) findViewById(R.id.sp2);
 45         Spinner sp3 = (Spinner) findViewById(R.id.sp3);
 46
 47         final MyAdapter1 mMyAdapter1 = new MyAdapter1(this, -1, sheng);
 48         final MyAdapter2 mMyAdapter2 = new MyAdapter2(this, -1, citys);
 49         final MyAdapter3 mMyAdapter3 = new MyAdapter3(this, -1, qu);
 50
 51         sp1.setAdapter(mMyAdapter1);
 52         sp2.setAdapter(mMyAdapter2);
 53         sp3.setAdapter(mMyAdapter3);
 54
 55         sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
 56
 57             @Override
 58             public void onItemSelected(AdapterView<?> arg0, View arg1, int possheng, long arg3) {
 59                 p = possheng;
 60                 switch (possheng) {
 61                 case 0:
 62                     citys.clear();
 63                     for (String n : city_sichuan)
 64                         citys.add(n);
 65                     mMyAdapter2.notifyDataSetChanged();
 66
 67                     break;
 68                 case 1:
 69                     citys.clear();
 70                     for (String n : city_shanxi)
 71                         citys.add(n);
 72                     mMyAdapter2.notifyDataSetChanged();
 73
 74                     break;
 75                 case 2:
 76
 77                     citys.clear();
 78                     for (String n : city_guangdong)
 79                         citys.add(n);
 80                     mMyAdapter2.notifyDataSetChanged();
 81                     break;
 82
 83                 default:
 84                     break;
 85                 }
 86             }
 87
 88             @Override
 89             public void onNothingSelected(AdapterView<?> arg0) {
 90
 91             }
 92         });
 93         sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
 94
 95             @Override
 96             public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long arg3) {
 97
 98                 switch (pos) {
 99                 case 0:
100                     if (p == 0) {
101
102                         qu.clear();
103
104                         for (String n : qu_chengdu)
105                             qu.add(n);
106                         mMyAdapter3.notifyDataSetChanged();
107
108                     } else if (p == 1) {
109                         qu.clear();
110                         for (String n : qu_taiyuan)
111                             qu.add(n);
112                         mMyAdapter3.notifyDataSetChanged();
113                     } else if (p == 2) {
114                         qu.clear();
115                         for (String n : qu_guangzhou)
116                             qu.add(n);
117                         mMyAdapter3.notifyDataSetChanged();
118                     }
119
120                     break;
121                 case 1:
122                     if (p == 0) {
123                         qu.clear();
124                         for (String n : qu_mianyang)
125                             qu.add(n);
126                         mMyAdapter3.notifyDataSetChanged();
127                     }
128                     break;
129                 case 2:
130                     if (p == 0) {
131                         qu.clear();
132                         for (String n : qu_guangyuan)
133                             qu.add(n);
134                         mMyAdapter3.notifyDataSetChanged();
135                     }
136                     break;
137
138                 default:
139                     break;
140                 }
141             }
142
143             @Override
144             public void onNothingSelected(AdapterView<?> arg0) {
145
146             }
147         });
148
149     }
150
151     private class MyAdapter1 extends ArrayAdapter {
152         private Context context;
153         private String[] sheng;
154         private LayoutInflater flater;
155
156         public MyAdapter1(Context context, int resource, String[] sheng) {
157             super(context, resource, sheng);
158             this.context = context;
159             this.sheng = sheng;
160             flater = LayoutInflater.from(context);
161         }
162
163         @Override
164         public int getCount() {
165             return sheng.length;
166         }
167
168         @Override
169         public View getView(int position, View convertView, ViewGroup parent) {
170             if (convertView == null)
171                 convertView = flater.inflate(R.layout.list, null);
172
173             TextView tv = (TextView) convertView.findViewById(R.id.tv1);
174             tv.setText(sheng[position]);
175
176             return convertView;
177         }
178
179         @Override
180         public View getDropDownView(int position, View convertView, ViewGroup parent) {
181             if (convertView == null)
182                 convertView = flater.inflate(R.layout.list, null);
183
184             TextView tv = (TextView) convertView.findViewById(R.id.tv1);
185             tv.setText(sheng[position]);
186
187             return convertView;
188
189         }
190
191     }
192
193     private class MyAdapter2 extends ArrayAdapter {
194         private Context context;
195         private ArrayList<String> citys;
196         private LayoutInflater flater;
197
198         public MyAdapter2(Context context, int resource, ArrayList<String> citys) {
199             super(context, resource, citys);
200             this.context = context;
201             this.citys = citys;
202             flater = LayoutInflater.from(context);
203         }
204
205         @Override
206         public int getCount() {
207             return citys.size();
208         }
209
210         @Override
211         public View getView(int position, View convertView, ViewGroup parent) {
212             if (convertView == null)
213                 convertView = flater.inflate(R.layout.list, null);
214
215             TextView tv = (TextView) convertView.findViewById(R.id.tv1);
216             tv.setText(citys.get(position) + "");
217
218             return convertView;
219         }
220
221         @Override
222         public View getDropDownView(int position, View convertView, ViewGroup parent) {
223             if (convertView == null)
224                 convertView = flater.inflate(R.layout.list, null);
225
226             TextView tv = (TextView) convertView.findViewById(R.id.tv1);
227             tv.setText(citys.get(position));
228
229             return convertView;
230
231         }
232
233     }
234
235     private class MyAdapter3 extends ArrayAdapter {
236         private Context context;
237         private ArrayList<String> qu;
238         private LayoutInflater flater;
239
240         public MyAdapter3(Context context, int resource, ArrayList<String> qu) {
241             super(context, resource, qu);
242             this.context = context;
243             this.qu = qu;
244             flater = LayoutInflater.from(context);
245         }
246
247         @Override
248         public int getCount() {
249             return qu.size();
250         }
251
252         @Override
253         public View getView(int position, View convertView, ViewGroup parent) {
254             if (convertView == null)
255                 convertView = flater.inflate(R.layout.list, null);
256
257             TextView tv = (TextView) convertView.findViewById(R.id.tv1);
258             tv.setText(qu.get(position));
259
260             return convertView;
261         }
262
263         @Override
264         public View getDropDownView(int position, View convertView, ViewGroup parent) {
265             if (convertView == null)
266                 convertView = flater.inflate(R.layout.list, null);
267
268             TextView tv = (TextView) convertView.findViewById(R.id.tv1);
269             tv.setText(qu.get(position));
270
271             return convertView;
272
273         }
274
275     }
276
277 }

vales XML:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3
 4     <string-array name="sheng">
 5         <item>四川省</item>
 6         <item>山西省</item>
 7         <item>广东省</item>
 8     </string-array>
 9
10     <string-array name="city_sichuan">
11         <item>成都</item>
12         <item>绵阳</item>
13         <item>广元</item>
14         <item>巴中</item>
15         <item>乐山</item>
16         <item>眉山</item>
17     </string-array>
18
19     <string-array name="city_shanxi">
20         <item>太原</item>
21         <item>晋中</item>
22         <item>介休</item>
23         <item>大同</item>
24         <item>运城</item>
25         <item>长治</item>
26     </string-array>
27
28     <string-array name="city_guangdong">
29         <item>广州</item>
30         <item>深圳</item>
31         <item>东莞</item>
32         <item>云浮</item>
33         <item>珠海</item>
34         <item>湛江</item>
35     </string-array>
36
37     <string-array name="qu_chengdu">
38         <item>金牛区</item>
39         <item>青羊区</item>
40         <item>成华区</item>
41         <item>高新区</item>
42         <item>武侯区</item>
43         <item>锦江区</item>
44     </string-array>
45
46     <string-array name="qu_taiyuan">
47         <item>迎泽区</item>
48         <item>万柏林区</item>
49         <item>杏花岭区</item>
50         <item>小店区</item>
51         <item>尖草坪区</item>
52         <item>晋源区</item>
53     </string-array>
54
55     <string-array name="qu_guangzhou">
56         <item>荔湾区</item>
57         <item>海珠区</item>
58         <item>越秀区</item>
59         <item>天河区</item>
60         <item>白云区</item>
61         <item>花都区</item>
62     </string-array>
63
64     <string-array name="qu_mianyang">
65         <item>培城区</item>
66         <item>游仙区</item>
67     </string-array>
68
69     <string-array name="qu_guangyuan">
70         <item>旺苍区</item>
71         <item>剑阁区</item>
72     </string-array>
73
74 </resources>

xml文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="horizontal"
 7     tools:context="com.lixu.duocengxiala.MainActivity" >
 8
 9     <Spinner
10         android:id="@+id/sp1"
11         android:layout_weight="1"
12         android:layout_width="wrap_content"
13         android:layout_height="wrap_content"
14         />
15      <Spinner
16         android:id="@+id/sp2"
17          android:layout_weight="1"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         />
21       <Spinner
22         android:id="@+id/sp3"
23          android:layout_weight="1"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         />
27
28 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6
 7     <TextView
 8         android:id="@+id/tv1"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content" />
11
12 </LinearLayout>

程序运行效果图:

时间: 2024-10-07 22:33:48

多层下拉选择框的实现。的相关文章

Bootstrap系列 -- 15. 下拉选择框select

Bootstrap框架中的下拉选择框使用和原始的一致,多行选择设置multiple属性的值为multiple.Bootstrap框架会为这些元素提供统一的样式风格 <form role="form"> <div class="form-group"> <select class="form-control"> <option>1</option> <option>2</

下拉选择框改变表单action的简单例子

做了个简单的小例子 用js弄的. form  的代码 1 <form  action=""  method="post" id="formRole"> type的 下拉单 1 2 3 4 5 6 <select name="type" id="type"  onchange="changeAction()">       <option value=&quo

一款javascript实现的超炫的下拉选择框

今天为给大家带来一款javascript实现的超炫的下拉选择框.下拉的列表由半用透明的幽灵按钮组成.显示下拉的时候,列表项由左右两侧飞入.消息时飞向左右两侧.一起看下效果图 在线预览   源码下载 实现的代码. html代码: <div class="scifiUI"> <h1> Scifi Dropdown</h1> <ul> <li>Interactive</li> <li>Animated<

第三章:Select 下拉选择框的练习

1. 练习 Select 下拉选择框的练习. 2. 针对于这样的情况,如何来定位元素: 3. 总结: 看到下拉选择框,标准的 Select 下拉框时,可以选择合适的页面元素来定位.

基于jQuery美化联动下拉选择框

今天给大家介绍一款基于jQuery美化联动下拉选择框.这款下下拉选择框js里自带了全国所有城市的数数库.下拉选择框适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id="container"> <div class="inner"> <section id="main_content"

[jQueryUI] - Chosen:select下拉选择框美化插件及问题【转】

hosen 是一个支持jquery的select下拉框美化插件,它能让丑陋的.很长的select选择框变的更好看.更方便.不仅如此,它更扩展了select,增加了自动筛选的功能.它可对列表进行分组,同时也可禁用某些选择项. 先来看下插件的效果:   跟这个比起来,原来的select样式是不是弱爆了! 马上来武装我们的select吧: 1.先把js和css文件引用到网页里面去: ? 1 2 3 <link href="js/jqueryUI/chosen/chosen.css" t

bootstrap-表单控件——下拉选择框select元素

1.运行效果如图所示 2.实现代码如下 <!DOCTYPE html> <html> <head>     <meta charset="utf-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <title>表单控件--下拉选择框select元</title>     <!-

jQuery插件——下拉选择框

其实,之前也写过jQuery插件,今天写的是一个模拟select选择的下拉插件. 既然是jQuery插件,那么必然是依赖jQuery的了. 老规矩,直接上代码吧! ;(function () { $.fn.extend({ dropdown:function (options) { return this.each(function (i, val) { $(val).css("position", "relative"); // 设置默认参数 var opts =

jquery.chosen.js下拉选择框美化插件项目实例

由于之前使用的bootstrap-select插件是建立在bootstrap基础上的,实际使用到项目中的时候,与我们使用的ace-admin(基于bootstrap)存在样式冲突,导致下拉框的样式发生变化.为了界面的美观,不得已查资料寻找另外的插件. 使用jquery.chosen.js下拉选择框美化插件同样也能达到类似效果 完成效果如下 实现步骤如下 1.导入相关文件 <link rel="stylesheet" href="${ctxStatic}/css/chos