文件名称:android 日期选择器
文件大小:992KB
文件格式:RAR
更新时间:2018-08-17 06:44:59
android 日期 选择
package com.custom.dtselector.dialog;
import java.util.Calendar;
import com.custom.dtselector.NumericWheelAdapter;
import com.custom.dtselector.OnWheelChangedListener;
import com.custom.dtselector.WheelView;
import com.demo.dtselector.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.Button;
public class DateSelectorDialog extends BaseDialog {
private static final int START_YEAR = 1900;
private static final int END_YEAR = 2100;
private int tdYear = 0;
private int tdMonth = 0;
private int tdDay = 0;
private WheelView wlvYear = null;
private WheelView wlvMonth = null;
private WheelView wlvDay = null;
private NumericWheelAdapter yearAdapter = null;
private NumericWheelAdapter monthAdapter = null;
private NumericWheelAdapter dayAdapter[] = new NumericWheelAdapter[4];
private NumericWheelAdapter curDayAdapter = null;
private int sltYear = 0;
private int sltMonth = 0;
private int sltDay = 0;
private String strSltYear = "";
private String strSltMonth = "";
private String strSltDay = "";
/**回调接口*/
private OnSelectDateEvent resultEvent = 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.date_selector);
wlvYear = (WheelView)dialog.findViewById(R.id.date_selector_year);
wlvMonth = (WheelView)dialog.findViewById(R.id.date_selector_month);
wlvDay = (WheelView)dialog.findViewById(R.id.date_selector_day);
Button btnSelect = (Button)dialog.findViewById(R.id.btn_date_yes);
btnSelect.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
selectDate();
}
});
initCurDate();
initAdapters();
//对话框隐藏后,调用回调事件
dialog.setOnCancelListener(new OnCancelListener(){
@Override
public void onCancel(DialogInterface arg0) {
// TODO Auto-generated method stub
if (resultEvent != null) {
resultEvent.onSelectDateResult(sltYear, sltMonth, sltDay);
}
}
});
}
@Override
public void update(Object obj) {
// TODO Auto-generated method stub
}
/**
* 设置回调事件
* @param event
*/
public void setOnSelectDateEvent(OnSelectDateEvent event) {
this.resultEvent = event;
}
private void initCurDate() {
Calendar instance = Calendar.getInstance();
tdYear = instance.get(Calendar.YEAR);
tdMonth = instance.get(Calendar.MONTH) + 1;
tdDay = instance.get(Calendar.DAY_OF_MONTH);
}
private int itemOfYear(int year) {
return year - START_YEAR;
}
private int itemOfMonth(int month) {
return month - 1;
}
private int itemOfDay(int day) {
return day - 1;
}
/**
* dapeter[0]->[1~28] dapeter[1]->[1~29]
* dapeter[2]->[1~30] dapeter[3]->[1~31]
* @param year
* @param month
* @return
*/
private int indexAdapter(int year,int month) {
int index_data[] = {31,28,31,30,31,30,31,31,30,31,30,31};
if (month == 2) {
return DateTimeUtils.isLeapYear(year) ? 1 : 0;
} else {
if (index_data[month - 1] == 31) {
return 3;
} else {
return 2;
}
}
}
private void initAdapters() {
yearAdapter = new NumericWheelAdapter(START_YEAR,END_YEAR,"%4d");
monthAdapter = new NumericWheelAdapter(1,12,"d");
for (int i = 0; i < dayAdapter.length; i++) {
dayAdapter[i] = new NumericWheelAdapter(1,28 + i,"d");
}
sltYear = tdYear;
sltMonth = tdMonth;
sltDay = tdDay;
//赋值数据
wlvYear.setVisibleItems(3);
wlvYear.setCyclic(true);
wlvYear.setAdapter(yearAdapter);
int itemYear = itemOfYear(sltYear);
wlvYear.setCurrentItem(itemYear);
wlvMonth.setVisibleItems(3);
wlvMonth.setCyclic(true);
wlvMonth.setAdapter(monthAdapter);
int itemMonth = itemOfMonth(sltMonth);
wlvMonth.setCurrentItem(itemMonth);
int adapterIndex = indexAdapter(sltYear,sltMonth);
curDayAdapter = dayAdapter[adapterIndex];
wlvDay.setVisibleItems(3);
wlvDay.setCyclic(true);
wlvDay.setAdapter(curDayAdapter);
int itemDay = itemOfDay(sltDay);
wlvDay.setCurrentItem(itemDay);
wlvYear.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
strSltYear = yearAdapter.getItem(newValue);
android.util.Log.i("",String.format("old=%d new=%d v=%s",
oldValue,newValue,strSltYear));
if (strSltYear != null) {
sltYear = Integer.valueOf(strSltYear);
if (sltMonth == 2) {
int tmpIndex = indexAdapter(sltYear,sltMonth);
NumericWheelAdapter tmpAdapter = dayAdapter[tmpIndex];
if (tmpAdapter != curDayAdapter) {
curDayAdapter = tmpAdapter;
wlvDay.setAdapter(curDayAdapter);
}
}
} else {
sltYear = 0;
}
}
});
wlvMonth.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
strSltMonth = monthAdapter.getItem(newValue);
android.util.Log.i("",String.format("old=%d new=%d v=%s",
oldValue,newValue,strSltMonth));
if (strSltMonth != null) {
sltMonth = Integer.valueOf(strSltMonth);
if (sltYear != 0 && sltMonth != 0) {
int tmpIndex = indexAdapter(sltYear,sltMonth);
NumericWheelAdapter tmpAdapter = dayAdapter[tmpIndex];
if (tmpAdapter != curDayAdapter) {
curDayAdapter = tmpAdapter;
wlvDay.setAdapter(curDayAdapter);
}
}
} else {
sltMonth = 0;
}
}
});
wlvDay.addChangingListener(new OnWheelChangedListener() {
@Override
public void onChanged(WheelView wheel, int oldValue, int newValue) {
// TODO Auto-generated method stub
strSltDay = curDayAdapter.getItem(newValue);
android.util.Log.i("",String.format("old=%d new=%d v=%s",
oldValue,newValue,strSltDay));
if (strSltDay != null) {
sltDay = Integer.valueOf(strSltDay);
} else {
sltDay = 0;
}
}
});
}
private void selectDate() {
hide();
}
/**
* 回调接口
*/
public interface OnSelectDateEvent {
public void onSelectDateResult(int year,int month,int day);
};
}
【文件预览】:
DateTimeSelector
----bin()
--------classes.dex(470KB)
--------res()
--------dexedLibs()
--------classes()
--------DateTimeSelector.apk(193KB)
--------resources.ap_(31KB)
--------AndroidManifest.xml(890B)
----res()
--------drawable-ldpi()
--------values-v11()
--------menu()
--------values-v14()
--------drawable-hdpi()
--------drawable-xhdpi()
--------drawable()
--------values()
--------layout()
----proguard-project.txt(781B)
----ic_launcher-web.png(54KB)
----assets()
----gen()
--------com()
----src()
--------com()
----.project(852B)
----.classpath(364B)
----project.properties(563B)
----AndroidManifest.xml(890B)
----libs()
--------android-support-v4.jar(377KB)