我的应用程序中有多个DatePickers,我试图单个方法为多个TextView设置datepicker而不是将Datepicker放到每个TextView,Date对话框似乎正确显示但更改只更新到最后一个项目
这是我创建的方法
public void Setdate( final EditText setdateEditview){
setdateEditview.setInputType(InputType.TYPE_NULL);
setdateEditview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog.show();
Log.d ("ttt","herre we o"+setdateEditview);
}
});
Calendar newCalendar = Calendar.getInstance();
DatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
Log.d ("ttt","herre we o"+setdateEditview);
setdateEditview.setText(dateFormatter.format(newDate.getTime()));
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
我正在通过此方式调用上述方法....
mCustomerDueDateEditText = (EditText) findViewById(R.id.edit_CustommerDueDate);
mForecastedDueDateEditText = (EditText) findViewById(R.id.edit_ForcastedDueDate);
Setdate(mCustomerDueDateEditText);
Setdate(mForecastedDueDateEditText);
答案 0 :(得分:0)
像这样更改方法签名,并在想要更新时循环遍历它。
public void Setdate( final EditText... setdateEditviewList){
setdateEditview.setInputType(InputType.TYPE_NULL);
setdateEditview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DatePickerDialog.show();
Log.d ("ttt","herre we o"+setdateEditview);
}
});
Calendar newCalendar = Calendar.getInstance();
DatePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar newDate = Calendar.getInstance();
newDate.set(year, monthOfYear, dayOfMonth);
Log.d ("ttt","herre we o"+setdateEditview);
//loop through the EditText here
EditText et;
for(int = 0; i<setdateEditviewList.length; i++){
et = setdateEditviewList[i];
et.setText(dateFormatter.format(newDate.getTime()));
}
}
},newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}
按如下方式调用方法:
mCustomerDueDateEditText = (EditText) findViewById(R.id.edit_CustommerDueDate);
mForecastedDueDateEditText = (EditText) findViewById(R.id.edit_ForcastedDueDate);
Setdate(mCustomerDueDateEditText, mForecastedDueDateEditText);