码迷,mamicode.com
首页 > 移动开发 > 详细

7.Android开源项目WheelView的时间和地址联动选择对话框

时间:2016-12-01 14:45:37      阅读:512      评论:0      收藏:0      [点我收藏+]

标签:method   content   src   try   demo   smis   author   return   apk   

类似WheelView的时间和地址联动选择对话框在现在App经常看到,今天小结下。

主布局界面:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:background="#ffffff"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/tv_birth"
 9         android:layout_width="match_parent"
10         android:layout_height="50dip"
11         android:layout_marginLeft="12dip"
12         android:layout_marginRight="12dip"
13         android:layout_marginTop="50dip"
14         android:background="@drawable/btn_blue_selector"
15         android:gravity="center"
16         android:text="选择时间" />
17     <TextView 
18         android:id="@+id/tv_address"
19         android:layout_width="match_parent"
20         android:layout_height="50dip"
21         android:layout_marginLeft="12dip"
22         android:layout_marginRight="12dip"
23         android:layout_marginTop="24dip"
24         android:background="@drawable/btn_blue_selector"
25         android:gravity="center"
26         android:text="选择地点"/>
27 
28 </LinearLayout>

选择时间布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/ly_myinfo_changebirth"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:background="#00000000"
 7     android:gravity="center"
 8     android:orientation="vertical" >
 9 
10     <LinearLayout
11         android:id="@+id/ly_myinfo_changebirth_child"
12         android:layout_width="300dip"
13         android:layout_height="wrap_content"
14         android:layout_marginLeft="27dip"
15         android:layout_marginRight="27dip"
16         android:background="@drawable/bg_dialog_selector"
17         android:orientation="vertical" >
18 
19         <TextView
20             android:id="@+id/tv_share_title"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:layout_marginBottom="14dip"
24             android:layout_marginLeft="18dip"
25             android:layout_marginTop="18dip"
26             android:gravity="center_vertical"
27             android:text="选择日期"
28             android:textColor="#000000"
29             android:textSize="18sp" />
30         <LinearLayout 
31             android:layout_width="match_parent"
32             android:layout_height="160dip"
33             android:orientation="horizontal"
34             android:layout_marginLeft="20dip"
35             android:layout_marginRight="20dip"
36             android:gravity="center_vertical">
37             <com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
38                 android:id="@+id/wv_birth_year"
39                 android:layout_width="0dip"
40                 android:layout_weight="1"
41                 android:layout_height="match_parent"
42                 android:layout_gravity="center_vertical"
43                 />
44             <com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
45                 android:id="@+id/wv_birth_month"
46                 android:layout_width="0dip"
47                 android:layout_weight="1"
48                 android:layout_height="match_parent"
49                 android:layout_gravity="center_vertical"
50                 />
51             <com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
52                 android:id="@+id/wv_birth_day"
53                 android:layout_width="0dip"
54                 android:layout_weight="1"
55                 android:layout_height="match_parent"
56                 android:layout_gravity="center_vertical"
57                 />
58         </LinearLayout>
59         <RelativeLayout
60             android:layout_width="match_parent"
61             android:layout_height="wrap_content"
62             android:layout_marginTop="25dip"
63             android:layout_marginBottom="21dip">
64 
65                 <TextView
66                     android:id="@+id/btn_myinfo_sure"
67                     android:layout_width="77dip"
68                     android:layout_height="33dip"
69                     android:layout_alignParentRight="true"
70                     android:layout_marginRight="15dip"
71                     android:text="确认"
72                     android:gravity="center"
73                     android:textSize="16sp" />
74 
75 
76                 <TextView
77                     android:id="@+id/btn_myinfo_cancel"
78                     android:layout_width="77dip"
79                     android:layout_height="33dip"
80                     android:layout_toLeftOf="@+id/btn_myinfo_sure"
81                     android:text="取消"
82                     android:gravity="center"
83                     android:textSize="16sp" />
84         </RelativeLayout>
85     </LinearLayout>
86 
87 </LinearLayout>

选择地点布局:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:app="http://schemas.android.com/apk/res-auto"
 4     android:id="@+id/ly_myinfo_changeaddress"
 5     android:layout_width="match_parent"
 6     android:layout_height="match_parent"
 7     android:background="#00000000"
 8     android:gravity="center"
 9     android:orientation="vertical" >
10 
11     <LinearLayout
12         android:id="@+id/ly_myinfo_changeaddress_child"
13         android:layout_width="300dip"
14         android:layout_height="wrap_content"
15         android:layout_marginLeft="27dip"
16         android:layout_marginRight="27dip"
17         android:background="@drawable/bg_dialog_selector"
18         android:orientation="vertical" >
19 
20         <TextView
21             android:id="@+id/tv_share_title"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:layout_marginBottom="14dip"
25             android:layout_marginLeft="18dip"
26             android:layout_marginTop="18dip"
27             android:gravity="center_vertical"
28             android:text="选择地区"
29             android:textColor="#000000"
30             android:textSize="18sp" />
31         <LinearLayout 
32             android:layout_width="match_parent"
33             android:layout_height="160dip"
34             android:orientation="horizontal"
35             android:layout_marginLeft="20dip"
36             android:layout_marginRight="20dip"
37             android:gravity="center_vertical">
38             <com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
39                 android:id="@+id/wv_address_province"
40                 android:layout_width="0dip"
41                 android:layout_weight="1"
42                 android:layout_height="match_parent"
43                 android:layout_gravity="center_vertical"
44                 />
45             <com.example.riger.wheelviewdemo.wheel.widget.views.WheelView
46                 android:id="@+id/wv_address_city"
47                 android:layout_width="0dip"
48                 android:layout_weight="1"
49                 android:layout_height="match_parent"
50                 android:layout_gravity="center_vertical"
51                 />
52         </LinearLayout>
53         <RelativeLayout
54             android:layout_width="match_parent"
55             android:layout_height="wrap_content"
56             android:layout_marginTop="25dip"
57             android:layout_marginBottom="21dip" >
58 
59                 <TextView
60                     android:id="@+id/btn_myinfo_sure"
61                     android:layout_width="77dip"
62                     android:layout_height="33dip"
63                     android:text="确认"
64                     android:gravity="center"
65                     android:layout_alignParentRight="true"
66                     android:layout_marginRight="15dip"
67                     android:textSize="16sp" />
68 
69 
70                 <TextView
71                     android:id="@+id/btn_myinfo_cancel"
72                     android:layout_width="77dip"
73                     android:layout_height="33dip"
74                     android:layout_toLeftOf="@+id/btn_myinfo_sure"
75                     android:gravity="center"
76                     android:text="取消"
77                     android:textSize="16sp" />
78         </RelativeLayout>
79     </LinearLayout>
80 
81 </LinearLayout>

时间对话框代码:

  1 package com.example.riger.wheelviewdemo;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Calendar;
  5 
  6 import android.app.Dialog;
  7 import android.content.Context;
  8 import android.os.Bundle;
  9 import android.view.View;
 10 import android.view.ViewGroup;
 11 import android.widget.TextView;
 12 
 13 import com.example.riger.wheelviewdemo.wheel.widget.adapters.AbstractWheelTextAdapter;
 14 import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelChangedListener;
 15 import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelScrollListener;
 16 import com.example.riger.wheelviewdemo.wheel.widget.views.WheelView;
 17 
 18 /**
 19  * 日期选择对话框
 20  * 
 21  * @author ywl
 22  *
 23  */
 24 public class ChangeBirthDialog extends Dialog implements android.view.View.OnClickListener {
 25 
 26     private Context context;
 27     private WheelView wvYear;
 28     private WheelView wvMonth;
 29     private WheelView wvDay;
 30 
 31     private View vChangeBirth;
 32     private View vChangeBirthChild;
 33     private TextView btnSure;
 34     private TextView btnCancel;
 35 
 36     private ArrayList<String> arry_years = new ArrayList<String>();
 37     private ArrayList<String> arry_months = new ArrayList<String>();
 38     private ArrayList<String> arry_days = new ArrayList<String>();
 39     private CalendarTextAdapter mYearAdapter;
 40     private CalendarTextAdapter mMonthAdapter;
 41     private CalendarTextAdapter mDaydapter;
 42 
 43     private int month;
 44     private int day;
 45 
 46     private int currentYear = getYear();
 47     private int currentMonth = 1;
 48     private int currentDay = 1;
 49 
 50     private int maxTextSize = 24;
 51     private int minTextSize = 14;
 52 
 53     private boolean issetdata = false;
 54 
 55     private String selectYear;
 56     private String selectMonth;
 57     private String selectDay;
 58 
 59     private OnBirthListener onBirthListener;
 60 
 61     public ChangeBirthDialog(Context context) {
 62         super(context, R.style.ShareDialog);
 63         this.context = context;
 64     }
 65 
 66     @Override
 67     protected void onCreate(Bundle savedInstanceState) {
 68         super.onCreate(savedInstanceState);
 69         setContentView(R.layout.dialog_myinfo_changebirth);
 70         wvYear = (WheelView) findViewById(R.id.wv_birth_year);
 71         wvMonth = (WheelView) findViewById(R.id.wv_birth_month);
 72         wvDay = (WheelView) findViewById(R.id.wv_birth_day);
 73 
 74         vChangeBirth = findViewById(R.id.ly_myinfo_changebirth);
 75         vChangeBirthChild = findViewById(R.id.ly_myinfo_changebirth_child);
 76         btnSure = (TextView) findViewById(R.id.btn_myinfo_sure);
 77         btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel);
 78 
 79         vChangeBirth.setOnClickListener(this);
 80         vChangeBirthChild.setOnClickListener(this);
 81         btnSure.setOnClickListener(this);
 82         btnCancel.setOnClickListener(this);
 83 
 84         if (!issetdata) {
 85             initData();
 86         }
 87         initYears();
 88         mYearAdapter = new CalendarTextAdapter(context, arry_years, setYear(currentYear), maxTextSize, minTextSize);
 89         wvYear.setVisibleItems(5);
 90         wvYear.setViewAdapter(mYearAdapter);
 91         wvYear.setCurrentItem(setYear(currentYear));
 92 
 93         initMonths(month);
 94         mMonthAdapter = new CalendarTextAdapter(context, arry_months, setMonth(currentMonth), maxTextSize, minTextSize);
 95         wvMonth.setVisibleItems(5);
 96         wvMonth.setViewAdapter(mMonthAdapter);
 97         wvMonth.setCurrentItem(setMonth(currentMonth));
 98 
 99         initDays(day);
100         mDaydapter = new CalendarTextAdapter(context, arry_days, currentDay - 1, maxTextSize, minTextSize);
101         wvDay.setVisibleItems(5);
102         wvDay.setViewAdapter(mDaydapter);
103         wvDay.setCurrentItem(currentDay - 1);
104 
105         wvYear.addChangingListener(new OnWheelChangedListener() {
106 
107             @Override
108             public void onChanged(WheelView wheel, int oldValue, int newValue) {
109                 // TODO Auto-generated method stub
110                 String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
111                 selectYear = currentText;
112                 setTextviewSize(currentText, mYearAdapter);
113                 currentYear = Integer.parseInt(currentText);
114                 setYear(currentYear);
115                 initMonths(month);
116                 mMonthAdapter = new CalendarTextAdapter(context, arry_months, 0, maxTextSize, minTextSize);
117                 wvMonth.setVisibleItems(5);
118                 wvMonth.setViewAdapter(mMonthAdapter);
119                 wvMonth.setCurrentItem(0);
120             }
121         });
122 
123         wvYear.addScrollingListener(new OnWheelScrollListener() {
124 
125             @Override
126             public void onScrollingStarted(WheelView wheel) {
127                 // TODO Auto-generated method stub
128 
129             }
130 
131             @Override
132             public void onScrollingFinished(WheelView wheel) {
133                 // TODO Auto-generated method stub
134                 String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
135                 setTextviewSize(currentText, mYearAdapter);
136             }
137         });
138 
139         wvMonth.addChangingListener(new OnWheelChangedListener() {
140 
141             @Override
142             public void onChanged(WheelView wheel, int oldValue, int newValue) {
143                 // TODO Auto-generated method stub
144                 String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
145                 selectMonth = currentText;
146                 setTextviewSize(currentText, mMonthAdapter);
147                 setMonth(Integer.parseInt(currentText));
148                 initDays(day);
149                 mDaydapter = new CalendarTextAdapter(context, arry_days, 0, maxTextSize, minTextSize);
150                 wvDay.setVisibleItems(5);
151                 wvDay.setViewAdapter(mDaydapter);
152                 wvDay.setCurrentItem(0);
153             }
154         });
155 
156         wvMonth.addScrollingListener(new OnWheelScrollListener() {
157 
158             @Override
159             public void onScrollingStarted(WheelView wheel) {
160                 // TODO Auto-generated method stub
161 
162             }
163 
164             @Override
165             public void onScrollingFinished(WheelView wheel) {
166                 // TODO Auto-generated method stub
167                 String currentText = (String) mMonthAdapter.getItemText(wheel.getCurrentItem());
168                 setTextviewSize(currentText, mMonthAdapter);
169             }
170         });
171 
172         wvDay.addChangingListener(new OnWheelChangedListener() {
173 
174             @Override
175             public void onChanged(WheelView wheel, int oldValue, int newValue) {
176                 // TODO Auto-generated method stub
177                 String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());
178                 setTextviewSize(currentText, mDaydapter);
179                 selectDay = currentText;
180             }
181         });
182 
183         wvDay.addScrollingListener(new OnWheelScrollListener() {
184 
185             @Override
186             public void onScrollingStarted(WheelView wheel) {
187                 // TODO Auto-generated method stub
188 
189             }
190 
191             @Override
192             public void onScrollingFinished(WheelView wheel) {
193                 // TODO Auto-generated method stub
194                 String currentText = (String) mDaydapter.getItemText(wheel.getCurrentItem());
195                 setTextviewSize(currentText, mDaydapter);
196             }
197         });
198 
199     }
200 
201     public void initYears() {
202         for (int i = getYear(); i > 1950; i--) {
203             arry_years.add(i + "");
204         }
205     }
206 
207     public void initMonths(int months) {
208         arry_months.clear();
209         for (int i = 1; i <= months; i++) {
210             arry_months.add(i + "");
211         }
212     }
213 
214     public void initDays(int days) {
215         arry_days.clear();
216         for (int i = 1; i <= days; i++) {
217             arry_days.add(i + "");
218         }
219     }
220 
221     private class CalendarTextAdapter extends AbstractWheelTextAdapter {
222         ArrayList<String> list;
223 
224         protected CalendarTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
225             super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
226             this.list = list;
227             setItemTextResource(R.id.tempValue);
228         }
229 
230         @Override
231         public View getItem(int index, View cachedView, ViewGroup parent) {
232             View view = super.getItem(index, cachedView, parent);
233             return view;
234         }
235 
236         @Override
237         public int getItemsCount() {
238             return list.size();
239         }
240 
241         @Override
242         protected CharSequence getItemText(int index) {
243             return list.get(index) + "";
244         }
245     }
246 
247     public void setBirthdayListener(OnBirthListener onBirthListener) {
248         this.onBirthListener = onBirthListener;
249     }
250 
251     @Override
252     public void onClick(View v) {
253 
254         if (v == btnSure) {
255             if (onBirthListener != null) {
256                 onBirthListener.onClick(selectYear, selectMonth, selectDay);
257             }
258         } else if (v == btnSure) {
259 
260         } else if (v == vChangeBirthChild) {
261             return;
262         } else {
263             dismiss();
264         }
265         dismiss();
266 
267     }
268 
269     public interface OnBirthListener {
270         public void onClick(String year, String month, String day);
271     }
272 
273     /**
274      * 设置字体大小
275      * 
276      * @param curriteItemText
277      * @param adapter
278      */
279     public void setTextviewSize(String curriteItemText, CalendarTextAdapter adapter) {
280         ArrayList<View> arrayList = adapter.getTestViews();
281         int size = arrayList.size();
282         String currentText;
283         for (int i = 0; i < size; i++) {
284             TextView textvew = (TextView) arrayList.get(i);
285             currentText = textvew.getText().toString();
286             if (curriteItemText.equals(currentText)) {
287                 textvew.setTextSize(maxTextSize);
288             } else {
289                 textvew.setTextSize(minTextSize);
290             }
291         }
292     }
293 
294     public int getYear() {
295         Calendar c = Calendar.getInstance();
296         return c.get(Calendar.YEAR);
297     }
298 
299     public int getMonth() {
300         Calendar c = Calendar.getInstance();
301         return c.get(Calendar.MONTH) + 1;
302     }
303 
304     public int getDay() {
305         Calendar c = Calendar.getInstance();
306         return c.get(Calendar.DATE);
307     }
308 
309     public void initData() {
310         setDate(getYear(), getMonth(), getDay());
311         this.currentDay = 1;
312         this.currentMonth = 1;
313     }
314 
315     /**
316      * 设置年月日
317      * 
318      * @param year
319      * @param month
320      * @param day
321      */
322     public void setDate(int year, int month, int day) {
323         selectYear = year + "";
324         selectMonth = month + "";
325         selectDay = day + "";
326         issetdata = true;
327         this.currentYear = year;
328         this.currentMonth = month;
329         this.currentDay = day;
330         if (year == getYear()) {
331             this.month = getMonth();
332         } else {
333             this.month = 12;
334         }
335         calDays(year, month);
336     }
337 
338     /**
339      * 设置年份
340      * 
341      * @param year
342      */
343     public int setYear(int year) {
344         int yearIndex = 0;
345         if (year != getYear()) {
346             this.month = 12;
347         } else {
348             this.month = getMonth();
349         }
350         for (int i = getYear(); i > 1950; i--) {
351             if (i == year) {
352                 return yearIndex;
353             }
354             yearIndex++;
355         }
356         return yearIndex;
357     }
358 
359     /**
360      * 设置月份
361      * 
362      * @param year
363      * @param month
364      * @return
365      */
366     public int setMonth(int month) {
367         int monthIndex = 0;
368         calDays(currentYear, month);
369         for (int i = 1; i < this.month; i++) {
370             if (month == i) {
371                 return monthIndex;
372             } else {
373                 monthIndex++;
374             }
375         }
376         return monthIndex;
377     }
378 
379     /**
380      * 计算每月多少天
381      * 
382      * @param month
383      * @param leayyear
384      */
385     public void calDays(int year, int month) {
386         boolean leayyear = false;
387         if (year % 4 == 0 && year % 100 != 0) {
388             leayyear = true;
389         } else {
390             leayyear = false;
391         }
392         for (int i = 1; i <= 12; i++) {
393             switch (month) {
394             case 1:
395             case 3:
396             case 5:
397             case 7:
398             case 8:
399             case 10:
400             case 12:
401                 this.day = 31;
402                 break;
403             case 2:
404                 if (leayyear) {
405                     this.day = 29;
406                 } else {
407                     this.day = 28;
408                 }
409                 break;
410             case 4:
411             case 6:
412             case 9:
413             case 11:
414                 this.day = 30;
415                 break;
416             }
417         }
418         if (year == getYear() && month == getMonth()) {
419             this.day = getDay();
420         }
421     }
422 }

选择地点对话框代码:

  1 package com.example.riger.wheelviewdemo;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStream;
  5 import java.util.ArrayList;
  6 import java.util.HashMap;
  7 import java.util.Map;
  8 
  9 import org.json.JSONArray;
 10 import org.json.JSONException;
 11 import org.json.JSONObject;
 12 
 13 import android.app.Dialog;
 14 import android.content.Context;
 15 import android.os.Bundle;
 16 import android.view.View;
 17 import android.view.ViewGroup;
 18 import android.widget.TextView;
 19 
 20 import com.example.riger.wheelviewdemo.wheel.widget.adapters.AbstractWheelTextAdapter;
 21 import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelChangedListener;
 22 import com.example.riger.wheelviewdemo.wheel.widget.views.OnWheelScrollListener;
 23 import com.example.riger.wheelviewdemo.wheel.widget.views.WheelView;
 24 
 25 /**
 26  * 更改封面对话框
 27  * 
 28  * @author ywl
 29  *
 30  */
 31 public class ChangeAddressDialog extends Dialog implements android.view.View.OnClickListener {
 32 
 33     private WheelView wvProvince;
 34     private WheelView wvCitys;
 35     private View lyChangeAddress;
 36     private View lyChangeAddressChild;
 37     private TextView btnSure;
 38     private TextView btnCancel;
 39 
 40     private Context context;
 41     private JSONObject mJsonObj;
 42     private String[] mProvinceDatas;
 43     private Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>();
 44 
 45     private ArrayList<String> arrProvinces = new ArrayList<String>();
 46     private ArrayList<String> arrCitys = new ArrayList<String>();
 47     private AddressTextAdapter provinceAdapter;
 48     private AddressTextAdapter cityAdapter;
 49 
 50     private String strProvince = "湖南";
 51     private String strCity = "长沙";
 52     private OnAddressCListener onAddressCListener;
 53 
 54     private int maxsize = 24;
 55     private int minsize = 14;
 56 
 57     public ChangeAddressDialog(Context context) {
 58         super(context, R.style.ShareDialog);
 59         this.context = context;
 60     }
 61 
 62     @Override
 63     protected void onCreate(Bundle savedInstanceState) {
 64         super.onCreate(savedInstanceState);
 65         setContentView(R.layout.dialog_myinfo_changeaddress);
 66 
 67         wvProvince = (WheelView) findViewById(R.id.wv_address_province);
 68         wvCitys = (WheelView) findViewById(R.id.wv_address_city);
 69         lyChangeAddress = findViewById(R.id.ly_myinfo_changeaddress);
 70         lyChangeAddressChild = findViewById(R.id.ly_myinfo_changeaddress_child);
 71         btnSure = (TextView) findViewById(R.id.btn_myinfo_sure);
 72         btnCancel = (TextView) findViewById(R.id.btn_myinfo_cancel);
 73 
 74         lyChangeAddress.setOnClickListener(this);
 75         lyChangeAddressChild.setOnClickListener(this);
 76         btnSure.setOnClickListener(this);
 77         btnCancel.setOnClickListener(this);
 78 
 79         initJsonData();
 80         initDatas();
 81         initProvinces();
 82         provinceAdapter = new AddressTextAdapter(context, arrProvinces, getProvinceItem(strProvince), maxsize, minsize);
 83         wvProvince.setVisibleItems(5);
 84         wvProvince.setViewAdapter(provinceAdapter);
 85         wvProvince.setCurrentItem(getProvinceItem(strProvince));
 86 
 87         initCitys(mCitisDatasMap.get(strProvince));
 88         cityAdapter = new AddressTextAdapter(context, arrCitys, getCityItem(strCity), maxsize, minsize);
 89         wvCitys.setVisibleItems(5);
 90         wvCitys.setViewAdapter(cityAdapter);
 91         wvCitys.setCurrentItem(getCityItem(strCity));
 92 
 93         wvProvince.addChangingListener(new OnWheelChangedListener() {
 94 
 95             @Override
 96             public void onChanged(WheelView wheel, int oldValue, int newValue) {
 97                 // TODO Auto-generated method stub
 98                 String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
 99                 strProvince = currentText;
100                 setTextviewSize(currentText, provinceAdapter);
101                 String[] citys = mCitisDatasMap.get(currentText);
102                 initCitys(citys);
103                 cityAdapter = new AddressTextAdapter(context, arrCitys, 0, maxsize, minsize);
104                 wvCitys.setVisibleItems(5);
105                 wvCitys.setViewAdapter(cityAdapter);
106                 wvCitys.setCurrentItem(0);
107             }
108         });
109 
110         wvProvince.addScrollingListener(new OnWheelScrollListener() {
111 
112             @Override
113             public void onScrollingStarted(WheelView wheel) {
114                 // TODO Auto-generated method stub
115 
116             }
117 
118             @Override
119             public void onScrollingFinished(WheelView wheel) {
120                 // TODO Auto-generated method stub
121                 String currentText = (String) provinceAdapter.getItemText(wheel.getCurrentItem());
122                 setTextviewSize(currentText, provinceAdapter);
123             }
124         });
125 
126         wvCitys.addChangingListener(new OnWheelChangedListener() {
127 
128             @Override
129             public void onChanged(WheelView wheel, int oldValue, int newValue) {
130                 // TODO Auto-generated method stub
131                 String currentText = (String) cityAdapter.getItemText(wheel.getCurrentItem());
132                 strCity = currentText;
133                 setTextviewSize(currentText, cityAdapter);
134             }
135         });
136 
137         wvCitys.addScrollingListener(new OnWheelScrollListener() {
138 
139             @Override
140             public void onScrollingStarted(WheelView wheel) {
141                 // TODO Auto-generated method stub
142 
143             }
144 
145             @Override
146             public void onScrollingFinished(WheelView wheel) {
147                 // TODO Auto-generated method stub
148                 String currentText = (String) cityAdapter.getItemText(wheel.getCurrentItem());
149                 setTextviewSize(currentText, cityAdapter);
150             }
151         });
152     }
153 
154     private class AddressTextAdapter extends AbstractWheelTextAdapter {
155         ArrayList<String> list;
156 
157         protected AddressTextAdapter(Context context, ArrayList<String> list, int currentItem, int maxsize, int minsize) {
158             super(context, R.layout.item_birth_year, NO_RESOURCE, currentItem, maxsize, minsize);
159             this.list = list;
160             setItemTextResource(R.id.tempValue);
161         }
162 
163         @Override
164         public View getItem(int index, View cachedView, ViewGroup parent) {
165             View view = super.getItem(index, cachedView, parent);
166             return view;
167         }
168 
169         @Override
170         public int getItemsCount() {
171             return list.size();
172         }
173 
174         @Override
175         protected CharSequence getItemText(int index) {
176             return list.get(index) + "";
177         }
178     }
179 
180     /**
181      * 设置字体大小
182      * 
183      * @param curriteItemText
184      * @param adapter
185      */
186     public void setTextviewSize(String curriteItemText, AddressTextAdapter adapter) {
187         ArrayList<View> arrayList = adapter.getTestViews();
188         int size = arrayList.size();
189         String currentText;
190         for (int i = 0; i < size; i++) {
191             TextView textvew = (TextView) arrayList.get(i);
192             currentText = textvew.getText().toString();
193             if (curriteItemText.equals(currentText)) {
194                 textvew.setTextSize(24);
195             } else {
196                 textvew.setTextSize(14);
197             }
198         }
199     }
200 
201     public void setAddresskListener(OnAddressCListener onAddressCListener) {
202         this.onAddressCListener = onAddressCListener;
203     }
204 
205     @Override
206     public void onClick(View v) {
207         // TODO Auto-generated method stub
208         if (v == btnSure) {
209             if (onAddressCListener != null) {
210                 onAddressCListener.onClick(strProvince, strCity);
211             }
212         } else if (v == btnCancel) {
213 
214         } else if (v == lyChangeAddressChild) {
215             return;
216         } else {
217             dismiss();
218         }
219         dismiss();
220     }
221 
222     /**
223      * 回调接口
224      * 
225      * @author Administrator
226      *
227      */
228     public interface OnAddressCListener {
229         public void onClick(String province, String city);
230     }
231 
232     /**
233      * 从文件中读取地址数据
234      */
235     private void initJsonData() {
236         try {
237             StringBuffer sb = new StringBuffer();
238             //InputStream is = context.getAssets().open("city.json");
239             InputStream is = context.getClass().getClassLoader().getResourceAsStream("assets/" + "city.json");
240             int len = -1;
241             byte[] buf = new byte[1024];
242             while ((len = is.read(buf)) != -1) {
243                 sb.append(new String(buf, 0, len, "gbk"));
244             }
245             is.close();
246             mJsonObj = new JSONObject(sb.toString());
247         } catch (IOException e) {
248             e.printStackTrace();
249         } catch (JSONException e) {
250             e.printStackTrace();
251         }
252     }
253 
254     /**
255      * 解析数据
256      */
257     private void initDatas() {
258         try {
259             JSONArray jsonArray = mJsonObj.getJSONArray("citylist");
260             mProvinceDatas = new String[jsonArray.length()];
261             for (int i = 0; i < jsonArray.length(); i++) {
262                 JSONObject jsonP = jsonArray.getJSONObject(i);
263                 String province = jsonP.getString("p");
264 
265                 mProvinceDatas[i] = province;
266 
267                 JSONArray jsonCs = null;
268                 try {
269                     /**
270                      * Throws JSONException if the mapping doesn‘t exist or is
271                      * not a JSONArray.
272                      */
273                     jsonCs = jsonP.getJSONArray("c");
274                 } catch (Exception e1) {
275                     continue;
276                 }
277                 String[] mCitiesDatas = new String[jsonCs.length()];
278                 for (int j = 0; j < jsonCs.length(); j++) {
279                     JSONObject jsonCity = jsonCs.getJSONObject(j);
280                     String city = jsonCity.getString("n");
281                     mCitiesDatas[j] = city;
282                     JSONArray jsonAreas = null;
283                     try {
284                         /**
285                          * Throws JSONException if the mapping doesn‘t exist or
286                          * is not a JSONArray.
287                          */
288                         jsonAreas = jsonCity.getJSONArray("a");
289                     } catch (Exception e) {
290                         continue;
291                     }
292 
293                     String[] mAreasDatas = new String[jsonAreas.length()];
294                     for (int k = 0; k < jsonAreas.length(); k++) {
295                         String area = jsonAreas.getJSONObject(k).getString("s");
296                         mAreasDatas[k] = area;
297                     }
298                 }
299                 mCitisDatasMap.put(province, mCitiesDatas);
300             }
301 
302         } catch (JSONException e) {
303             e.printStackTrace();
304         }
305         mJsonObj = null;
306     }
307 
308     /**
309      * 初始化省会
310      */
311     public void initProvinces() {
312         int length = mProvinceDatas.length;
313         for (int i = 0; i < length; i++) {
314             arrProvinces.add(mProvinceDatas[i]);
315         }
316     }
317 
318     /**
319      * 根据省会,生成该省会的所有城市
320      * 
321      * @param citys
322      */
323     public void initCitys(String[] citys) {
324         if (citys != null) {
325             arrCitys.clear();
326             int length = citys.length;
327             for (int i = 0; i < length; i++) {
328                 arrCitys.add(citys[i]);
329             }
330         } else {
331             String[] city = mCitisDatasMap.get("湖南");
332             arrCitys.clear();
333             int length = city.length;
334             for (int i = 0; i < length; i++) {
335                 arrCitys.add(city[i]);
336             }
337         }
338         if (arrCitys != null && arrCitys.size() > 0
339                 && !arrCitys.contains(strCity)) {
340             strCity = arrCitys.get(0);
341         }
342     }
343 
344     /**
345      * 初始化地点
346      * 
347      * @param province
348      * @param city
349      */
350     public void setAddress(String province, String city) {
351         if (province != null && province.length() > 0) {
352             this.strProvince = province;
353         }
354         if (city != null && city.length() > 0) {
355             this.strCity = city;
356         }
357     }
358 
359     /**
360      * 返回省会索引,没有就返回默认“湖南”
361      * 
362      * @param province
363      * @return
364      */
365     public int getProvinceItem(String province) {
366         int size = arrProvinces.size();
367         int provinceIndex = 0;
368         boolean noprovince = true;
369         for (int i = 0; i < size; i++) {
370             if (province.equals(arrProvinces.get(i))) {
371                 noprovince = false;
372                 return provinceIndex;
373             } else {
374                 provinceIndex++;
375             }
376         }
377         if (noprovince) {
378             strProvince = "湖南";
379             return 17;
380         }
381         return provinceIndex;
382     }
383 
384     /**
385      * 得到城市索引,没有返回默认“长沙”
386      * 
387      * @param city
388      * @return
389      */
390     public int getCityItem(String city) {
391         int size = arrCitys.size();
392         int cityIndex = 0;
393         boolean nocity = true;
394         for (int i = 0; i < size; i++) {
395             System.out.println(arrCitys.get(i));
396             if (city.equals(arrCitys.get(i))) {
397                 nocity = false;
398                 return cityIndex;
399             } else {
400                 cityIndex++;
401             }
402         }
403         if (nocity) {
404             strCity = "长沙";
405             return 0;
406         }
407         return cityIndex;
408     }
409 
410 }

运行效果:

技术分享  技术分享  技术分享    技术分享  技术分享

7.Android开源项目WheelView的时间和地址联动选择对话框

标签:method   content   src   try   demo   smis   author   return   apk   

原文地址:http://www.cnblogs.com/benchao/p/6121477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!