https://github.com/gzu-liyujiang/AndroidPicker
project:build.gradle
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io"}
}
}
app:build.gradle
compile 'com.alibaba:fastjson:1.1.55.android'
compile('com.github.gzu-liyujiang.AndroidPicker:WheelPicker:1.5.4') {
exclude group: 'com.android.support'
}
// compile('com.github.gzu-liyujiang.AndroidPicker:FilePicker:1.5.4') {
// exclude group: 'com.android.support'
// }
// compile('com.github.gzu-liyujiang.AndroidPicker:ColorPicker:1.5.4') {
// exclude group: 'com.android.support'
// }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lenovo.mypicker.MainActivity">
<TextView
android:id="@+id/tv_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="你选择的地址"
android:textColor="@color/colorPrimary"
android:textSize="25sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onAddressPicker"
android:text="地址选择(包括省级、地级、县级)" />
LinearLayout>
public class MainActivity extends AppCompatActivity {
private TextView tvAddress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvAddress = ((TextView) this.findViewById(R.id.tv_address));
}
public void onAddressPicker(View view) {
AddressPickTask task = new AddressPickTask(this);
// task.setHideProvince(true);//隐藏省,显示市、县
// task.setHideCounty(true);//隐藏县,显示省、市
task.setCallback(new AddressPickTask.Callback() {
@Override
public void onAddressInitFailed() {
ToastUtil.showToast(MainActivity.this, "数据初始化失败");
}
@Override
public void onAddressPicked(Province province, City city, County county) {
String address;
if (county == null) {
address = province.getAreaName() + city.getAreaName();
ToastUtil.showToast(MainActivity.this, province.getAreaName() + city.getAreaName());
} else {
address = province.getAreaName() + city.getAreaName() + county.getAreaName();
ToastUtil.showToast(MainActivity.this, province.getAreaName() + city.getAreaName() + county.getAreaName());
}
tvAddress.setText(address);
}
});
//以下几种格式显示效果相同
// task.execute("广东省", "深圳市", "龙岗区");//设置默认
// task.execute("广东", "深圳", "龙岗");//设置默认
// task.execute("广东省", "深圳", "龙岗");//设置默认
// task.execute("广", "深圳", "龙岗");//设置默认
// task.execute("广", "深", "龙岗");//设置默认
task.execute("广", "深", "龙");//设置默认
}
}
city.json 从作者的GitHub上获取,并下载后放在assets目录下。
/**
* 获取地址数据并显示地址选择器
*
* @author 李玉江[QQ:1032694760]
* @since 2015/12/15
*/
public class AddressPickTask extends AsyncTask<String, Void, ArrayList<Province>> {
private Activity activity;
private ProgressDialog dialog;
private String selectedProvince = "", selectedCity = "", selectedCounty = "";
private boolean hideProvince = false;
private boolean hideCounty = false;
/*-------------------------------构造方法-------------------------------*/
public AddressPickTask(Activity activity) {
this.activity = activity;
}
/*-------------------------------set方法-------------------------------*/
/**
* 隐藏省级行政区,只显示地市级和区县级。
*
* @param hideProvince
*/
public void setHideProvince(boolean hideProvince) {
this.hideProvince = hideProvince;
}
/**
* 隐藏县级行政区,只显示省级和市级。
*
* @param hideCounty
*/
public void setHideCounty(boolean hideCounty) {
this.hideCounty = hideCounty;
}
/*-------------------------------接口回调-------------------------------*/
private Callback callback;
public void setCallback(Callback callback) {
this.callback = callback;
}
public interface Callback extends AddressPicker.OnAddressPickListener {
void onAddressInitFailed();
}
/*-------------------------------AsyncTask方法--------------------------------*/
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(activity, null, "正在初始化数据...", true, true);
}
@Override
protected ArrayList doInBackground(String... params) {
if (params != null) {
switch (params.length) {
case 1:
selectedProvince = params[0];
break;
case 2:
selectedProvince = params[0];
selectedCity = params[1];
break;
case 3:
selectedProvince = params[0];
selectedCity = params[1];
selectedCounty = params[2];
break;
default:
break;
}
}
ArrayList data = new ArrayList<>();
try {
String json = ConvertUtils.toString(activity.getAssets().open("city.json"));
data.addAll(JSON.parseArray(json, Province.class));
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(ArrayList result) {
dialog.dismiss();
if (result.size() > 0) {
AddressPicker picker = new AddressPicker(activity, result);
/** 显示几列 **/
picker.setHideProvince(hideProvince);
picker.setHideCounty(hideCounty);
/** 标题区域 **/
picker.setTopBackgroundColor(activity.getResources().getColor(R.color.brown));//设置标题背景颜色
picker.setTopHeight(35);//设置顶部标题栏高度(单位为dp)
picker.setCancelText("取消");
picker.setCancelTextSize(13);
picker.setCancelTextColor(activity.getResources().getColor(R.color.colorPrimary));//设置顶部标题栏取消按钮文字颜色
picker.setCancelVisible(true);//设置左边按钮是否显示
picker.setTitleText("所在区域");//标题栏
picker.setTitleTextSize(13);//标题栏大小
picker.setTitleTextColor(activity.getResources().getColor(R.color.colorPrimary));//设置所在区域颜色
picker.setSubmitText("完成");//设置标题右边文字
picker.setSubmitTextSize(13);//设置完成按钮大小
picker.setSubmitTextColor(activity.getResources().getColor(R.color.colorPrimary));//设置完成按钮颜色
/** 标题与列表的分割线 **/
picker.setTopLineColor(activity.getResources().getColor(R.color.qing));
picker.setTopLineVisible(true);
picker.setTopLineHeight(10);
/** 列表区域 **/
picker.setTextSize(16);//字体大小
picker.setTextColor(activity.getResources().getColor(R.color.purple));//设置省市县字体滚动颜色
picker.setDividerColor(activity.getResources().getColor(R.color.colorAccent));//设置分割线的颜色
/** 弹框消失的设置 **/
picker.setCancelable(true);//false:必须选择后弹框才消失,true:点击框外可弹框消失
/** 列的比重 **/
if (hideCounty) {
picker.setColumnWeight(0.8f, 1.0f);
} else if (hideProvince) {
picker.setColumnWeight(1.0f, 0.8f);
} else {
picker.setColumnWeight(1.0f, 1.0f, 1.0f);
}
picker.setSelectedItem(selectedProvince, selectedCity, selectedCounty);//设置默认
picker.setOnAddressPickListener(callback);
picker.show();
} else {
callback.onAddressInitFailed();
}
}
}
<resources>
<color name="colorPrimary">#3F51B5color>
<color name="colorPrimaryDark">#303F9Fcolor>
<color name="colorAccent">#FF4081color>
<color name="qing">#60ff40color>
<color name="yellow">#ffe940color>
<color name="purple">#ed0eb6color>
<color name="brown">#d65d17color>
resources>
/**
* 防止多次点击,Toast重复显示,
*/
public class ToastUtil {
private static Toast toast;
public static void showToast(Context context, String content) {
if (toast == null) {
toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
} else {
toast.setText(content);
}
toast.show();
}
}