使用datepicker - Android在两个edittext中设置日期

时间:2014-07-04 04:16:55

标签: android datepicker

我是编程新手,我正在开发一款Android应用。在那里我有两个edittext,我需要使用日期选择器设置不同的日期。 问题是,当它从datepicker小部件设置日期时,两个edittexts的日期值都会更改。我怎样才能解决这个问题???  这是我的代码

 Calendar myCalendar = Calendar.getInstance();
 Calendar myCalendar2 = Calendar.getInstance();

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
    myCalendar.set(Calendar.YEAR, year);
    myCalendar.set(Calendar.MONTH, monthOfYear);
    myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

    myCalendar2.set(Calendar.YEAR, year);
    myCalendar2.set(Calendar.MONTH, monthOfYear);
    myCalendar2.set(Calendar.DAY_OF_MONTH, dayOfMonth);

    updateLabel();
    updateLabel2();

}

 private void updateLabel() {
        sDate.setText(sdf.format(myCalendar.getTime()));
        }
 private void updateLabel2() {
        eDate.setText(sdf.format(myCalendar2.getTime()));
        }

}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.et_SDate: 
         new DatePickerDialog(Domestic.this,this, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        break;

    case R.id.et_EDate:
        new DatePickerDialog(Domestic.this,this, myCalendar2
                .get(Calendar.YEAR), myCalendar2.get(Calendar.MONTH),
                myCalendar2.get(Calendar.DAY_OF_MONTH)).show();
        break;
    }
}

0 个答案:

没有答案