同一活动中多个EditEdit的日期选择器

时间:2018-12-11 12:29:54

标签: java android

在我的应用程序中,我有两个edittext,并且要在打开两个edittext datepicker对话框时单击并在编辑文本中设置两个值。

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateDOB();
            updateDOA();
        }

    };
    doa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(FamilyEditActivity.this, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
    spoucedob.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            new DatePickerDialog(FamilyEditActivity.this, date, myCalendar
                    .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                    myCalendar.get(Calendar.DAY_OF_MONTH)).show();
        }
    });
}

private void updateDOA() {
    String myFormat = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    doa.setText(sdf.format(myCalendar.getTime()));
}
private void updateDOB() {
    String myFormat = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    spoucedob.setText(sdf.format(myCalendar.getTime()));
}

这是我的代码。当我在一个edittext中设置日期时,它会同时更改其他edittext的值。请告诉我是什么问题... 谢谢。

1 个答案:

答案 0 :(得分:3)

在类级别声明一个变量:

EditText clickedEditText;

,在点击监听器中,将其设置为点击的EditText:

doa.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        clickedEditText = (EditText) view;
        new DatePickerDialog(FamilyEditActivity.this, date, myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});
spoucedob.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        clickedEditText = (EditText) view;
        new DatePickerDialog(FamilyEditActivity.this, date, myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)).show();
    }
});

现在您只需要一种方法update()

private void update() {
    String myFormat = "yyyy-MM-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
    clickedEditText.setText(sdf.format(myCalendar.getTime()));
}

onDateSet()中的

final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
        @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);
            update();
        }
};