1. overview
Remember to do the mall project before, need to achieve three-level linkage in the address selection, convenient for users to fill in the address quickly, then used a so-called. android-wheel The open source control felt very useful at that time. The only trouble was that it needed to organize and parse the xml files of the provinces and cities by itself. The idea was simple, but the amount of code was relatively large. Another open source component, citypickerview, was discovered by accident.
github address: crazyandcoder/citypicker
2. Achieving results
The following is a demonstration of the implementation effect:
3. Implementation method
dependencies { ... compile 'liji.library.dev:citypickerview:0.7.0' }
Instead of introducing the layout file, we will directly demonstrate the implementation code:
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 address_manager_titleBar; EditText new_address_name,new_address_phone,new_address_address; Anchong_Address newAddress = new Anchong_Address(); TextView new_address_area; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_new_address); initView(); } //Texview Click events public void chooseArea(View view) { //Judging Hidden State of Input Method InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive()) { imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); selectAddress();//call CityPicker Select region } } private void selectAddress() { CityPicker cityPicker = new CityPicker.Builder(NewAddressActivity.this) .textSize(14) .title("Address selection") .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(); //Monitoring method to get the result of selection cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() { @Override public void onSelected(String... citySelected) { //Province String province = citySelected[0]; //City String city = citySelected[1]; //District and County (if two levels of action are set, then the item returns empty) String district = citySelected[2]; //Zip code String code = citySelected[3]; //by TextView assignment 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 address: crazyandcoder/citypicker
Return result
The information of the selected provincial and urban areas can be obtained only by passing in Context, and the results can be returned to four items, which can be selected according to their actual needs.
- citySelected[0]: Provincial Information
- City Selected [1]: Represents: City Information
- citySelected[2]: Indicates: District and County Information
- citySelected[3]: Represents: zip code information
Method description
- textSize (Roller text size, int type, default 18)
- Title (selector title, default to Select Area)
- backgroundPop (background, default translucent, 16-bit color code with alpha value, such as 0xa0ffffff)
- titleBackgroundColor (title bar background, default gray, #C7C7C7)
- confirTextColor (confirm button font color, default to system color Primary color value)
- cancelTextColor (cancel button font color, default to system color Primary color value)
- Province (default display province, item location directly located after display selector)
- city (default display market, item location directly located after display selector)
- district (default display area, item location directly located after the selector is displayed)
- textColor (Roller text color, int type, default 0xFF5858)
- Provce Cyclic
- City Cyclic
- Distct Cyclic
- visibleItemsCount (Number of item s displayed by the wheel, int type, default 5)
- itemPadding
- OnlyShow Province AndCity (boolean flag). (Does it show only two levels of action between provinces and municipalities, removing districts or counties)
- TitleTextColor (header text color, default: # E9E9E9)