如何根据选择的单选按钮动态更新布局?目前我在 oncreate 中有我的所有代码,它似乎只检查一次以查看选择了哪个单选按钮,但我怎样才能让它在每次更改时更新?
这是我的代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainLinear = new LinearLayout(this);
mainLinear.setOrientation(LinearLayout.VERTICAL);
LinearLayout ButtonLayout = new LinearLayout(this);
ButtonLayout.setOrientation(LinearLayout.HORIZONTAL);
ButtonLayout.setPadding(120, 15, 0, 0);
mainLinear.addView(ButtonLayout);
Button deleteSelectedButton = new Button(this);
deleteSelectedButton.setText("Delete Selected");
Button backButton = new Button(this);
backButton.setText("Back");
ButtonLayout.addView(deleteSelectedButton);
ButtonLayout.addView(backButton);
LinearLayout deleteAppointmentLayout = new LinearLayout(this);
deleteAppointmentLayout.setOrientation(LinearLayout.VERTICAL);
mainLinear.addView(deleteAppointmentLayout);
TextView dayLabel = new TextView(this);
dayLabel.setText("Days");
dayLabel.setPadding(220, 10, 0, 0);
dayLabel.setTypeface(Typeface.DEFAULT_BOLD);
dayLabel.setTextSize(15);
deleteAppointmentLayout.addView(dayLabel);
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(LinearLayout.HORIZONTAL);
radioGroup.setPadding(10, 0, 0, 0);
deleteAppointmentLayout.addView(radioGroup);
RadioButton mondayRadioButton = new RadioButton(this);
RadioButton tuesdayRadioButton = new RadioButton(this);
RadioButton wednesdayRadioButton = new RadioButton(this);
RadioButton thursdayRadioButton = new RadioButton(this);
RadioButton fridayRadioButton = new RadioButton(this);
mondayRadioButton.setText("Mon");
tuesdayRadioButton.setText("Tue");
wednesdayRadioButton.setText("Wed");
thursdayRadioButton.setText("Thu");
fridayRadioButton.setText("Fri");
mondayRadioButton.setTextSize(12);
tuesdayRadioButton.setTextSize(12);
wednesdayRadioButton.setTextSize(12);
thursdayRadioButton.setTextSize(12);
fridayRadioButton.setTextSize(12);
mondayRadioButton.setChecked(true);
mondayRadioButton.setId(1);
tuesdayRadioButton.setId(2);
wednesdayRadioButton.setId(3);
thursdayRadioButton.setId(4);
fridayRadioButton.setId(5);
//OnClickListener radlistener;
//radioGroup.setOnClickListener(radlistener);
radioGroup.addView(mondayRadioButton);
radioGroup.addView(tuesdayRadioButton);
radioGroup.addView(wednesdayRadioButton);
radioGroup.addView(thursdayRadioButton);
radioGroup.addView(fridayRadioButton);
CheckBox checkBox = new CheckBox(this);
ScrollView scrollView = new ScrollView(this);
mainLinear.addView(scrollView);
LinearLayout deleteAppointmentsLayout = new LinearLayout(this);
deleteAppointmentsLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(deleteAppointmentsLayout);
APData = new AppointmentDataSource(this);
APData.open();
//printf("Go to here");
List < Appointment > appointments = APData.retrieveAllAppointments();
APData.close();
String time, duration, description, boxText;
long id;
int loop = 0;
if (mondayRadioButton.isChecked()) {
deleteAppointmentsLayout.removeAllViews();
for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
Appointment item = i.next();
if (item.getDay() == 1) {
System.out.println("fucken did work");
id = item.getId();
time = item.getTime();
duration = item.getDuration();
description = item.getDescription();
boxText = time + ", " + duration + ", " + description;
checkBox.setText(boxText);
checkBox.setId((int) id);
deleteAppointmentsLayout.addView(checkBox);
} else {
System.out.println("fucken didnt work");
}
}
}
if (tuesdayRadioButton.isChecked()) {
deleteAppointmentsLayout.removeAllViews();
for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
Appointment item = i.next();
if (item.getDay() == 2) {
id = item.getId();
time = item.getTime();
duration = item.getDuration();
description = item.getDescription();
boxText = time + ", " + duration + ", " + description;
checkBox.setText(boxText);
checkBox.setId((int) id);
deleteAppointmentsLayout.addView(checkBox);
} else {}
}
}
if (wednesdayRadioButton.isChecked()) {
deleteAppointmentsLayout.removeAllViews();
for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
Appointment item = i.next();
if (item.getDay() == 3) {
id = item.getId();
time = item.getTime();
duration = item.getDuration();
description = item.getDescription();
boxText = time + ", " + duration + ", " + description;
checkBox.setText(boxText);
checkBox.setId((int) id);
deleteAppointmentsLayout.addView(checkBox);
} else {}
}
}
if (thursdayRadioButton.isChecked()) {
deleteAppointmentsLayout.removeAllViews();
for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
Appointment item = i.next();
if (item.getDay() == 4) {
id = item.getId();
time = item.getTime();
duration = item.getDuration();
description = item.getDescription();
boxText = time + ", " + duration + ", " + description;
checkBox.setText(boxText);
checkBox.setId((int) id);
deleteAppointmentsLayout.addView(checkBox);
} else {}
}
}
if (fridayRadioButton.isChecked()) {
deleteAppointmentsLayout.removeAllViews();
for (Iterator < Appointment > i = appointments.iterator(); i.hasNext();) {
Appointment item = i.next();
if (item.getDay() == 5) {
id = item.getId();
time = item.getTime();
duration = item.getDuration();
description = item.getDescription();
boxText = time + ", " + duration + ", " + description;
checkBox.setText(boxText);
checkBox.setId((int) id);
deleteAppointmentsLayout.addView(checkBox);
} else {}
}
}
this.setContentView(mainLinear);
deleteSelectedButton.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View view) {}
});
backButton.setOnClickListener(new View.OnClickListener() {@
Override
public void onClick(View view) {
finish();
}
});
}
更新的代码(希望我将所有变量更改为 FINAL 并导致持续时间描述和 boxtext 错误,“无法分配最终的局部变量描述,因为它是在封闭类型中定义的”< - 只有当我制作复选框时才会这样做决赛)
mondayRadioButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mondayRadioButton.isChecked()){
deleteAppointmentsLayout.removeAllViews();
for(Iterator<Appointment> i = appointments.iterator(); i.hasNext();){
Appointment item = i.next();
if(item.getDay() == 1){
System.out.println("fucken did work");
id = item.getId();
time = item.getTime();
duration = item.getDuration();
description = item.getDescription();
boxText = time + ", " + duration + ", " + description;
checkBox.setText(boxText);
checkBox.setId((int) id);
deleteAppointmentsLayout.addView(checkBox);
}
else {
System.out.println("fucken didnt work");
}
}
}
}
});