我已经使用循环动态创建了多个EditText。所以我创建了多个具有相同名称的EditText。当我点击EditText时,我正在调用DatePickerDialog。我可以选择日期和设置完成。完成后,所选日期仅设置为我创建的最后一个EditText。
我想设置所有EditText的日期。是否可以在使用相同名称创建的EditText中设置值?
这是我的代码。
for(int i =0;i<5;i++)
{
estimate_closedate = new EditText(getActivity());
estimate_closedate.setLayoutParams(params1);
estimate_closedate.setWidth(1000);
estimate_closedate.setHint("Estimated Close Date");
estimate_closedate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog dpd = new DatePickerDialog(context,
mDateSetListener, myear, mmonth, mday);
dpd.show();
}
});
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myear = year;
mmonth = monthOfYear + 1;
mday = dayOfMonth;
if (mmonth < 10) {
month = 0 + Integer.toString(mmonth);
} else {
month = Integer.toString(mmonth);
}
if (mday < 10) {
day = 0 + Integer.toString(mday);
} else {
day = Integer.toString(mday);
}
nextdate = (new StringBuilder().append(myear).append("-")
.append(month).append("-").append(day)).toString();
estimate_closedate.setText(nextdate);
}
}
};
答案 0 :(得分:0)
试试这个(请参阅我的comments
):
for(int i =0;i<5;i++)
{
EditText estimate_closedate = new EditText(getActivity()); //create new instance
estimate_closedate.setId(i); //set the id (0-4)
estimate_closedate.setLayoutParams(params1);
estimate_closedate.setWidth(1000);
estimate_closedate.setHint("Estimated Close Date");
estimate_closedate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog dpd = new DatePickerDialog(context,
mDateSetListener, myear, mmonth, mday);
dpd.show();
}
});
}
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myear = year;
mmonth = monthOfYear + 1;
mday = dayOfMonth;
if (mmonth < 10) {
month = 0 + Integer.toString(mmonth);
} else {
month = Integer.toString(mmonth);
}
if (mday < 10) {
day = 0 + Integer.toString(mday);
} else {
day = Integer.toString(mday);
}
nextdate = (new StringBuilder().append(myear).append("-")
.append(month).append("-").append(day)).toString();
//estimate_closedate.setText(nextdate);
for(int i = 0; i < 5; i++)
{
EditText estimate_closedate = (EditText) findViewbyId(i); //get the id previously set
estimate_closedate.setText(nextdate);
}
}
}
};
当您使用setId
循环创建EditText
和findViewbyId
以查找已创建的EditText
时,该点位于for
。