【问题标题】:Android get value of the selected radio buttonAndroid获取所选单选按钮的值
【发布时间】:2012-06-27 00:20:34
【问题描述】:

我有一个 RadioGroup rg1,我想获取所选单选按钮的值。

我知道我可以使用以下方法获取所选单选按钮的id

if(rg1.getCheckedRadioButtonId()!=-1)
int id= rg1.getCheckedRadioButtonId()

这给了我 id ,但我想要那个按钮的值。

【问题讨论】:

    标签: android android-event


    【解决方案1】:

    您需要获取该索引处的单选按钮,然后获取该按钮文本的值。试试下面的代码。

    if(rg1.getCheckedRadioButtonId()!=-1){
        int id= rg1.getCheckedRadioButtonId();
        View radioButton = rg1.findViewById(id);
        int radioId = radioGroup.indexOfChild(radioButton);
        RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
        String selection = (String) btn.getText();
    }
    

    【讨论】:

    • getCheckedRadioButton返回的id是不是只有RadioGroup内的id?为什么不能获取按钮的 id,然后使用更通用的 findViewById 来获取 RadioButton,而不是添加额外的 indexOfChild 和 getChildAt 步骤
    • int radioId = radioGroup.indexOfChild(radioButton); 的 radioGroup 是什么
    • radioGroup 是 RadioGroup 的对象;该组包含所有单选按钮。它是那些单选按钮的父级
    • 所以 rg1 和 radioGroup 在这种情况下是同一个对象,对吧?
    • 或者,如果使用isChecked 方法进行检查,您可以获取带有 ID 的单选按钮并获取其文本。像这样RadioButton radio_male = ((RadioButton) findViewById(R.id.rbtn_male)); String gend = radio_male.isChecked() ? radio_male.getText().toString() : radio_female.getText().toString();
    【解决方案2】:

    试试这个:

    RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
    String radiovalue = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
    

    【讨论】:

    • 谢谢,我已经得到了答案,希望我可以选择多个答案作为接受的答案:(,真的谢谢你,你总是帮助我
    • 谢谢。你应该验证它是否为空,因为它可能会使应用程序崩溃。
    【解决方案3】:
    RadioGroup rg = (RadioGroup)findViewById(R.id.youradio);
    String radiovalue = (RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();  
    

    【讨论】:

      【解决方案4】:

      一行代码

      String buisnesstype = ((RadioButton) rdtranscompany.findViewById(rdtranscompany.getCheckedRadioButtonId())).getText().toString();
      

      【讨论】:

        【解决方案5】:
        rb1=(RadioButton)findViewById(rg1.getCheckedRadioButtonId());
        

        现在您可以使用rb1.getText() 来获取选中的 Radiobutton 上的文本

        【讨论】:

          【解决方案6】:

          我觉得你应该试试这个

          RadioGroup rg=(RadioGroup)findViewById(R.id.youradio);
          String radiovalue=(RadioButton)this.findViewById(rg.getCheckedRadioButtonId())).getText().toString();
          

          【讨论】:

            【解决方案7】:
            RadioGroup bhktype_RadioGr = (RadioGroup)findViewById(R.id.bhkypeRadioGroup);
            int flatTypeId = bhktype_RadioGroup.getCheckedRadioButtonId();
            String flat_type = ((RadioButton) findViewById(flatTypeId)).getText().toString();
            

            【讨论】:

              【解决方案8】:

              简单回答一行

              View v = yourView;  // as a button
              
              String radiovalue = (RadioButton)v).getText().toString();
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-06-26
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-08-13
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多