类似各种app内的城市选择

时间:2021-07-31 09:08:47

类似各种app内的城市选择



github 地址  :http://www.jianshu.com/p/b469c6f02754

依赖 :

compile 'com.zaaach:citypicker:1.1'

這个是效果图,最近在github 上看到的 实现他很简单,添加依赖和权限几行代码就可以实现這个功能


private static final int REQUEST_CODE_PICK_CITY = 233;
private TextView resultTV;


resultTV = (TextView) findViewById(R.id.tv_result);
findViewById(R.id.btn_select).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivityForResult(new Intent(MainActivity.this, CityPickerActivity.class),
                REQUEST_CODE_PICK_CITY);
    }
});


通过onActivityResult  返回选择的那个城市

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_PICK_CITY && resultCode == RESULT_OK){
        if (data != null){
            String city = data.getStringExtra(CityPickerActivity.KEY_PICKED_CITY);
            resultTV.setText("当前选择:" + city);
        }
    }
}


要实现定位效果  需要去开发平台申请相应的key 


<activity  android:name="com.zaaach.citypicker.CityPickerActivity"  android:screenOrientation="portrait"  android:theme="@style/CityPicker.NoActionBar"  android:windowSoftInputMode="stateHidden|adjustPan" />

<meta-data  android:name="com.amap.api.v2.apikey"  android:value="fc827e681a54cc949554a989b55b1e93" />