如何根据选择的单选按钮动态更新布局? 目前我在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错误,“无法分配最终的局部变量描述,因为它是在封闭类型中定义的”&lt; - 只有这样做当我把checkBox作为决赛时也是如此)
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");
}
}
}
}
});
答案 0 :(得分:0)
你已经在Oncreate中编写了上面的代码,它会在你进入屏幕时第一次工作,之后就不会了,所以你需要在事件触发时执行上面的代码
对于单个单选按钮设置onClickLister
radiobutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//your code here
}
});
对于像这样的小组setOnCheckedChangeListener
grp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
//your code here
}
});
在上述侦听器中发布代码
您还可以在radiobutton上使用onCheckChangeListener
radiobtn.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// your code here
}
});
修改强>
public class MainActivity extends Activity {
EditText message, password, username; // these are called fields
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//instantiate like this
username = new EditText(this);
password = (EditText) findViewById(R.id.editText2);
message = (EditText) findViewById(R.id.editText3);
}
}
答案 1 :(得分:0)
您可以尝试在RadioGroup上实现“onCheckedChanged()”,然后更新布局中的信息,甚至根据已检查的无线电充气不同的布局。