Android使用android-wheel實現(xiàn)省市縣三級聯(lián)動
今天沒事跟群里面侃大山,有個哥們說道Android Wheel這個控件,以為是Andriod內(nèi)置的控件,google一把,發(fā)現(xiàn)是個github上的一個控件。
下載地址:https://code.google.com/p/android-wheel/ 發(fā)現(xiàn)很適合做省市縣三級聯(lián)動就做了一個。
先看下效果圖:
1、首先導(dǎo)入github上的wheel項目
2、新建個項目,然后選擇記得右鍵->Properties->Android中將wheel添加為lib:
上面兩個步驟是導(dǎo)入所有開源項目的過程了。
3、下面開始代碼的編寫:首先是省市區(qū)的json文件,放置在asserts的city.json中:
大概的格式先了解一下,一會代碼會根據(jù)這樣的格式解析
{"citylist": [{"p":"河北", "c":[{"n":"石家莊", "a":[{"s":"長安區(qū)"},{"s":"橋東區(qū)"},{"s":"鹿泉市"}] }] }
4、布局文件,比較簡單就3個WheelView分別代表省,市,縣,還有一個按鈕:
<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="請選擇城市" 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的編寫:注釋相當詳細,節(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[]>(); /** * 當前省的名稱 */ private String mCurrentProviceName; /** * 當前市的名稱 */ private String mCurrentCityName; /** * 當前區(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ù)當前的市,更新區(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ù)當前的省,更新市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(); } /** * 解析整個Json對象,完成后釋放Json對象的內(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);// 每個省的json對象 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()];// 當前市的所有區(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對象 */ 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
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android PickerView實現(xiàn)三級聯(lián)動效果
- Android實現(xiàn)省市區(qū)三級聯(lián)動
- 最好用的Android省市區(qū)三級聯(lián)動選擇效果
- Android日期選擇器實現(xiàn)年月日三級聯(lián)動
- Android中使用開源框架Citypickerview實現(xiàn)省市區(qū)三級聯(lián)動選擇
- Android自定義WheelView地區(qū)選擇三級聯(lián)動
- Android省市區(qū)三級聯(lián)動控件使用方法實例講解
- android-wheel控件實現(xiàn)三級聯(lián)動效果
- Android實現(xiàn)三級聯(lián)動下拉框 下拉列表spinner的實例代碼
- Android實現(xiàn)城市選擇三級聯(lián)動
相關(guān)文章
詳細解讀Android系統(tǒng)中的application標簽
這篇文章主要介紹了Android系統(tǒng)中的application標簽,以application來聲明App是Android入門學習中的基礎(chǔ)知識,需要的朋友可以參考下2016-04-04Android ListView與ScrollView沖突的解決方法總結(jié)
這篇文章主要介紹了Android ListView與ScrollView沖突的解決方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04Android判斷用戶2G/3G/4G移動數(shù)據(jù)網(wǎng)絡(luò)
這篇文章主要介紹了Android判斷用戶2G/3G/4G移動數(shù)據(jù)網(wǎng)絡(luò)的方法,感興趣的小伙伴們可以參考一下2015-12-12Android抽屜導(dǎo)航Navigation Drawer實例解析
這篇文章主要為大家詳細介紹了Android抽屜導(dǎo)航NavigationDrawer實例,感興趣的小伙伴們可以參考一下2016-05-05Android TimerTask 的簡單應(yīng)用及注意事項
這篇文章主要介紹了Android TimerTask 的簡單應(yīng)用及注意事項的相關(guān)資料,需要的朋友可以參考下2017-06-06Android 配置gradle實現(xiàn)VersionCode自增實例
今天小編就為大家分享一篇Android 配置gradle實現(xiàn)VersionCode自增實例,具有很好的 參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03