1

我在 Java 文件中创建了 datePicker 对话框。这是一个代码:

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);
    //return new DatePickerDialog(getActivity(), (EditSessionActivity)getActivity(), year, month, day);

    // 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) {

        mDateDisplay .setText(String.valueOf(day) + "/"
                + String.valueOf(month + 1) + "/" + String.valueOf(year));
        // set selected date into datepicker also
    }
    }

我想将 ID 添加到我的 DatePicker。我怎样才能从 Java 文件中做到这一点。

4

2 回答 2

0

核实:

static final int DATE_DIALOG_ID = 999;
showDialog(DATE_DIALOG_ID);

覆盖以下内容:

protected Dialog onCreateDialog(int id) {
Calendar c = Calendar.getInstance();
int cyear = c.get(Calendar.YEAR);
int cmonth = c.get(Calendar.MONTH);
int cday = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(this,  mDateSetListener,  cyear, cmonth, cday);
}

我还需要一个Listner:

    private 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) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;

 //set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
   .append("-").append(day).append("-").append(year)
   .append(" "));

// set selected date into datepicker also
dpResult.init(year, month, day, null);

}
};
于 2013-06-09T12:53:13.713 回答
0

看看 android API 参考总是好的。看看这个这个

无论如何,作为答案,您可以执行以下操作

DatePicker picker=datePickerDialog.getDatePicker();
picker.setId("picker");
于 2013-06-09T13:02:41.317 回答