public class MainActivity extends AppCompatActivity {
private WheelView mYearView;
private WheelView mMonthView;
private WheelView mDayView;
private WheelView mHourView;
private WheelView mMinView;
private Button mTimeBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mYearView = (WheelView)findViewById(R.id.year);
mMonthView = (WheelView)findViewById(R.id.month);
mDayView = (WheelView)findViewById(R.id.day);
mHourView = (WheelView)findViewById(R.id.hour);
mMinView = (WheelView)findViewById(R.id.mins);
mTimeBtn = (Button)findViewById(R.id.tv_time);
setDate();
setTime();
mTimeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String time = String.format(Locale.CHINA,"%04d年%02d月%02d日 %02d时%02d分",mYearView.getCurrentItem()+1950,
mMonthView.getCurrentItem()+1,mDayView.getCurrentItem()+1,mHourView.getCurrentItem(),mMinView.getCurrentItem());
Toast.makeText(MainActivity.this, time, Toast.LENGTH_LONG).show();
}
});
}
private void setTime() {
Calendar c = Calendar.getInstance();
int curHour = c.get(Calendar.HOUR_OF_DAY);
int curMin = c.get(Calendar.MINUTE);
initHour();
initMins();
// 设置当前时间
mHourView.setCurrentItem(curHour);
mMinView.setCurrentItem(curMin);
mHourView.setVisibleItems(7);
mMinView.setVisibleItems(7);
}
private void setDate() {
Calendar c = Calendar.getInstance();
int curYear = c.get(Calendar.YEAR);
int curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
int curDate = c.get(Calendar.DATE);
initYear();
initMonth();
initDay(curYear,curMonth);
mYearView.setCurrentItem(curYear - 1950);
mMonthView.setCurrentItem(curMonth - 1);
mDayView.setCurrentItem(curDate - 1);
mYearView.setVisibleItems(7);
mMonthView.setVisibleItems(7);
mDayView.setVisibleItems(7);
}
/**
* 初始化年
*/
private void initYear() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1950, 2050);
numericWheelAdapter.setLabel(" 年");
numericWheelAdapter.setTextSize(25);
mYearView.setViewAdapter(numericWheelAdapter);
mYearView.setCyclic(true);
}
/**
* 初始化月
*/
private void initMonth() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,1, 12, "%02d");
numericWheelAdapter.setLabel(" 月");
numericWheelAdapter.setTextSize(25);
mMonthView.setViewAdapter(numericWheelAdapter);
mMonthView.setCyclic(true);
}
/**
* 初始化天
*/
private void initDay(int arg1, int arg2) {
NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(this,1, getDay(arg1, arg2), "%02d");
numericWheelAdapter.setLabel(" 日");
numericWheelAdapter.setTextSize(25);
mDayView.setViewAdapter(numericWheelAdapter);
mDayView.setCyclic(true);
}
/**
* 初始化时
*/
private void initHour() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,0, 23, "%02d");
numericWheelAdapter.setLabel(" 时");
numericWheelAdapter.setTextSize(25);
mHourView.setViewAdapter(numericWheelAdapter);
mHourView.setCyclic(true);
}
/**
* 初始化分
*/
private void initMins() {
NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this,0, 59, "%02d");
numericWheelAdapter.setLabel(" 分");
numericWheelAdapter.setTextSize(25);
mMinView.setViewAdapter(numericWheelAdapter);
mMinView.setCyclic(true);
}
/**
*
* @param year
* @param month
* @return
*/
private int getDay(int year, int month) {
int day = 30;
boolean flag = false;
switch (year % 4) {
case 0:
flag = true;
break;
default:
flag = false;
break;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
day = 31;
break;
case 2:
day = flag ? 29 : 28;
break;
default:
day = 30;
break;
}
return day;
}
}
资源下载地址 http://download.csdn.net/download/shaoninglipan/10016094
参考 http://www.cnblogs.com/liushilin/p/5749481.html