2

我有下一个代码:

dateDisplay1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
    public void onClick(View v) {
        option = 1;
        showDialog(DATE_DIALOG_ID);
    }
});

和 onCreateDialog 函数:

protected Dialog onCreateDialog(int id) {

    Calendar cal = Calendar.getInstance();

    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, dayDate, cal.get(Calendar.YEAR),      cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
    case TIME_DIALOG_ID:
        return new TimePickerDialog(this, timeDate, cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), false);
    }
    return null;
}

我想要做的是将已弃用的 showDialog() 调整为 DialogFragment,但没有成功。下面的代码有效,但它不是最佳实践。所以我想更正代码。

你将如何实现它?

4

2 回答 2

10
public class ReminderEditActivity extends Activity {
    private Button mDateButton;
    private Button mTimeButton;
    public Calendar mCalendar;
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String TIME_FORMAT = "kk:mm";
    DialogFragment dateFragment;
    DialogFragment timeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_edit);
        mTimeButton = (Button) findViewById(R.id.reminder_time);
        mDateButton = (Button) findViewById(R.id.reminder_date);
        mCalendar = Calendar.getInstance();
        mDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePickerDialog(v);
            }
            });
        mTimeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTimePickerDialog(v);
            }
            });

    }

    public void showDatePickerDialog(View v) {
        dateFragment = new DatePickerFragment();
        dateFragment.show(getFragmentManager(), "datePicker");

    }

    public void showTimePickerDialog(View v) {
        timeFragment = new TimePickerFragment();
        timeFragment.show(getFragmentManager(), "timePicker");
    }

    public void updateDateButtonText() {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        String dateForButton = dateFormat.format(mCalendar.getTime());
        mDateButton.setText(dateForButton);
    }
    private void updateTimeButtonText() {
        SimpleDateFormat timeFormat = new SimpleDateFormat(TIME_FORMAT);
        String timeForButton = timeFormat.format(mCalendar.getTime());
        mTimeButton.setText(timeForButton);
        }

    class DatePickerFragment extends DialogFragment implements
            DatePickerDialog.OnDateSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current date as the default date in the picker
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            // Create a new instance of DatePickerDialog and return it
            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        public void onDateSet(DatePicker view, int year, int month, int day) {
            mCalendar.set(Calendar.YEAR, year);
            mCalendar.set(Calendar.MONTH, month);
            mCalendar.set(Calendar.DAY_OF_MONTH, day);
            updateDateButtonText();
        }
    }

    public class TimePickerFragment extends DialogFragment implements
            TimePickerDialog.OnTimeSetListener {

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the current time as the default values for the picker
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(getActivity(), this, hour, minute,
                    DateFormat.is24HourFormat(getActivity()));
        }

        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            mCalendar.set(Calendar.MINUTE, minute);
            updateTimeButtonText();
        }
    }

}
于 2013-10-29T08:03:48.383 回答
0

下面的代码有助于在我的适配器中过去几天受限的警报对话框中显示日历。

  changedate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(type.equals("1") ) {

                DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

                    // when dialog box is closed, below method will be called.
                    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
                        mmonth = selectedMonth;
                        mday = selectedDay;
                        myear = selectedYear;


                        changedate.setText(new StringBuilder()
                                .append(mday).append("-").append(mmonth + 1).append("-")
                                .append(myear).append(" "));

                    }

                };
                switch (DATE_DIALOG_ID) {
                    case DATE_DIALOG_ID:

                        dat1 = new DatePickerDialog(context, datePickerListener, myear, mmonth, mday);
                        c = Calendar.getInstance();
                        c.add(Calendar.DATE, 0);
                        Date newDate = c.getTime();
                        dat1.getDatePicker().setMinDate(newDate.getTime()-1000);
                        dat1.getDatePicker().setMinDate(newDate.getTime() - (newDate.getTime() % (24 * 60 * 60 * 1000)));
                        dat1.setButton(DatePickerDialog.BUTTON_POSITIVE, context.getString(R.string.ok), dat1);
                        dat1.setButton(DatePickerDialog.BUTTON_NEGATIVE, context.getString(R.string.cancel), dat1);


                }
                dat1.show();
            }

        }
    });
于 2017-05-10T09:24:50.453 回答