In Android, citypickerview, an open-source framework, is used to achieve three levels of association between provinces, cities, and cities,
1. Overview
I remember that when I was working on a mall project, I needed to implement provincial/municipal linkages in the address selection to facilitate users to quickly enter the address. At that time, I used an open-source control called android-wheel, at that time, I felt very easy to use. The only trouble was to sort out and parse xml files in provinces and cities. The idea was very simple, but the amount of code was relatively large. Another open-source component, citypickerview, was discovered by accident.
Github address: crazyandcoder/citypicker
2. Implementation results
The following shows the implementation result:
3. Implementation Method (1) Add dependency
dependencies { ... compile 'liji.library.dev:citypickerview:0.7.0'}
(2) code implementation
The layout file is not introduced here. The implementation code is directly demonstrated:
Package com. mly. panhouye. anchong. activity; import android. content. context; import android. content. intent; import android. graphics. color; import android. OS. bundle; import android. text. textUtils; import android. view. view; import android. view. inputmethod. inputMethodManager; import android. widget. editText; import android. widget. textView; import com. lljjcoder. citypickerview. widget. cityPicker; import com. mly. panhouye. anchong. r; import com. mly. panhouye. anchong. entity. anchong_Address; import com. mly. panhouye. anchong. view. titleBarView; import static com. mly. panhouye. anchong. utils. constant. NEWADDRESS; public class NewAddressActivity extends BaseActivity {TitleBarView comment; EditText new_address_name, comment, comment; Anchong_Address newAddress = new Anchong_Address (); TextView comment; @ Override protected void onCreate (Bundle comment) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_new_address); initView () ;}// Texview Click Event public void chooseArea (View view) {// determine the hidden state of the input method. InputMethodManager imm = (InputMethodManager) getSystemService (Context. INPUT_METHOD_SERVICE); if (imm. isActive () {imm. hideSoftInputFromWindow (view. getWindowToken (), InputMethodManager. HIDE_NOT_ALWAYS); selectAddress (); // call CityPicker selection region} private void selectAddress () {CityPicker cityPicker = new CityPicker. builder (NewAddressActivity. this ). textSize (14 ). title ("select address "). titleBackgroundColor ("# FFFFFF "). titleTextColor ("#696969 "). confirTextColor ("#696969 "). cancelTextColor ("#696969 "). province ("Jiangsu province "). city ("Changzhou city "). district ("tianning district "). textColor (Color. parseColor ("#000000 ")). provinceCyclic (true ). cityCyclic (false ). districtCyclic (false ). visibleItemsCount (7 ). itemPadding (10 ). onlyShowProvinceAndCity (false ). build (); cityPicker. show (); // The Listener method to obtain the selection result cityPicker. setOnCityItemClickListener (new CityPicker. onCityItemClickListener () {@ Override public void onSelected (String... citySelected) {// province String province = citySelected [0]; // city String city = citySelected [1]; // district/county (if two levels of association are set, the return value is null.) String district = citySelected [2]; // zip code String code = citySelected [3]; // assign new_address_area.setText (province. trim () + "-" + city. trim () + "-" + district. trim () ;}}) ;}@ Override protected void initView () {new_address_name = (EditText) findViewById (R. id. new_address_name); new_address_phone = (EditText) findViewById (R. id. new_address_phone); new_address_address = (EditText) findViewById (R. id. new_address_address); new_address_area = (TextView) findViewById (R. id. new_address_area );}}
4. Instructions for use
From github: crazyandcoder/citypicker
Result returned
You only need to input Context to obtain the information of the selected province, city, and region. Four items are returned, which can be selected based on your actual needs.
- CitySelected [0]: province Information
- CitySelected [1]: Indicates city information.
- CitySelected [2]: indicates the district/county information.
- CitySelected [3]: indicates the zip code.
Method description
- TextSize (scroll wheel text size, int type, 18 by default)
- Title (selector title, "select region" by default ")
- BackgroundPop (background, translucent by default, 16-bit color code with alpha value, such as 0xa0ffffff)
- TitleBackgroundColor (Title Bar background, gray by default, # C7C7C7)
- ConfirTextColor (confirm the font color of the button, which defaults to the colorPrimary color value of the system)
- CancelTextColor (cancel button font color, which defaults to the system's colorPrimary color value)
- Province (the default province is displayed, and the item position is located directly after the selector is displayed)
- City (by default, the city is displayed, and the position of the item directly located after the selector is displayed)
- District (default display area, which displays the position of the item directly located after the selector)
- TextColor (color of scroll wheel text, int type, 0xFF585858 by default)
- ProvinceCyclic)
- CityCyclic)
- DistrictCyclic)
- VisibleItemsCount (number of items displayed on the scroll wheel, int type, 5 by default)
- ItemPadding (scroll wheel item spacing, 5dp by default)
- OnlyShowProvinceAndCity (boolean flag)
- TitleTextColor (Title text color, # E9E9E9 by default)