用户在UI上的EditText行输入日期。我将Bundle中的日期传递给DatePickerDialog片段。我希望DatePicker对话框显示用户的日期而非今天的日期,但今天的日期仍在继续显示。我在这里缺少什么?
活动文件:
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
fListenerEditText = (ListenerEditText) findViewById(R.id.FEditText);
fListenerEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && (fListenerEditText.getText().length() == 0)) {
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
else {
Bundle bundle = new Bundle();
Calendar c = Calendar.getInstance();
bundle.putInt("year", c.get(Calendar.YEAR));
bundle.putInt("month", c.get(Calendar.MONTH));
bundle.putInt("day", c.get(Calendar.DAY_OF_MONTH));
DatePickerFragment newFragment = new DatePickerFragment();
newFragment.setArguments(bundle);
newFragment.show(getSupportFragmentManager(), null);
}
}
});
片段文件:
...
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
Calendar c;
int year = 0, month = 0, day = 0;
public DatePickerFragment() {
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
if (getArguments() != null) {
c = Calendar.getInstance();
year = getArguments().getInt("year");
month = getArguments().getInt("month");
day = getArguments().getInt("day");
c.set(year, month, day);
}
else {
c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
}
DatePickerDialog picker = new DatePickerDialog(getActivity(),
this, year, month, day);
picker.getDatePicker().setCalendarViewShown(true);
picker.getDatePicker().setSpinnersShown(false);
picker.setTitle("Select a Due Date");
return picker;
}
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
EditText txtDate = (EditText) getActivity().getWindow().getDecorView().getRootView().findViewById(R.id.FEditText);
txtDate.setText((monthOfYear + 1) + "/" + dayOfMonth + "/" + year + " ");
txtDate.setSelection(txtDate.getText().length());
}
答案 0 :(得分:0)
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
year_p = c.get(Calendar.YEAR);
month_p = c.get(Calendar.MONTH);
day_p = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year_p, month_p, day_p
);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);
if (year < year_p)
{
// view.updateDate(year_p, month_p, day_p);
custom_dialog(getActivity());
}
else if (month < month_p && year == year_p)
{
// view.updateDate(year_p, month_p, day_p);
custom_dialog(getActivity());
}
else if (day < day_p && year == year_p && month == month_p)
{
// view.updateDate(year_p, month_p, day_p);
custom_dialog(getActivity());
}
else if(day<day_p && year<year_p && month<month_p)
{
custom_dialog(getActivity());
}
else if(day>=day_p && year>=year_p && month>=month_p)
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if(CommonCode.isPastDate(formattedDate, "yyyy-MM-dd")==false)
{
// Toast.makeText(getActivity(), "passssst", Toast.LENGTH_SHORT).show();
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
str_setdate=formattedDate;
}
}
}