本篇文章讲述的是android中时间,地址滚轮选择器的使用,文章参考自
android-pickers
本文也是基于对这个库中日期选择器的使用给一个简单的介绍。
讲述的内容包括:
- 库依赖
- 混淆注意
- 代码中运用
ok,下面做一个简单的讲解。
库依赖
首先在 项目的 project 的gradle中添加 meaven 依赖,注意不是 app的 gradle,而是 project 的
allprojects {
repositories {
maven { url "https://jitpack.io" }//必须添加这行
}
}
然后在app的gradle中添加对jar包的依赖,是app的gradle,而不是 project的gradle哦
dependencies {
compile 'com.github.addappcn:android-pickers:1.0.1'
}
混淆
由于地址选择器使用了fastjson来解析,混淆时候需要加入以下类似的规则,不混淆Province、City等实体类。
# 时间,地址选择器
-keepattributes InnerClasses,Signature
-keepattributes *Annotation*
-keep class cn.addapp.pickers.entity.** { *;}
代码中运用
在Mainactivity中的调用,我设了一个简单的点击事件,在onClick()事件中调用以下方法:
public void onYearMonthDayPicker(View view) {
final DatePicker picker = new DatePicker(this);
picker.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
picker.setWidth((int) (picker.getScreenWidthPixels() * 0.6));
picker.setCanLoop(false);
picker.setWheelModeEnable(true);
picker.setTopPadding(15);
picker.setRangeStart(2016, 8, 29);
picker.setRangeEnd(2111, 1, 11);
picker.setSelectedItem(2050, 10, 14);
picker.setWeightEnable(true);
picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
@Override
public void onDatePicked(String year, String month, String day) {
showShortToast(year + "-" + month + "-" + day);
}
});
picker.setOnWheelListener(new DatePicker.OnWheelListener() {
@Override
public void onYearWheeled(int index, String year) {
picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
}
@Override
public void onMonthWheeled(int index, String month) {
picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
}
@Override
public void onDayWheeled(int index, String day) {
picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
}
});
picker.show();
}
需要注意的是,这个方法是让picker显示在名目中央的,效果图如下:
1.gifpicker.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
picker.setWidth((int) (picker.getScreenWidthPixels() * 0.6));
是为了控制picker的位置和显示宽度。
若不加这两句代码,即代码写的如下的话:
public void onYearMonthDayPicker(View view) {
final DatePicker picker = new DatePicker(this);
picker.setCanLoop(false);
picker.setWheelModeEnable(true);
picker.setTopPadding(15);
picker.setRangeStart(2016, 8, 29);
picker.setRangeEnd(2111, 1, 11);
picker.setSelectedItem(2050, 10, 14);
picker.setWeightEnable(true);
picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
@Override
public void onDatePicked(String year, String month, String day) {
showShortToast(year + "-" + month + "-" + day);
}
});
picker.setOnWheelListener(new DatePicker.OnWheelListener() {
@Override
public void onYearWheeled(int index, String year) {
picker.setTitleText(year + "-" + picker.getSelectedMonth() + "-" + picker.getSelectedDay());
}
@Override
public void onMonthWheeled(int index, String month) {
picker.setTitleText(picker.getSelectedYear() + "-" + month + "-" + picker.getSelectedDay());
}
@Override
public void onDayWheeled(int index, String day) {
picker.setTitleText(picker.getSelectedYear() + "-" + picker.getSelectedMonth() + "-" + day);
}
});
picker.show();
}
效果图是如下的:
2.gifok,关于这个库的话,还有很多其它效果,大家有兴趣的话可以去看我以上提供的原库地址,谢谢诶。
网友评论