当前位置: 首页 > 工具软件 > CityPicker > 使用案例 >

CityPicker的使用

秋向阳
2023-12-01

选择了多款地址选择器,操作过于繁琐,而且找不到方法,使用CityPicker解决,使用方法如下:

1.在AndroidManifest.xml中添加(否则会报错):

tools:replace="android:theme"

2.在Build.gradle(Module app)中添加:

implementation 'liji.library.dev:citypickerview:5.1.0'

3.声明变量:

//城市选择器
private CityPickerView mPicker = new CityPickerView();

4.在onCreate方法中添加:

//请务必进行初始化
mPicker.init(this); 

5.在监听器实现弹出选择器:

//添加默认的配置,可以自己修改
                        CityConfig cityConfig = new CityConfig.Builder()
                                //设置默认显示省份
                                .province("陕西") 
                                .build();
                        mPicker.setConfig(cityConfig);
                        //监听选择点击事件及返回结果
                        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
                            @Override
                            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                                //省份
                                if (province != null && city != null && district != null) {
                                    SystermUtils.showTip(ConsignorIdentifyActivity.this,province.toString()+"  "+city.toString()+"  "+district.toString());
                                }
                            }

                            @Override
                            public void onCancel() {
                                SystermUtils.showTip(ConsignorIdentifyActivity.this, "已取消");
                            }
                        });
                        //显示
                        mPicker.showCityPicker( );

6.参考文献:

https://blog.csdn.net/qq_34915398/article/details/82558627

https://github.com/crazyandcoder/citypicker

 类似资料: