java - Android 使用单选组更改动态 View

标签 java android listeners

如何根据选择的单选按钮动态更新布局? 目前我的所有代码都在 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 并导致持续时间描述和框文本出错,并显示“无法分配最终局部变量描述,因为它是在封闭类型中定义的”<- 仅在以下情况下才会这样做我也将复选框作为最后一个)

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");
                    }
            }
            }
          }

        });

最佳答案

你已经在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

            }
        });

在上述监听器中发布代码

您还可以在单​​选按钮上使用 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);
}
}

关于java - Android 使用单选组更改动态 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197441/

相关文章:

java - 消除最后一个逗号和空格

java - 使用 java 计算所有构成有效命令的子字符串

java - JXLS - 如何在工作簿中创建指向 Excel 工作表的超链接

java - 错误 : field name cannot be declared static

android - 为什么 FocusManager 在 AlertDialog 中不起作用?

android - 将异步监听器转换/包装为 Observable (RxJava2)

java - Eclipse SWT : How to disable scrolledcomposite scrolling on mousewheel

android - 如何在移动网站上使用 dojo 绘制圆环图

java - 请求方法 GET 返回错误的内容长度

java - Android 日期选择器监听器不能工作超过一次..