Android使用android-wheel實(shí)現(xiàn)省市縣三級(jí)聯(lián)動(dòng)
今天沒事跟群里面侃大山,有個(gè)哥們說道Android Wheel這個(gè)控件,以為是Andriod內(nèi)置的控件,google一把,發(fā)現(xiàn)是個(gè)github上的一個(gè)控件。
下載地址:https://code.google.com/p/android-wheel/ 發(fā)現(xiàn)很適合做省市縣三級(jí)聯(lián)動(dòng)就做了一個(gè)。
先看下效果圖:
1、首先導(dǎo)入github上的wheel項(xiàng)目
2、新建個(gè)項(xiàng)目,然后選擇記得右鍵->Properties->Android中將wheel添加為lib:
上面兩個(gè)步驟是導(dǎo)入所有開源項(xiàng)目的過程了。
3、下面開始代碼的編寫:首先是省市區(qū)的json文件,放置在asserts的city.json中:
大概的格式先了解一下,一會(huì)代碼會(huì)根據(jù)這樣的格式解析
{"citylist": [{"p":"河北", "c":[{"n":"石家莊", "a":[{"s":"長安區(qū)"},{"s":"橋東區(qū)"},{"s":"鹿泉市"}] }] }
4、布局文件,比較簡(jiǎn)單就3個(gè)WheelView分別代表省,市,縣,還有一個(gè)按鈕:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:text="請(qǐng)選擇城市" android:textColor="#ffffff" android:textSize="20sp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/layout_bg" android:orientation="horizontal" > <kankan.wheel.widget.WheelView android:id="@+id/id_province" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > </kankan.wheel.widget.WheelView> <kankan.wheel.widget.WheelView android:id="@+id/id_city" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > </kankan.wheel.widget.WheelView> <kankan.wheel.widget.WheelView android:id="@+id/id_area" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" > </kankan.wheel.widget.WheelView> </LinearLayout> <Button android:onClick="showChoose" android:layout_gravity="right" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="確定" /> </LinearLayout>
5、Activity的編寫:注釋相當(dāng)詳細(xì),節(jié)不贅述了。
package com.example.wheel_province; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.Map; import kankan.wheel.widget.OnWheelChangedListener; import kankan.wheel.widget.WheelView; import kankan.wheel.widget.adapters.ArrayWheelAdapter; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Toast; /** * * @author zhy * */ public class CitiesActivity extends Activity implements OnWheelChangedListener { /** * 把全國的省市區(qū)的信息以json的格式保存,解析完成后賦值為null */ private JSONObject mJsonObj; /** * 省的WheelView控件 */ private WheelView mProvince; /** * 市的WheelView控件 */ private WheelView mCity; /** * 區(qū)的WheelView控件 */ private WheelView mArea; /** * 所有省 */ private String[] mProvinceDatas; /** * key - 省 value - 市s */ private Map<String, String[]> mCitisDatasMap = new HashMap<String, String[]>(); /** * key - 市 values - 區(qū)s */ private Map<String, String[]> mAreaDatasMap = new HashMap<String, String[]>(); /** * 當(dāng)前省的名稱 */ private String mCurrentProviceName; /** * 當(dāng)前市的名稱 */ private String mCurrentCityName; /** * 當(dāng)前區(qū)的名稱 */ private String mCurrentAreaName =""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.citys); initJsonData(); mProvince = (WheelView) findViewById(R.id.id_province); mCity = (WheelView) findViewById(R.id.id_city); mArea = (WheelView) findViewById(R.id.id_area); initDatas(); mProvince.setViewAdapter(new ArrayWheelAdapter<String>(this, mProvinceDatas)); // 添加change事件 mProvince.addChangingListener(this); // 添加change事件 mCity.addChangingListener(this); // 添加change事件 mArea.addChangingListener(this); mProvince.setVisibleItems(5); mCity.setVisibleItems(5); mArea.setVisibleItems(5); updateCities(); updateAreas(); } /** * 根據(jù)當(dāng)前的市,更新區(qū)WheelView的信息 */ private void updateAreas() { int pCurrent = mCity.getCurrentItem(); mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent]; String[] areas = mAreaDatasMap.get(mCurrentCityName); if (areas == null) { areas = new String[] { "" }; } mArea.setViewAdapter(new ArrayWheelAdapter<String>(this, areas)); mArea.setCurrentItem(0); } /** * 根據(jù)當(dāng)前的省,更新市WheelView的信息 */ private void updateCities() { int pCurrent = mProvince.getCurrentItem(); mCurrentProviceName = mProvinceDatas[pCurrent]; String[] cities = mCitisDatasMap.get(mCurrentProviceName); if (cities == null) { cities = new String[] { "" }; } mCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities)); mCity.setCurrentItem(0); updateAreas(); } /** * 解析整個(gè)Json對(duì)象,完成后釋放Json對(duì)象的內(nèi)存 */ private void initDatas() { try { JSONArray jsonArray = mJsonObj.getJSONArray("citylist"); mProvinceDatas = new String[jsonArray.length()]; for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonP = jsonArray.getJSONObject(i);// 每個(gè)省的json對(duì)象 String province = jsonP.getString("p");// 省名字 mProvinceDatas[i] = province; JSONArray jsonCs = null; try { /** * Throws JSONException if the mapping doesn't exist or is * not a JSONArray. */ jsonCs = jsonP.getJSONArray("c"); } catch (Exception e1) { continue; } String[] mCitiesDatas = new String[jsonCs.length()]; for (int j = 0; j < jsonCs.length(); j++) { JSONObject jsonCity = jsonCs.getJSONObject(j); String city = jsonCity.getString("n");// 市名字 mCitiesDatas[j] = city; JSONArray jsonAreas = null; try { /** * Throws JSONException if the mapping doesn't exist or * is not a JSONArray. */ jsonAreas = jsonCity.getJSONArray("a"); } catch (Exception e) { continue; } String[] mAreasDatas = new String[jsonAreas.length()];// 當(dāng)前市的所有區(qū) for (int k = 0; k < jsonAreas.length(); k++) { String area = jsonAreas.getJSONObject(k).getString("s");// 區(qū)域的名稱 mAreasDatas[k] = area; } mAreaDatasMap.put(city, mAreasDatas); } mCitisDatasMap.put(province, mCitiesDatas); } } catch (JSONException e) { e.printStackTrace(); } mJsonObj = null; } /** * 從assert文件夾中讀取省市區(qū)的json文件,然后轉(zhuǎn)化為json對(duì)象 */ private void initJsonData() { try { StringBuffer sb = new StringBuffer(); InputStream is = getAssets().open("city.json"); int len = -1; byte[] buf = new byte[1024]; while ((len = is.read(buf)) != -1) { sb.append(new String(buf, 0, len, "gbk")); } is.close(); mJsonObj = new JSONObject(sb.toString()); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } /** * change事件的處理 */ @Override public void onChanged(WheelView wheel, int oldValue, int newValue) { if (wheel == mProvince) { updateCities(); } else if (wheel == mCity) { updateAreas(); } else if (wheel == mArea) { mCurrentAreaName = mAreaDatasMap.get(mCurrentCityName)[newValue]; } } public void showChoose(View view) { Toast.makeText(this, mCurrentProviceName + mCurrentCityName + mCurrentAreaName, 1).show(); } }
源碼下載:http://xiazai.jb51.net/201608/yuanma/Androidwheel(jb51.net).rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android PickerView實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)效果
- Android實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)
- 最好用的Android省市區(qū)三級(jí)聯(lián)動(dòng)選擇效果
- Android日期選擇器實(shí)現(xiàn)年月日三級(jí)聯(lián)動(dòng)
- Android中使用開源框架Citypickerview實(shí)現(xiàn)省市區(qū)三級(jí)聯(lián)動(dòng)選擇
- Android自定義WheelView地區(qū)選擇三級(jí)聯(lián)動(dòng)
- Android省市區(qū)三級(jí)聯(lián)動(dòng)控件使用方法實(shí)例講解
- android-wheel控件實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)效果
- Android實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)下拉框 下拉列表spinner的實(shí)例代碼
- Android實(shí)現(xiàn)城市選擇三級(jí)聯(lián)動(dòng)
相關(guān)文章
Android 中倒計(jì)時(shí)驗(yàn)證兩種常用方式實(shí)例詳解
這篇文章主要介紹了Android 中倒計(jì)時(shí)驗(yàn)證兩種常用方式實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android手勢(shì)控制實(shí)現(xiàn)縮放、移動(dòng)圖片
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)控制實(shí)現(xiàn)縮放、移動(dòng)圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02詳細(xì)解讀Android系統(tǒng)中的application標(biāo)簽
這篇文章主要介紹了Android系統(tǒng)中的application標(biāo)簽,以application來聲明App是Android入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2016-04-04Android中微信搶紅包助手的實(shí)現(xiàn)詳解
本篇文章主要介紹了Android中微信搶紅包助手的實(shí)現(xiàn)詳解,通過利用AccessibilityService輔助服務(wù),監(jiān)測(cè)屏幕內(nèi)容,如監(jiān)聽狀態(tài)欄的信息,屏幕跳轉(zhuǎn)等,以此來實(shí)現(xiàn)自動(dòng)拆紅包的功能,有興趣的可以了解一下。2017-02-02Android ListView與ScrollView沖突的解決方法總結(jié)
這篇文章主要介紹了Android ListView與ScrollView沖突的解決方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04Android判斷用戶2G/3G/4G移動(dòng)數(shù)據(jù)網(wǎng)絡(luò)
這篇文章主要介紹了Android判斷用戶2G/3G/4G移動(dòng)數(shù)據(jù)網(wǎng)絡(luò)的方法,感興趣的小伙伴們可以參考一下2015-12-12Android抽屜導(dǎo)航Navigation Drawer實(shí)例解析
這篇文章主要為大家詳細(xì)介紹了Android抽屜導(dǎo)航NavigationDrawer實(shí)例,感興趣的小伙伴們可以參考一下2016-05-05Android TimerTask 的簡(jiǎn)單應(yīng)用及注意事項(xiàng)
這篇文章主要介紹了Android TimerTask 的簡(jiǎn)單應(yīng)用及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2017-06-06Android 配置gradle實(shí)現(xiàn)VersionCode自增實(shí)例
今天小編就為大家分享一篇Android 配置gradle實(shí)現(xiàn)VersionCode自增實(shí)例,具有很好的 參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03