In Android, citypickerview, an open-source framework, is used to achieve three levels of association between provinces, cities, and cities,

Source: Internet
Author: User

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.

  1. CitySelected [0]: province Information
  2. CitySelected [1]: Indicates city information.
  3. CitySelected [2]: indicates the district/county information.
  4. CitySelected [3]: indicates the zip code.
Method description
  1. TextSize (scroll wheel text size, int type, 18 by default)
  2. Title (selector title, "select region" by default ")
  3. BackgroundPop (background, translucent by default, 16-bit color code with alpha value, such as 0xa0ffffff)
  4. TitleBackgroundColor (Title Bar background, gray by default, # C7C7C7)
  5. ConfirTextColor (confirm the font color of the button, which defaults to the colorPrimary color value of the system)
  6. CancelTextColor (cancel button font color, which defaults to the system's colorPrimary color value)
  7. Province (the default province is displayed, and the item position is located directly after the selector is displayed)
  8. City (by default, the city is displayed, and the position of the item directly located after the selector is displayed)
  9. District (default display area, which displays the position of the item directly located after the selector)
  10. TextColor (color of scroll wheel text, int type, 0xFF585858 by default)
  11. ProvinceCyclic)
  12. CityCyclic)
  13. DistrictCyclic)
  14. VisibleItemsCount (number of items displayed on the scroll wheel, int type, 5 by default)
  15. ItemPadding (scroll wheel item spacing, 5dp by default)
  16. OnlyShowProvinceAndCity (boolean flag)
  17. TitleTextColor (Title text color, # E9E9E9 by default)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.