dob.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(), "DatePicker");
}
});
字符串 下面是DateFragment的代码片段
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm+1, dd);
}
public void populateSetDate(int year, int month, int day) {
dob.setText(month+"/"+day+"/"+year);
}
}
//Initialize datePickerDialog in any onClickEvent (Fro example on certain button click) and show it's contents
DatePickerDialog datePickerDialog = createDatePickerDialog(datePickerDialogOnDateSetListener, activity, null);
datePickerDialog.show();
// Method to create Universal DatePickerDialog no matter the SDK (current supports only SDK 19+)
public DatePickerDialog createUniversalDatePickerDialog(DatePickerDialog.OnDateSetListener datePickerDialogOnDateSetListener, Context context, Calendar calendar) {
if (calendar == null) {
calendar = Calendar.getInstance();
}
final DatePickerDialog datePickerDialog;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
datePickerDialog = new DatePickerDialog(context, R.style.DatePickerDialogTheme, datePickerDialogOnDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setCalendarViewShown(false);
datePickerDialog.getDatePicker().setSpinnersShown(true);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
datePickerDialog = new DatePickerDialog(context, R.style.DatePickerDialogLatestTheme, datePickerDialogOnDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setCalendarViewShown(false);
datePickerDialog.getDatePicker().setSpinnersShown(true);
} else {
datePickerDialog = new DatePickerDialog(context, datePickerDialogOnDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
}
return datePickerDialog;
}
// the callback received when the user "chooses" the date in the dialog
public DatePickerDialog.OnDateSetListener datePickerDialogOnDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar selectedDate = Calendar.getInstance();
selectedDate.set(Calendar.YEAR, year);
selectedDate.set(Calendar.MONTH, monthOfYear);
selectedDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");
date.setText(simpleDateFormat.format(selectedDate.getTime()));
}
};
5条答案
按热度按时间u2nhd7ah1#
在按钮中单击调用DateFragment,如下所示
字符串
下面是DateFragment的代码片段
型
7z5jn7bk2#
这是另一个例子:
字符串
dgenwo3n3#
首先,我可以说,,选择的答案是工作fine.it是一个很好的方法.但如果你想使用MaterailDatePicker与片段.我发现了一个解决方案.尝试以下设置DatePicker在片段.
grad
字符串
DatePickerFragment
型
arknldoa4#
字符串
rbpvctlc5#
我推荐一种方法,它也涵盖了SDK的版本。因为应用程序可能会刹车,如果例如SDK 24的构造函数被使用,而应用程序本身在SDK 21上运行。下面的解决方案应该同时适用于片段和活动:
字符串