Android中省市区三级联动

Android中省市区的三级联动

本文链接:https://blog.csdn.net/qq_34915398/article/details/82558627

效果图

三极联动1.png

1.添加依赖

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

2.Activity 设置

private CityPickerView mPicker = new CityPickerView();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.delivery_add_new);
    mPicker.init(this); //必须!  初始化城市数据     
    ……
}

3.设置点击事件

deliveryAddDistrict.setOnClickListener(this);

@Override
public void onClick(View view) {
    if (view == deliveryAddDistrict){  //地区联动选择
        //添加默认的配置,可以自己修改
        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) {
                    deliveryAddDistrict.setText(province.toString()+"  "+city.toString()+"  "+district.toString());
                }
            }

            @Override
            public void onCancel() {
                ToastUtils.showLongToast(DeliveryAddNewActivity.this, "已取消");
            }
        });
        //显示
        mPicker.showCityPicker( );
    }

你可能感兴趣的:(Android中省市区三级联动)