尚未找到解决方案,我花了几个小时来应对这个挑战。 我怎样才能得到一个日期,看看当你按下“今天”按钮时效果如何?
想要获取按下“今天”按钮时显示的日期。 并且,我们相信尝试将信息添加到指定日期的屏幕显示的过程。
DatePickerDialog dpdialog = new DatePickerDialog(me, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int pickyear, int monthOfYear,
int dayOfMonth) {
}
}, year, month-1, day);
// Dialog(Positive Button)
dpdialog.setButton(
DialogInterface.BUTTON_POSITIVE,
"set",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Positive Button
// How to get the date set
String day = dpdialog.getDayOfMonth();
String month = dpdialog.getMonth() + 1;
String year = dpdialog.getYear();
}
}
);
// Dialog (Negative Button)
dpdialog.setButton(
DialogInterface.BUTTON_NEGATIVE,
"cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Negative Button
}
}
);
// Dialog(Neutral Button)
dpdialog.setButton(
DialogInterface.BUTTON_NEUTRAL,
"today",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Neutral Button
}
}
);
dpdialog.show();
最佳答案
外部类中定义的变量将从内部类/内联方法中可见,因此您可以这样做:
private String day;
private String month;
private String year;
DatePickerDialog dpdialog = new DatePickerDialog(me, new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int pickyear, int monthOfYear, int dayOfMonth) {
// you have the picked year/month/day here.
// For example:
day = String.valueOf(dayOfMonth);
month = String.valueOf(monthOfYear);
year = String.valueOf(pickyear);
}
}, year, month-1, day);
关于android - 如何获取android中DatePickerDialog中设置的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9677721/