There's nothing to talk about today with the group, a friend said that the Android wheel control, thought to be a built-in control, Google a andriod, found to be a github on a control.
Download Address:https://code.google.com/p/android-wheel/ found very suitable to do the provincial and municipal three-level linkage made one.
First look at the effect of the picture:
1, first import github on the wheel project
2, create a new item, and then choose to remember right key->properties->android add wheel as Lib:
The two steps above are the process of importing all open source projects.
3, the following start code writing: First, the provincial city of JSON files, placed in the asserts City.json:
The approximate format to understand first, a code will be based on such a format parsing
{"CityList":
[{"P": "Hebei", "
C": [{"n": "Shijiazhuang",
"a": [{"S": "Changan District"},{"s": "Qiaodong District"},{"s": "The Luquan"}]}
]
}
4, the layout file, relatively simple 3 wheelview respectively on behalf of the province, city, county, and a button:
<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" and roid:layout_height= "wrap_content" android:layout_gravity= "center" android:layout_margin= "10DP" android:text= "please Select City "android:textcolor=" #ffffff "android:textsize=" 20sp "/> <linearlayout android:layout_width=" fill _parent "android:layout_height=" wrap_content "android:background=" @drawable/layout_bg "android:orientation=" ho
Rizontal "> <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= "OK"/>
</LinearLayout>
5, the activity of the preparation: note Quite detailed, the section does not repeat.
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 {/** *
The information of the provinces and cities in the country is saved in JSON format, and after parsing is done, the value is null/private Jsonobject mjsonobj;
/** * Province Wheelview Control/private Wheelview mprovince;
/** * City of Wheelview control/private Wheelview mcity;
/** * Area of the Wheelview control/private Wheelview Marea;
/** * All Provinces * * Private string[] mprovincedatas; /** * Key-province value-City s */private map<string, string[]> mcitisdatasmap = new hashmap<string, string[]> ();
/** * Key-city Values-District S * * * private map<string, string[]> mareadatasmap = new hashmap<string, string[]> ();
/** * Current province name */private String mcurrentprovicename;
/** * The name of the current city/private String mcurrentcityname;
/** * The name of the current zone */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 (This, Mprovincedatas) (new arraywheeladapter<string>);
Add Change Event Mprovince.addchanginglistener (this);
Add Change Event Mcity.addchanginglistener (this);
Add Change Event Marea.addchanginglistener (this);
Mprovince.setvisibleitems (5);
Mcity.setvisibleitems (5);
Marea.setvisibleitems (5);
Updatecities ();
Updateareas (); }/** * Based on currentCity, the new zone wheelview information * * 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 (this, areas) (new arraywheeladapter<string>);
Marea.setcurrentitem (0);
/** * According to the current province, update the information of the city Wheelview * * private void Updatecities () {int pcurrent = Mprovince.getcurrentitem ();
Mcurrentprovicename = Mprovincedatas[pcurrent];
string[] Cities = mcitisdatasmap.get (Mcurrentprovicename);
if (cities = = null) {cities = new string[] {"};
} mcity.setviewadapter (this, cities) (new arraywheeladapter<string>);
Mcity.setcurrentitem (0);
Updateareas (); /** * Parses the entire JSON object, releasing the memory of the JSON object after completion/private void Initdatas () {try {Jsonarray Jsonarray = Mjsonobj.getjsonarra
Y ("CityList");
Mprovincedatas = new String[jsonarray.length ()]; for (int i = 0; i < Jsonarray.Length ();
i++) {Jsonobject JsonP = Jsonarray.getjsonobject (i);//JSON object String province = jsonp.getstring ("P") for each province;/province name
Mprovincedatas[i] = province;
Jsonarray Jsoncs = null;
try {/** * Throws jsonexception if the mapping doesn ' t exist or is *. 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");//the name mcitiesdatas[j] = Urban;
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 ()];//all extents for the current city for (int k = 0; k < jsonareas.length (); k++) {String area = Jsonareas.getjsOnobject (k). getString ("s");//region name mareasdatas[k] = area;
Mareadatasmap.put (city, Mareasdatas);
} mcitisdatasmap.put (province, Mcitiesdatas);
} catch (Jsonexception e) {e.printstacktrace ();
} mjsonobj = null; /** * Reads the JSON file in the province from the Assert folder and converts it to a JSON object/private void Initjsondata () {try {stringbuffer sb = new STRINGB
Uffer ();
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 (); The processing of/** * Change event/@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 (this, mcurrentprovicename + Mcurrentcityname + mcurrentareanam, view) {Toast.maketext
E, 1). Show ();
}
}
SOURCE Download: Http://xiazai.jb51.net/201608/yuanma/Androidwheel (jb51.net). rar
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.