【问题标题】:how to get id of checked radio from radio group如何从电台组获取已检查电台的 ID
【发布时间】:2014-01-29 05:25:27
【问题描述】:

我创建了动态单选组,每个组都有三个单选按钮,因此检查更改侦听器如何获取单选按钮的 id 在单选组中检查.....提前致谢..

    public class Rate_me_up extends Activity implements OnClickListener,
        OnCheckedChangeListener {
    LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rate_me_up);
        layout = (LinearLayout) findViewById(R.id.linear);
        Button btnp = (Button) findViewById(R.id.buttonp);
        btnp.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
        radioGroup.setOrientation(1);

        LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.addView(radioGroup, p);
        RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
        radioButtonView.setText("RadioButton");
        radioButtonView.setChecked(false);
        radioGroup.addView(radioButtonView, p);

        RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
        radioButtonView2.setText("RadioButton2");
        radioButtonView2.setChecked(false);
        radioGroup.addView(radioButtonView2, p);
        RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
        radioButtonView3.setText("RadioButton2");
        radioGroup.addView(radioButtonView3, p);
        radioButtonView3.setChecked(false);
        radioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

    }

}

【问题讨论】:

  • RadioButtonsetId() 方法的第一个设置 id。
  • 在这样做之后...如何获取 id ...
  • checkedId in onCheckedChanged 将返回您选中的RadioButton 的 ID。
  • 当你想获得 Radio 的 id 时,在 Button Click 或 Radio Click 上
  • 我已经发布了简单的答案stackoverflow.com/a/21422976/1318946,我认为你必须搜索,这是bit.ly/1goR2HD这样的基本问题

标签: android android-layout android-radiogroup android-radiobutton


【解决方案1】:

将单选按钮值转换为字符串。

这就是你需要尝试的方式,

点击时:

genradiocheck();
radiovalue =  radiobutton1.getText().toString();

`

    private boolean genradiocheck()
  {
    boolean gender_flag = false;

    if (r_group.getCheckedRadioButtonId() == -1) 
    {
      //
    } 
    else 
    {
        radiobutton1 = (RadioButton) findViewById(r_group
                  .getCheckedRadioButtonId());

     gender_flag = true;
    }

    return gender_flag;
  }

希望这会有所帮助。

【讨论】:

    【解决方案2】:
    switch (radioXO.getCheckedRadioButtonId()) {
                case R.id.rdo_X:
                    player1Character = "X";
                    break;
                case R.id.rdo_O:
                    player1Character = "O";
                    break;
    
                }
    

    【讨论】:

      【解决方案3】:

      这将对您有所帮助:

      int index = myRadioGroup.indexOfChild(findViewById(myRadioGroup.getCheckedRadioButtonId()));
      

      【讨论】:

        【解决方案4】:

        首先设置 RadioButtonsetId() 的 id 。执行以下操作,

        @Override
                public void onClick(View v) {
                    RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
                    radioGroup.setOrientation(1);
        
                    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                    layout.addView(radioGroup, p);
                    RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
                    radioButtonView.setId(1);
                    radioButtonView.setText("RadioButton");
                    radioButtonView.setChecked(false);
                    radioGroup.addView(radioButtonView, p);
        
                    RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
                    radioButtonView.setId(2);
                    radioButtonView2.setText("RadioButton2");
                    radioButtonView2.setChecked(false);
                    radioGroup.addView(radioButtonView2, p);
        
                    RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
                    radioButtonView.setId(3);
                    radioButtonView3.setText("RadioButton2");
                    radioGroup.addView(radioButtonView3, p);
                    radioButtonView3.setChecked(false);
                    radioGroup.setOnCheckedChangeListener(this);
        
                }
        
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
        
                switch (checkedId) {
                        case 1:
                            // first radiobutton is clicked
                            break;
                        case 2:
                            // second radiobutton is clicked
                            break;
                        case 3:
                            // third radiobutton is clicked
                            break;
        
        
                        default:
                            break;
                }
        

        【讨论】:

          【解决方案5】:

          您需要在尝试获取 id 之前设置 id 。使用以下代码。

             @Override
          public void onClick(View v) {
              RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
              radioGroup.setOrientation(1);
          
              LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                      LinearLayout.LayoutParams.WRAP_CONTENT,
                      LinearLayout.LayoutParams.WRAP_CONTENT);
              layout.addView(radioGroup, p);
              RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
              radioButtonView.setId(1);
              radioButtonView.setText("RadioButton");
              radioButtonView.setChecked(false);
              radioGroup.addView(radioButtonView, p);
          
              RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
              radioButtonView.setId(2);
              radioButtonView2.setText("RadioButton2");
              radioButtonView2.setChecked(false);
              radioGroup.addView(radioButtonView2, p);
              RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
              radioButtonView.setId(3);
              radioButtonView3.setText("RadioButton2");
              radioButtonView3.setChecked(false);
              radioGroup.addView(radioButtonView3, p);
              radioGroup.setOnCheckedChangeListener(this);
          
          }
          
          @Override
          public void onCheckedChanged(RadioGroup group, int checkedId) {
          Toast.makeText(Rate_me_up.this, "id : "+checkedId, Toast.LENGTH_SHORT).show();//or you can also get the id by using group.getCheckedRadioButtonId()
          }
          

          【讨论】:

            【解决方案6】:

            checkId 中的字段

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            }
            

            会给你检查radioButton的id。

            编辑:

            这样做:

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            
            switch(checkedId){
               case R.id.radio1: //assumed id of radioButton1
               //statements
               break;
            
               case R.id.radio2:
               .
               break; //so on
             }
            }
            

            对于动态单选按钮,通过从那里获取 id 进行比较 比如radioButton1.getId();

            【讨论】:

            • 他已经在onClick的最后一行设置了setOnCheckedChangeListener
            • 请解释一下你的意思。我的意思是您是否知道将生成多少个单选按钮,或者它完全取决于运行时的用户操作?
            猜你喜欢
            • 2011-01-09
            • 1970-01-01
            • 1970-01-01
            • 2018-04-21
            • 2015-10-01
            • 2017-06-21
            • 1970-01-01
            • 1970-01-01
            • 2011-09-03
            相关资源
            最近更新 更多