Java Android使用放射组更改动态视图
如何根据选择的单选按钮动态更新布局? 目前,我的所有代码都在oncreate中,它似乎只检查一次以查看选择了哪个单选按钮,但如何使它在每次更改时都更新 这是我的密码Java Android使用放射组更改动态视图,java,android,listeners,Java,Android,Listeners,如何根据选择的单选按钮动态更新布局? 目前,我的所有代码都在oncreate中,它似乎只检查一次以查看选择了哪个单选按钮,但如何使它在每次更改时都更新 这是我的密码 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout mainLinear = new LinearLayout(this); mainLinear.setO
@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();
}
});
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout Main Linear=新的LinearLayout(本);
主线性。设置方向(线性布局。垂直);
LinearLayout按钮Layout=新的LinearLayout(此);
按钮布局。设置方向(线性布局。水平);
按钮布局。设置填充(120,15,0,0);
mainLinear.addView(按钮布局);
按钮deleteSelectedButton=新按钮(此按钮);
deleteSelectedButton.setText(“删除选定项”);
按钮backButton=新按钮(此按钮);
backButton.setText(“Back”);
ButtonLayout.addView(deleteSelectedButton);
按钮布局。添加视图(backButton);
LinearLayout DeleteAppointLayout=新的LinearLayout(此);
DeleteAppointLayout.setOrientation(LinearLayout.VERTICAL);
mainLinear.addView(删除指定布局);
TextView dayLabel=新的TextView(此);
dayLabel.setText(“天”);
设置填充(220,10,0,0);
dayLabel.setTypeface(Typeface.DEFAULT_BOLD);
dayLabel.setTextSize(15);
DeleteAppointLayout.addView(dayLabel);
射线组射线组=新射线组(此);
射线组设置方向(线性布局。水平);
放射组设置填充(10,0,0,0);
DeleteAppointLayout.addView(放射组);
RadioButton mondayRadioButton=新RadioButton(本);
RadioButton周二RadioButton=新RadioButton(本);
RadioButton周三RadioButton=新RadioButton(本);
RadioButton thursdayRadioButton=新RadioButton(本);
RadioButton fridayRadioButton=新的RadioButton(本);
mondayRadioButton.setText(“Mon”);
星期二RadioButton.setText(“星期二”);
周三RadioButton.setText(“周三”);
星期四(星期四);
fridayRadioButton.setText(“Fri”);
周一单选按钮。SettexSize(12);
星期二RadioButton.SettexSize(12);
星期三单选按钮。SettexSize(12);
周四RadioButton.setTextSize(12);
星期五RadioButton.SettexSize(12);
周一RadioButton.setChecked(真);
周一RadioButton.setId(1);
星期二RadioButton.setId(2);
星期三RadioButton.setId(3);
星期四拉迪奥布顿setId(4);
星期五单选按钮。设置ID(5);
//OnClickListener-radlistener;
//radioGroup.setOnClickListener(radlistener);
radioGroup.addView(周一单选按钮);
radioGroup.addView(周二RadioButton);
radioGroup.addView(星期三单选按钮);
radioGroup.addView(周四RadioButton);
radioGroup.addView(星期五RadioButton);
复选框=新复选框(此复选框);
ScrollView ScrollView=新的ScrollView(此);
mainLinear.addView(滚动视图);
LinearLayout DeleteAppointSlayout=新的LinearLayout(本);
DeleteAppointSlayout.setOrientation(线性布局.垂直);
scrollView.addView(DeleteAppointsLayout);
APData=新任命数据源(此);
APData.open();
//printf(“转到这里”);
列表<约会>约会=APData.retrieveAllAppoints();
APData.close();
字符串时间、持续时间、说明、文本框;
长id;
int循环=0;
if(mondayRadioButton.isChecked()){
DeleteAppointSlayout.removeAllViews();
for(迭代器i=appoints.Iterator();i.hasNext();){
预约项目=i.下一步();
如果(item.getDay()==1){
System.out.println(“他妈的没用”);
id=item.getId();
time=item.getTime();
duration=item.getDuration();
description=item.getDescription();
boxText=时间+”,“+持续时间+”,“+说明;
checkBox.setText(boxText);
checkBox.setId((int)id);
DeleteAppointSlayout.addView(复选框);
}否则{
System.out.println(“该死的没用”);
}
}
}
如果(星期二RadioButton.isChecked()){
DeleteAppointSlayout.removeAllViews();
for(迭代器i=appoints.Iterator();i.hasNext();){
预约项目=i.下一步();
if(item.getDay()==2){
id=item.getId();
time=item.getTime();
duration=item.getDuration();
description=item.getDescription();
boxText=时间+”,“+持续时间+”,“+说明;
checkBox.setText(boxText);
checkBox.setId((int)id);
DeleteAppointSlayout.addView(复选框);
}else{}
}
}
如果(星期三RadioButton.isChecked()){
DeleteAppointSlayout.removeAllViews();
for(迭代器i=appoints.Iterator();i.hasNext();){
预约项目=i.下一步();
如果(item.getDay()==3){
id=item.getId();
time=item.getTime();
duration=item.getDuration();
description=item.getDescription();
boxText=时间+”,“+持续时间+”,“+说明;
checkBox.setText(boxText);
checkBox.setId((int)id);
DeleteAppointSlayout.addView(复选框);
}else{}
}
}
if(thursdayRadioButton.isChecked()){
DeleteAppointSlayout.removeAllViews();
for(迭代器i=appoints.Iterator();i.hasNext();){
预约项目=i.下一步();
如果(item.getDay()==4){
id=item.getId();
time=item.getTime();
duration=item.getDuration();
description=item.getDescription();
boxText=时间+”,“+持续时间+”,“+说明;
checkBox.setText(boxText);
checkBox.setId((int)id);
DeleteAppointSlayout.addView(复选框);
}else{}
}
}
如果(fridayRadioButton.isChecked()){
DeleteAppointSlayout.removeAllViews();
for(迭代器i=appoints.Iterator();i.hasNext();){
预约项目=i.下一步();
如果(item.getDay()==5){
id=item.getId();
时间=i
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");
}
}
}
}
});
radiobutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//your code here
}
});
grp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
//your code here
}
});
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);
}
}