android 选择器 github,GitHub - jdsjlzx/AndroidPicker: 安卓选择器类库,包括日期时间选择器、单项选择器、地址选择器、颜色选择器、文件目录选择器、数字选择器...

Summary

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4150492d382532422d677265656e2e737667

68747470733a2f2f6170692e62696e747261792e636f6d2f7061636b616765732f677a752d6c6979756a69616e672f6d6176656e2f416e64726f69645069636b65722f696d616765732f646f776e6c6f61642e737667

Jcenter版本列表 版本迭代日志

安卓选择器类库,包括日期选择器、时间选择器、单项选择器、城市选择器、颜色选择器、文件选择器、目录选择器、数字选择器、星座选择器、生肖选择器等。

欢迎大伙儿在issues提交你的意见或建议

项目分支说明:

Branch_OldAndroidPicker-基于android-wheel的旧版选择器,不再维护;

Branch_MultiPart-WheelPicker、ColorPicker及FilePicker是分开的三个模块;

master-默认的最新分支,WheelPicker、ColorPicker及FilePicker合并为一个模块。

Install

“app”是测试用例;“library”包括WheelPicker、ColorPicker、FilePicker,

WheelPicker包括DatePicker、TimePicker、OptionPicker、AddressPicker、NumberPicker等,

如果需要所有的的选择器的话,建议依赖“AndroidPicker”。

dependencies {

compile 'cn.qqtheme.framework:AndroidPicker:latest.integration'

//compile 'cn.qqtheme.framework:WheelPicker:latest.integration'

//compile 'cn.qqtheme.framework:ColorPicker:latest.integration'

//compile 'cn.qqtheme.framework:FilePicker:latest.integration'

}

也可以手动下载本项目,复制“library”命名为“AndroidPicker”,然后:

dependencies {

compile project('AndroidPicker')

}

窗口进入退出动画(可选,默认动画为淡入淡出)

@anim/popup_in

@anim/popup_out

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();

地址选择器(含省级、地级、县级):

ArrayList data = new ArrayList();

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 data = new ArrayList();

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();

颜色选择器:

ColorPicker picker = new ColorPicker(this);

picker.setInitColor(0xFFDD00DD);

picker.setOnColorPickListener(new ColorPicker.OnColorPickListener() {

@Override

public void onColorPicked(int pickedColor) {

showToast(ConvertUtils.toColorString(pickedColor));

}

});

picker.show();

文件选择器(需要权限android.permission.READ_EXTERNAL_STORAGE):

FilePicker picker = new FilePicker(this);

picker.setShowHideDir(false);

picker.setRootPath(StorageUtils.getRootPath(this) + "Download/");

//picker.setAllowExtensions(new String[]{".apk"});

picker.setMode(FilePicker.Mode.File);

picker.setOnFilePickListener(new FilePicker.OnFilePickListener() {

@Override

public void onFilePicked(String currentPath) {

showToast(currentPath);

}

});

picker.show();

目录选择器(需要权限android.permission.READ_EXTERNAL_STORAGE):

FilePicker picker = new FilePicker(this);

picker.setMode(FilePicker.Mode.Directory);

picker.setOnFilePickListener(new FilePicker.OnFilePickListener() {

@Override

public void onFilePicked(String currentPath) {

showToast(currentPath);

}

});

picker.show();

Thanks

Screenshots

d2be2d5540f7725bdcbddea1a294faa2.gif

8064eacd85cbc37b6db0b43407f83968.gif

8f1b0b028cc23e203abb36f3b5dc3314.gif

address.gif

address.png

number.gif

constellation.gif

chinesezodiac.gif

color.gif

file.gif

dir.gif

Contact

687474703a2f2f7770612e71712e636f6d2f70613f703d323a313033323639343736303a3531

320e33dfbcc0a50468f60870bdb25fa0.png

1.省,市,区三级关联 2.地址数据本地化 3.自定义对话框 4.支付从xml文件,或对象文件加载 部分代码: package com.demo.selector; import java.io.InputStream; import com.address.selector.City; import com.address.selector.Country; import com.address.selector.Province; import com.address.selector.Region; import com.demo.address.R; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnCancelListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.AdapterView.OnItemSelectedListener; public class AddressSelectorDialog extends BaseDialog { /**省类型*/ private static final int INDEX_PROVINCE = 1; /**城市类型*/ private static final int INDEX_CITY = 2; /**区县类型*/ private static final int INDEX_REGION = 3; private static Country china = null; private Spinner spnProvince = null; private Spinner spnCity = null; private Spinner spnRegion = null; private ArrayAdapter dptProvince = null; private ArrayAdapter dptCity = null; private ArrayAdapter dptRegion = null; private SpinnerSelectedListener proListener = new SpinnerSelectedListener(); private SpinnerSelectedListener cityListener = new SpinnerSelectedListener(); private SpinnerSelectedListener regionListener = new SpinnerSelectedListener(); //当前选择地区的索引值 private int provinceId = -1; private int cityId = -1; private int regionId = -1; /**选择按钮*/ private Button btnSelect = null; /**回调用事件*/ private OnSelectorCancel selectorCancel = null; @Override public void init(Context context) { // TODO Auto-generated method stub this.context = context; dialog = new Dialog(this.context,R.style.CustomDialog); dialog.setContentView(R.layout.adress_selector); spnProvince = (Spinner)dialog.findViewById(R.id.spin_province); spnProvince.setTag(new Integer(INDEX_PROVINCE)); spnCity = (Spinner)dialog.findViewById(R.id.spin_city); spnCity.setTag(new Integer(INDEX_CITY)); spnRegion = (Spinner)dialog.findViewById(R.id.spin_region); spnRegion.setTag(new Integer(INDEX_REGION)); btnSelect = (Button)dialog.findViewById(R.id.btn_select); btnSelect.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub selectAddress(); } }); //当对话框隐藏后调用[回调事件] dialog.setOnCancelListener(new OnCancelListener(){ @Override public void onCancel(DialogInterface arg0) { // TODO Auto-generated method stub if (selectorCancel != null) { String proName = ""; String cityName = ""; String regName = ""; try { Province tmpPro = china.provinceLst.get(provinceId); proName = tmpPro.areaName; City tmpCity = tmpPro.cityLst.get(cityId); cityName = tmpCity.areaName; Region tmpReg = tmpCity.regionLst.get(regionId); regName = tmpReg.areaName; } catch (Exception e) { android.util.Log.e("selectorCancel???", e.getMessage()); } selectorCancel.onSelectorResult(proName, cityName, regName); } } }); //加载数据 if (china == null) { loadCountryAddressData(); } //加载完后,初始化适配器 initAdapters(); } @Override public void update(Object obj) { // TODO Auto-generated method stub } private void loadCountryAddressData() { try { /** * 反序化的包名,类名,版本ID必须一致,否则ClassNotFoundException */ InputStream stream = this.context.getAssets().open("china-city.obj"); china = Country.loadFromStream(stream); //mHandler.sendEmptyMessage(0); } catch (Exception e) { android.util.Log.e("error", e.getMessage()); } } class SpinnerSelectedListener implements OnItemSelectedListener{ @Override public void onItemSelected (AdapterView parent, View view, int position, long id) { // TODO Auto-generated method stub //android.util.Log.i("", String.format("[pos=%d]",position)); int index = (Integer)parent.getTag(); //ArrayAdapter obj = (ArrayAdapter)(parent.getAdapter()); //String value = obj.getItem(position); //android.util.Log.i("", "value=" + value); if (index == INDEX_PROVINCE) { provinceId = position; if (isValideProvinceIndex(provinceId)) { //改变城市选择 Province tmpProvince = china.provinceLst.get(provinceId); String lstNames[] = tmpProvince.listNames(); if (lstNames == null) { //没有城市,当然没区县 provinceId = -1; spnCity.setVisibility(View.INVISIBLE); regionId = -1; spnRegion.setVisibility(View.INVISIBLE); return ; } else { if (spnCity.getVisibility() == View.INVISIBLE) { spnCity.setVisibility(View.VISIBLE); } if (spnRegion.getVisibility() == View.INVISIBLE) { spnRegion.setVisibility(View.INVISIBLE); } } dptCity = new ArrayAdapter(context, android.R.layout.simple_spinner_item, lstNames); dptCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnCity.setAdapter(dptCity); spnCity.setOnItemSelectedListener(cityListener); } } else if (index == INDEX_CITY) { cityId = position; if (isValideCityIndex(provinceId,cityId)) { //改变区选择 Province tmpProvince = china.provinceLst.get(provinceId); City tmpCity = tmpProvince.cityLst.get(cityId); String lstNames[] = tmpCity.listNames(); if (lstNames == null) { //没有区县 regionId = -1; spnRegion.setVisibility(View.INVISIBLE); return ; } else if (spnRegion.getVisibility() == View.INVISIBLE) { spnRegion.setVisibility(View.VISIBLE); } dptRegion = new ArrayAdapter(context, android.R.layout.simple_spinner_item, lstNames); dptRegion.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnRegion.setAdapter(dptRegion); spnRegion.setOnItemSelectedListener(regionListener); } } else if (index == INDEX_REGION) { regionId = position; } } @Override public void onNothingSelected(AdapterView parent) { // TODO Auto-generated method stub } } private void initAdapters() { dptProvince = new ArrayAdapter(this.context, android.R.layout.simple_spinner_item, china.listNames()); //设置下拉列表的风格 dptProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //设置适配器 spnProvince.setAdapter(dptProvince); //设置侦听事件 spnProvince.setOnItemSelectedListener(proListener); } private boolean isValideProvinceIndex(int index) { return (china != null) && (index >= 0 && index = 0) && (china.provinceLst.get(provinceIndex).cityLst.size() > cityIndex); } return false; } private void selectAddress() { hide(); } /** * 调协回调事件 * @param selectorCancel */ public void setOnSelectorCancel(OnSelectorCancel selectorCancel) { this.selectorCancel = selectorCancel; } /** * 选择器关闭后,调用回调事件接口 */ public interface OnSelectorCancel { /** * @param proName 省份名 * @param cityName 城市名 * @param regName 区县名 */ public void onSelectorResult(String proName,String cityName,String regName); }; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值