【问题标题】:How to make the radio button checked in android while updating the form?更新表单时如何在android中选中单选按钮?
【发布时间】:2013-10-04 05:13:44
【问题描述】:

我有一个包含两个单选按钮(自配送和公司配送)的单选组的申请表。在插入详细信息时,一切正常,我可以通过以下代码获得单选按钮的文本:

radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

  @Override
  public void onCheckedChanged(RadioGroup arg0, int arg1) {
    // TODO Auto-generated method stub
    selectedId = radioGroup.getCheckedRadioButtonId();

    // find the radiobutton by returned id
    radioButton = (RadioButton) findViewById(selectedId);

    if (radioButton.isChecked()) {
      OPTIONS=radioButton.getText().toString();
      if (OPTIONS.equals("SelfDelivery")) {
        deliveryAddress.setVisibility(View.GONE);
        deliveryEdit.setVisibility(View.GONE);
      } else {
        deliveryAddress.setVisibility(View.VISIBLE);
        deliveryEdit.setVisibility(View.VISIBLE);
      }
    }
  }
});

当我按下编辑按钮并进入编辑页面时,我希望也可以根据用户选择单选按钮。 例如:如果用户正在选择公司交付(在编辑页面中,应在单选组中选择公司交付)。 不知道如何获取选中的id,并在编辑页面中选择单选组。

【问题讨论】:

  • 您可以将 Bundle 中的 OPTIONS 字符串传递给您的编辑页面,并在您的编辑页面 onCreate() 获取此字符串并检查您的值并根据它设置选中的单选按钮。

标签: android radio-group


【解决方案1】:
// try this
 // on edit try this way
        RadioButton radioButtonSelf;
        RadioButton radioButtonCompany;
        radioButtonSelf = (RadioButton) findViewById(R.id.selfradiobuttonid);
        radioButtonCompany = (RadioButton) findViewById(R.id.radioButtonCompany);

        if(OPTIONS.equals("SelfDelivery")){
            radioButtonSelf.setChecked(true);
            radioButtonCompany.setChecked(false);

        }else{
            radioButtonCompany.setChecked(true);
            radioButtonSelf.setChecked(false);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-10
    • 2017-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2019-02-27
    相关资源
    最近更新 更多