如何将一个DatePickerDialog用于多个EditText

时间:2015-10-13 18:15:12

标签: android android-datepicker

我有一个带有2个EditText(startDate和endDate)的活动。

我创建了一个函数,我将它传递给EditText,它将显示一个DatePicker:

private void showDatePicker(EditText et) {

    Calendar calendar = Calendar.getInstance();

    DatePickerDialog picker = new DatePickerDialog(getActivity(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));

    if (!Global.isEmpty(et)) {

        try {
            SimpleDateFormat sdf = new SimpleDateFormat(Global.USER_FRIENDLY_DATE_FORMAT, Locale.getDefault());
            calendar.setTime(sdf.parse(et.getText().toString()));
        } catch (ParseException e) {
            Toast.makeText(getActivity(), "Error setting the date", Toast.LENGTH_SHORT).show();
        }
    }
    picker.show();
}

当我选择日期时,我会在选择日期的情况下回复onDateSet()函数。

我的问题是,我怎么知道它是哪个EditText?

我知道有一个“DatePicker视图”作为参数传递给onDateSet(),但似乎无法弄清楚如何使用它,即使我应该这样做。

3 个答案:

答案 0 :(得分:2)

您可以通过调用DatepickerDialogpicker.getDatePicker()来设置setTag()的Tag to DatePicker对象。可以从回调函数{{1}中的datePicker对象中检索相同的标记}}

喜欢

onDateSet()

DatePickerDialog picker = new DatePickerDialog(getActivity(), listner , calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
 DatePicker pick = picker.getDatePicker().setTag(et.getId());

答案 1 :(得分:0)

您可以使用此功能,因为此代码可在我的应用中使用

var regex = new RegExp('^(?:533-)?\\d{3}-\\d{4}$');

console.log('533-123-4567'.match(regex));

答案 2 :(得分:0)

DatePickerDialog picker= new DatePickerDialog(getActivity(), this, year, month,day);
picker.getDatePicker().setTag("id")