Android: DatePickerDialog 只输入当前日期,不输入选择日期
Android: DatePickerDialog inputs current date only and not chosen date
我正在制作一个日期选择器对话框,用户可以在其中选择一个日期并将其发送到数据库。问题是每当我使用对话框时它只显示当前日期而不是选择的日期
代码片段:
searchField = (EditText) findViewById(R.id.editText1);
final Calendar myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
searchField.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(Attendance.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
private void updateLabel() {
String myFormat = "yyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
searchField.setText(sdf.format(myCalendar.getTime()));
}
这是我的 DatePickerDialog
我已经在 textView
上设置了 onClickListener()
在您的应用程序中使用它,效果很好。
int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
dateView = (TextView) findViewById(R.id.date_view);
dateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog dpd = new DatePickerDialog(MyActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
c.set(year, month, day);
String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
dateView.setText(date);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
}, mYear, mMonth, mDay);
dpd.show();
}
});
我正在制作一个日期选择器对话框,用户可以在其中选择一个日期并将其发送到数据库。问题是每当我使用对话框时它只显示当前日期而不是选择的日期
代码片段:
searchField = (EditText) findViewById(R.id.editText1);
final Calendar myCalendar = Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
searchField.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new DatePickerDialog(Attendance.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
private void updateLabel() {
String myFormat = "yyy-MM-dd"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
searchField.setText(sdf.format(myCalendar.getTime()));
}
这是我的 DatePickerDialog
我已经在 textView
上设置了 onClickListener()
在您的应用程序中使用它,效果很好。
int mYear, mMonth, mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
dateView = (TextView) findViewById(R.id.date_view);
dateView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog dpd = new DatePickerDialog(MyActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
c.set(year, month, day);
String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
dateView.setText(date);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
}, mYear, mMonth, mDay);
dpd.show();
}
});