0
在我的活動中,我需要2個日期選擇器。不幸的是,我不知道如何編碼。由於空間不足,我需要將它們顯示爲對話框。一個活動中的兩個DatePickers
這裏就是我已經和適用於一個的DatePicker:
public static class DatePickerFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
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(),
(DatePickerDialog.OnDateSetListener)
getActivity(), year, month, day);
}
}
public void datePicker(View view) {
DatePickerFragment fragment = new DatePickerFragment();
fragment.show(getSupportFragmentManager(), "date");
}
和相應的監聽器:
private void setDate(final Calendar calendar) {
final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
date = calendar.getTime();
Button btn = (Button) findViewById(R.id.date_text);
dateString = dateFormat.format(calendar.getTime());
btn.setText(dateString);
}
現在我需要爲第二個片段的第二個偵聽器,但我不知道如何執行它。 (片段只與已經安裝監聽器返回對話框)\
編輯:
我想出了主意,但按鈕文本後,才第2次嘗試設置:
public void datePicker2 (View view){
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);
DatePickerDialog dialog = new DatePickerDialog(this,listener,year,month,day);
dialog.show();
listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
Calendar cal = new GregorianCalendar(i, i1, i2);
setDateTo(cal);
}
};
}
private void setDateTo(final Calendar calendar) {
final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
date = calendar.getTime();
Button btn = (Button) findViewById(R.id.date_to_text);
dateString = dateFormat.format(calendar.getTime());
btn.setText(dateString);
}
您是否想擁有兩個'DatePicker'小部件在一個對話框中?或者你指的是有兩個單獨的'DatePickerDialogs'? – CommonsWare
兩個獨立的DatePickerDialogs – AndroidBegginer