我有两个DatePickerFragment
因为我需要选择一个开始和结束时间。
private void showDatePickerTimePeriodStart() {
final DatePickerFragment date = new DatePickerFragment();
// Sets up the current date in Dialog.
final Calendar calender = Calendar.getInstance();
final Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
// Sets callback to the captured data.
date.setCallBack(ondate);
date.show(getFragmentManager(), "Date Picker");
}
OnDateSetListener ondate = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
final Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, monthOfYear, dayOfMonth);
millisSinceEpochStart = calendar.getTimeInMillis();
}
};
private void showDatePickerTimePeriodEnd() {
final DatePickerFragment date = new DatePickerFragment();
// Sets up the current date in Dialog.
final Calendar calender = Calendar.getInstance();
final Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
/**
* Set Call back to capture selected date
*/
date.setCallBack(ondateSecond);
date.show(getFragmentManager(), "Date Picker");
}
OnDateSetListener ondateSecond = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
final Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, monthOfYear, dayOfMonth);
millisSinceEpochEnd = calendar.getTimeInMillis();
}
};
The Fragment(s):
public class DatePickerFragment extends DialogFragment {
OnDateSetListener ondateSet;
public DatePickerFragment() {}
public void setCallBack(OnDateSetListener ondate) {
ondateSet = ondate;
}
private int year;
private int month;
private int day;
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
year = args.getInt("year");
month = args.getInt("month");
day = args.getInt("day");
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new DatePickerDialog(getActivity(), ondateSet, year, month, day);
}
}
还有一个片段,它是相同的。
所以在我看来,如果我设置第一个Date,我需要做setCallBack
,但我有两个单独的按钮,所以据我所知,我需要另一个方法,它设置另一个回调。我想避免这种重复,因为它不是很干。
我该如何解决这个问题?
答案 0 :(得分:0)
从技术上讲,由于它设置了不同的变量,因此功能不同,重复次数也不多。压缩复制代码的一种方法是将其移动到另一种方法。试试这个:
public long getMillis(int year, int monthOfYear, int dayOfMonth){
final Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, monthOfYear, dayOfMonth);
return calendar.getTimeInMillis();
}
然后你可以简单地让你的回调调用上面的方法。
OnDateSetListener ondate= new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
millisSinceEpochStart = getMillis(year, monthOfYear, dayOfMonth);
}
};
...
OnDateSetListener ondateSecond = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
millisSinceEpochEnd = getMillis(year, monthOfYear, dayOfMonth);
}
};
答案 1 :(得分:0)
如果我理解正确(如果不让我知道,我会删除它),你可以做这样的事情
public class DatePickerFragment extends DialogFragment{
// What you already have
...
// Show the date time picker on click
// (assuming the click listener is already setup)
public void showDateTimePicker(){
final DatePickerFragment date = new DatePickerFragment();
// Sets up the current date in Dialog.
final Calendar calender = Calendar.getInstance();
final Bundle args = new Bundle();
args.putInt("year", calender.get(Calendar.YEAR));
args.putInt("month", calender.get(Calendar.MONTH));
args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
date.setArguments(args);
/**
* Set Call back to capture selected date
*/
date.setCallBack(getCallback());
date.show(getFragmentManager(), "Date Picker");
}
public OnDateSetListener getCallback(){
return new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
final Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(year, monthOfYear, dayOfMonth);
setTime(calendar.getTimeInMillis());
}
};
}
public void setTime(long timeInMillis){
// Override this method and do what you want with the millis
}
}
然后你在DatePickerFragment中所要做的就是覆盖setTime(long timeInMillis)并用值做你想做的事。
例如
public class StartDatePickerFragment extends DatePickerFragment{
@Override
public void setTime(long timeInMillis){
millisSinceEpochStart = timeInMillis;
// Do whatever else you want in the override call
...
}
// Do whatever else you want outside of the method
...
}
如果您不想使用默认侦听器,也可以覆盖getCallback()函数。