我使用按钮作为日期选择器。选择日期后,应在按钮上设置日期。我按照语法或动态生成此按钮。当我点击按钮时它只会改变创建的最后一个按钮的文本。提前谢谢......
private void init() {
edit = new ArrayList<EditText>();
listButtons = new ArrayList<Button>();
add = (Button) findViewById(R.id.add);
dynamic = (LinearLayout) findViewById(R.id.dynamic);
params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addLayout();
}
});
ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0;i<edit.size();i++)
{
String names = edit.get(i).getText().toString();
String Min = listButtons.get(i).getText().toString();
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
Time today = new Time(Time.getCurrentTimezone());
today.setToNow();
day = today.monthDay;
month = today.month;
year= today.year;
return new DatePickerDialog(this, datePickerListener, year,
month, day);
}
return new DatePickerDialog(this, datePickerListener, year,
month, day);
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth + 1;
day = selectedDay;
btn_date.setText(day+"/"+month+"/"+year);
}
};
private void addLayout() {
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
editText = new EditText(this);
editText.setId(j);
editText.setText("Test "+(j+1)+" ");
ll.addView(dose);
edit.add(dose);
// Create Button
btn_date = new Button(this);
// Give button an ID
btn_date.setId(j);
btn_date.setHint("Select Date");
// set the layoutParams on the button
btn_date.setLayoutParams(params);
final int index = j;
// Set click listener for button
btn_date.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + index+dose.getText().toString(),
Toast.LENGTH_LONG).show();
System.out.println(dose.getText().toString());
showDialog(DATE_DIALOG_ID+index);
}
});
//Add button to LinearLayout
ll.addView(btn_date);
listButtons.add(btn_date);
//Add button to LinearLayout defined in XML
dynamic.addView(ll);
j++;
}
}
答案 0 :(得分:1)
onClick有参数View v
,它引用了单击的元素(Button)。转换为Button以使setText方法可用
@Override
public void onClick(View v) {
((Button)v).setText("whatever");
}
如果要从addLayout方法中访问它,只需将其作为参数传递
@Override
public void onClick(View v) {
addLayout(v);
}
答案 1 :(得分:0)
保存您在onclick事件中的视图以及何时取消对话框,即在datePickerListener中使用该视图而不是btn_date;像这样的东西
Button clickedBtnDate = null;
btn_date.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
clickedBtnDate = (Button)v //instance of the button clicked is saved here in clickedBtnDate
Log.i("TAG", "index :" + index);
Toast.makeText(getApplicationContext(),
"Clicked Button Index :" + index+dose.getText().toString(),
Toast.LENGTH_LONG).show();
System.out.println(dose.getText().toString());
showDialog(DATE_DIALOG_ID+index);
}
});
现在在datePickerListener中执行
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth + 1;
day = selectedDay;
//btn_date.setText(day+"/"+month+"/"+year);
clickedBtnDate.setText(day+"/"+month+"/"+year);
}
};