最近的项目里要用到时间选择器,找了许久看到了比较成熟稳定的Bigkoo/Android-PickerView,拿来用了一下:
TimePickerView pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {//选择的
Log.i(TAG, "pvTime onTimeSelect:"+date.getTime());
}
}).setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
@Override
public void onTimeSelectChanged(Date date) {
Log.i(TAG, "pvTime onTimeSelectChanged:"+date.getTime());
}
})
.setTitleText(getString(R.string.device_dialog_title_time))//标题文字
.setSubmitColor(getResources().getColor(R.color.colorPrimary))//确定按钮文字颜色
.setCancelColor(getResources().getColor(R.color.color_gray))//取消按钮文字颜色
.setContentTextSize(18)//滚轮文字大小
.setType(new boolean[]{false, false, false, true, true, false})
.isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。
.build();
Dialog dialogTimePicker = pvTime.getDialog();
dialogTimePicker.getWindow().setGravity(Gravity.BOTTOM);//设置底部弹出
dialogTimePicker.getWindow().setWindowAnimations(R.style.AnimDialogTools);//设置动画
dialogTimePicker.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
没能自定义背景和title背景,加之需求变更了所以最后决定自定义。用weelView自定义显然成本过高,有什么办法能复用呢?
通过查看源码,发现以Builder形式建立TimePickerView控件,而TimePickerView控件中控制时间选择器的关键是WheelTime,因此我们如果直接使用WheelTime该有多好?
经过种种尝试我失败了,一直在wv_day.setAdapter报空指针异常
显然是有问题的,但是很不好解决,而且在布局上并不优化,于是我决定不用他的布局了,自己写一个。
于是我转变思路看起了weelTime的源码,它在内部对6个WeelView进行了控制。于是我复制了一份WeelTimer的代码,并开放了部分修改项,并使用了mPickerOptions减少内部变量的定义。同时修改了原来的输出方式,代码传送门
在布局中引入下面部分
<LinearLayout
android:id="@+id/ll_wheel"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="@dimen/space_15"
android:layout_marginRight="@dimen/space_15"
android:layout_marginBottom="@dimen/space_15"
android:orientation="horizontal">
<com.contrarywind.view.WheelView
android:id="@+id/wv_year"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<com.contrarywind.view.WheelView
android:id="@+id/wv_month"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<com.contrarywind.view.WheelView
android:id="@+id/wv_day"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<com.contrarywind.view.WheelView
android:id="@+id/wv_hour"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<com.contrarywind.view.WheelView
android:id="@+id/wv_minute"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<com.contrarywind.view.WheelView
android:id="@+id/wv_second"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
封装后的调用
WheelView[] views = new WheelView[6];
views[0] = contentView.findViewById(R.id.wv_year);
views[1] = contentView.findViewById(R.id.wv_month);
views[2] = contentView.findViewById(R.id.wv_day);
views[3] = contentView.findViewById(R.id.wv_hour);
views[4] = contentView.findViewById(R.id.wv_minute);
views[5] = contentView.findViewById(R.id.wv_second);
weelViewHelper = new WeelViewHelper(getActivity(), views); weelViewHelper.setTextColorCenter(getActivity().getResources().getColor(R.color.colorPrimary));
weelViewHelper.setContentTextSize(17);
weelViewHelper.setCyclic(true);
weelViewHelper.setDateFormat(dateFormat);
weelViewHelper.setType(new boolean[]{true, true, true, true, true, true});//默认全显示
weelViewHelper.setLabels("年","月","日","时","分","秒");
weelViewHelper.setListener(new WeelViewHelper.OnDataChangeListener() {
@Override
public void onDataChange(String data) {
if(isStart){
startDate.setText(data);
}else{
endDate.setText(data);
}
}
});
//所有变更项目需要在Show之前变更完毕,否则不生效
weelViewHelper.Show();
基于implementation ‘com.contrarywind:Android-PickerView:4.1.6’