安卓选择器类库
包括日期选择器、时间选择器、单项选择器、城市选择器、颜色选择器、文件选择器、目录选择器、数字选择器、星座选择器、生肖选择器等。
github地址:https://github.com/gzu-liyujiang/AndroidPicker
建议直接远程加载jcenter里的
“app”是测试用例;“library”包括WheelPicker、ColorPicker、FilePicker,
WheelPicker包括DatePicker、TimePicker、OptionPicker、AddressPicker、NumberPicker等,
如果需要所有的的选择器的话,建议依赖“AndroidPicker”。
latest.release表示使用最新版,
exclude group: 'com.android.support'表示避免重复包引入
dependencies {
compile('cn.qqtheme.framework:AndroidPicker:latest.release') {
exclude group: 'com.android.support'
}
}
窗口进入退出动画(可选,默认动画为淡入淡出)
<resources>
<style name="Animation.CustomPopup" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/popup_in</item>
<item name="android:windowExitAnimation">@anim/popup_out</item>
</style>
</resources>
picker.setAnimationStyle(R.style.Animation_CustomPopup);
Sample
日期选择器:
DatePicker picker = new DatePicker(this);
picker.setRange(1990, 2015);//年份范围
picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
@Override
public void onDatePicked(String year, String month, String day) {
showToast(year + "-" + month + "-" + day);
}
});
picker.show();
时间选择器:
//默认选中当前时间
TimePicker picker = new TimePicker(this);
picker.setTopLineVisible(false);
picker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
@Override
public void onTimePicked(String hour, String minute) {
showToast(hour + ":" + minute);
}
});
picker.show();
单项选择器(可用于性别、学历、职业、星座等选择):
OptionPicker picker = new OptionPicker(this, new String[]{
"第一项", "第二项", "这是一个很长很长很长很长很长很长很长很长很长的很长很长的很长很长的项"
});
picker.setOffset(2);
picker.setSelectedIndex(1);
picker.setTextSize(11);
picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
@Override
public void onOptionPicked(String option) {
showToast(option);
}
});
picker.show();
数字选择器(可用于身高、体重、年龄等选择):
NumberPicker picker = new NumberPicker(this);
picker.setOffset(2);//偏移量
picker.setRange(145, 200);//数字范围
picker.setSelectedItem(172);
picker.setLabel("厘米");
picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
@Override
public void onOptionPicked(String option) {
showToast(option);
}
});
picker.show();
地址选择器(含省级、地级、县级):
city.json放在Assets目录下:
city.json文件地址:https://github.com/gzu-liyujiang/AndroidPicker/tree/master/app/src/main/assets
需要依赖解析json的插件:
compile 'com.alibaba:fastjson:1.1.64.android'
代码:
ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
String json = AssetsUtils.readText(this, "city.json");
data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
AddressPicker picker = new AddressPicker(this, result);
picker.setSelectedItem("贵州", "贵阳", "花溪");
picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
@Override
public void onAddressPicked(String province, String city, String county) {
showToast(province + city + county);
}
});
picker.show();
地址选择器(含地级、县级):
ArrayList<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
String json = AssetsUtils.readText(this, "city2.json");
data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
AddressPicker picker = new AddressPicker(this, data);
picker.setHideProvince(true);
picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
@Override
public void onAddressPicked(String province, String city, String county) {
showToast(province + city + county);
}
});
picker.show();
星座选择器:
ConstellationPicker picker = new ConstellationPicker(this);
picker.setTopBackgroundColor(0xFFEEEEEE);
picker.setTopLineVisible(false);
picker.setCancelTextColor(0xFF33B5E5);
picker.setSubmitTextColor(0xFF33B5E5);
picker.setTextColor(0xFFFF0000, 0xFFCCCCCC);
picker.setLineColor(0xFFEE0000);
picker.setSelectedItem("射手");
picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
@Override
public void onOptionPicked(String option) {
showToast(option);
}
});
picker.show();
生肖选择器:
ChineseZodiacPicker picker = new ChineseZodiacPicker(this);
picker.setLineVisible(false);
picker.setSelectedItem("羊");
picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener() {
@Override
public void onOptionPicked(String option) {
showToast(option);
}
});
picker.show();
获取地址数据并显示地址选择器(带模拟加载进度)
原理:AsyncTask模拟解析数据
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import java.util.ArrayList;
import cn.qqtheme.framework.picker.AddressPicker;
/**
* 获取地址数据并显示地址选择器(带模拟加载进度)
*
* @author 李玉江[QQ:1032694760]
* @version 2015/12/15
*/
public class AddressInitTask extends AsyncTask<String, Void, ArrayList<AddressPicker.Province>> {
private Activity activity;
private ProgressDialog dialog;
private String selectedProvince = "", selectedCity = "", selectedCounty = "";
public AddressInitTask(Activity activity) {
this.activity = activity;
dialog = ProgressDialog.show(activity, null, "正在初始化数据...", true, true);
}
@Override
protected ArrayList<AddressPicker.Province> 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<AddressPicker.Province> data = new ArrayList<AddressPicker.Province>();
try {
String json = AssetsUtils.readText(activity, "city.json");
data.addAll(JSON.parseArray(json, AddressPicker.Province.class));
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(ArrayList<AddressPicker.Province> result) {
dialog.dismiss();
if (result.size() > 0) {
AddressPicker picker = new AddressPicker(activity, result);
picker.setSelectedItem(selectedProvince, selectedCity, selectedCounty);
picker.setOnAddressPickListener(new AddressPicker.OnAddressPickListener() {
@Override
public void onAddressPicked(String province, String city, String county) {
Toast.makeText(activity, province + city + county, Toast.LENGTH_LONG).show();
}
});
picker.show();
} else {
Toast.makeText(activity, "数据初始化失败", Toast.LENGTH_SHORT).show();
}
}
}
使用:
new AddressInitTask(this).execute("湖南", "长沙", "天心");
参考:
。。。