1. 程式人生 > >Android省市區三級聯動和地區定位

Android省市區三級聯動和地區定位

廢話不多說,直接上效果圖

這裡寫圖片描述

這裡寫圖片描述

1 匯入依賴庫

compile ‘com.github.open-android:citypicker:0.1.0’

2 地區定位

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //首先跳轉到列表頁面,通過startActivityForResult實現頁面跳轉傳值
Intent intent = new Intent(MainActivity.this, CityListSelectActivity.class); startActivityForResult(intent, CityListSelectActivity.CITY_SELECT_RESULT_FRAG); } //接收選擇器選中的結果: @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super
.onActivityResult(requestCode, resultCode, data); if (requestCode == CityListSelectActivity.CITY_SELECT_RESULT_FRAG) { if (resultCode == RESULT_OK) { if (data == null) { return; } Bundle bundle = data.getExtras(); CityInfoBean cityInfoBean = (CityInfoBean) bundle.getParcelable("cityinfo"
); if (null == cityInfoBean) return; //城市名稱 String cityName = cityInfoBean.getName(); //緯度 String latitude = cityInfoBean.getLongitude(); //經度 String longitude = cityInfoBean.getLatitude(); //獲取到城市名稱,經緯度值後可自行使用... } } } }

3 省市區三級聯動

public class Main2Activity extends AppCompatActivity {
    private Button button;
    private CityPicker cityPicker;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initCityPicker(Main2Activity.this);
        initView();
    }

    private void initView() {
        button = (Button) findViewById(R.id.choose);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cityPicker.show();
            }
        });
    }

    private void initCityPicker(final Context context) {
        cityPicker = new CityPicker.Builder(context)
                .textSize(20)
                .backgroundPop(0xa0000000)
                .title("地址選擇")
                .titleBackgroundColor("#d9ead3")
                .titleTextColor("#000000")
                .confirTextColor("#00ff00")
                .cancelTextColor("#ff0000")
                .province("北京市")
                .city("北京市")
                .district("海淀區")
                .textColor(Color.parseColor("#000000"))
                .provinceCyclic(true)
                .cityCyclic(false)
                .districtCyclic(false)
                .visibleItemsCount(7)
                .itemPadding(10)
                .onlyShowProvinceAndCity(false)
                .build();
        cityPicker.setOnCityItemClickListener(new CityPicker.OnCityItemClickListener() {
            @Override
            public void onSelected(String... citySelected) {
                //省份
                String province = citySelected[0];
                //城市
                String city = citySelected[1];
                //區縣(如果設定了兩級聯動,那麼該項返回空)
                String district = citySelected[2];
                //郵編
                String code = citySelected[3];
            }
            @Override
            public void onCancel() {
                Toast.makeText(context, "已取消", Toast.LENGTH_LONG).show();
            }
        });
    }
}

去年9月份做專案一直在找這種demo,現在找到了很好的依賴庫,希望大家都可以使用哦