我想使用datePickerDialog将两个不同的Date设置为两个不同的EditText。我使用两个不同的String对象来存储日期,然后分别在两个不同的EditText上设置它们,但问题是EditText都采用相同的值。
protected Dialog onCreateDialog(int id) {
// Get the calander
java.util.Calendar c = java.util.Calendar.getInstance();
// From calander get the year, month, day, hour, minute
int year = c.get(java.util.Calendar.YEAR);
int month = c.get(java.util.Calendar.MONTH);
int day = c.get(java.util.Calendar.DAY_OF_MONTH);
switch (id) {
case Date_from:
// Open the datepicker dialog
return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
month, day);
case Date_to:
return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
month, day);
}
return null;
}
// Date picker dialog
DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// store the data in one string and set it to edittext
String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextFrom.setText(date1);
String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextTo.setText(date2);
}
};
}
答案 0 :(得分:0)
它的发生是因为你的两个Strings对象都有相同的数据,来自方法。你正在做的只是将数据保存在不同的对象上,但数据是相同的。
String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextFrom.setText(date1);
String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextTo.setText(date2);
答案 1 :(得分:0)
它正在发生,因为您为editText设置了相同的值,如下所示,
String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextFrom.setText(date1);
String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextTo.setText(date2);
由于我们没有您的完整源代码,但您可以在这里尝试一些补丁,您可以传递另一个arg布尔值,
switch (id) {
case Date_from:
// Open the datepicker dialog
return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
month, day, true);
case Date_to:
return new DatePickerDialog(WastageRecordsByDateActivity.this, date_listener, year,
month, day, false);
}
return null;
}
// Date picker dialog
DatePickerDialog.OnDateSetListener date_listener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day, Boolean stat) {
// store the data in one string and set it to edittext
if(stat==true){
String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextFrom.setText(date1);
}
if(stat==false){
String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextTo.setText(date2);
}
}
};
答案 2 :(得分:0)
试试这个
switch (id) {
case Date_from:
// Open the datepicker dialog
return new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String date1 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextFrom.setText(date1);
}
}, 2017, 04, 25);
case Date_to:
return new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String date2 = String.valueOf(day) + "-" + String.valueOf(month + 1)
+ "-" + String.valueOf(year);
editTextTo.setText(date2);
}
}, 2017, 04, 25);
}
答案 3 :(得分:0)
试试这个,让我知道兄弟,
edtxt1a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edtxt1a.getWindowToken(), 0);
showDispatchCalendar();
}
});
private void showDispatchCalendar() {
Calendar c1 = Calendar.getInstance();
DatePickerDialog da = new DatePickerDialog(getActivity(), mDateSetListener, sYearIni, sMonthIni, sDayIni);
c1.add(DATE, 0);
Date newDate = c1.getTime();
da.getDatePicker().setMinDate(newDate.getTime());
da.show();
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYearIni = year;
mMonthIni = monthOfYear;
mDayIni = dayOfMonth;
colocar_fecha();
}
};
private void colocar_fecha() {
day1 = mDayIni;
month1 = mMonthIni + 1;
year1 = mYearIni;
edtxt2a.setEnabled(true);
edtxt2a.setBackgroundResource(R.drawable.rounded_border_edittext);
edtxt1a.setText(day1 + "-" + month1 + "-" + year1 + " ");
}