项目需求中有个功能模块需要用到时间选择控件,但是android系统自带的太丑了,只能自己优化下,结合WheelView实现滚轮选择日期,好像网上也挺多这种文章的。但是适用范围还是不同,希望这个能够对需求相同的朋友有一定帮助。控件标题还有年月日时分秒这些可以自己控制是否显示,先来看效果。
1.有年月日时分的开始时间
2.只有年月日的结束时间
3.用于有时身份证到期的时间选择(分为勾选长期和直接选择时间两种,另外长期后面自己也可以进行扩展)
4.项目结构
5.直接贴代码,代码里面注释很详细
<span style="font-size:18px;"><span style="font-size:14px;">package com.andrew.datechoosewheelviewdemo;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.andrew.datechoosewheelviewdemo.widget.OnWheelChangedListener;
import com.andrew.datechoosewheelviewdemo.widget.OnWheelScrollListener;
import com.andrew.datechoosewheelviewdemo.widget.WheelView;
import com.andrew.datechoosewheelviewdemo.widget.adapters.AbstractWheelTextAdapter;
import java.util.ArrayList;
import java.util.Calendar;
/**
* 使用说明:1.showLongTerm()是否显示长期选项
* 2.setTimePickerGone隐藏时间选择
* 3.接口DateChooseInterface
*
* 用于时间日期的选择
* Created by liuhongxia on 2016/4/16.
*/
public class DateChooseWheelViewDialog extends Dialog implements View.OnClickListener {
//控件
private WheelView mYearWheelView;
private WheelView mDateWheelView;
private WheelView mHourWheelView;
private WheelView mMinuteWheelView;
private CalendarTextAdapter mDateAdapter;
private CalendarTextAdapter mHourAdapter;
private CalendarTextAdapter mMinuteAdapter;
private CalendarTextAdapter mYearAdapter;
private TextView mTitleTextView;
private Button mSureButton;
private Dialog mDialog;
private Button mCloseDialog;
private LinearLayout mLongTermLayout;
private TextView mLongTermTextView;
//变量
private ArrayList<String> arry_date = new ArrayList<String>();
private ArrayList<String> arry_hour = new ArrayList<String>();
private ArrayList<String> arry_minute = new ArrayList<String>();
private ArrayList<String> arry_year = new ArrayList<String>();
private int nowDateId = 0;
private int nowHourId = 0;
private int nowMinuteId = 0;
private int nowYearId = 0;
private String mYearStr;
private String mDateStr;
private String mHourStr;
private String mMinuteStr;
private boolean mBlnBeLongTerm = false;//是否需要长期
private boolean mBlnTimePickerGone = false;//时间选择是否显示
//常量
private final int MAX_TEXT_SIZE = 18;
private final int MIN_TEXT_SIZE = 16;
private Context mContext;
private DateChooseInterface dateChooseInterface;
public DateChooseWheelViewDialog(Context context, DateChooseInterface dateChooseInterface) {
super(context);
this.mContext = context;
this.dateChooseInterface = dateChooseInterface;
mDialog = new Dialog(context,R.style.dialog);
initView();
initData();
}
private void initData() {
initYear();
initDate();
initHour();
initMinute();
initListener();
}
/**
* 初始化滚动监听事件
*/
private void initListener() {
//年份*****************************
mYearWheelView.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
String currentText = (String) mYearAdapter.getItemText(wheel.getCurrentItem());
setTextViewStyle(currentText, mYearAdapter);
mYearStr = arry_year.get(wheel.getCurrentItem()) + "";