【问题标题】:Android: A specific RadioButton belongs to which RadioGroup?Android:特定的 RadioButton 属于哪个 RadioGroup?
【发布时间】:2016-12-26 11:30:02
【问题描述】:

是否可以在 Android 中识别特定 Radio Button 属于哪个 Radio Group?

例如:

RadioGroup_A
     - RadioButton_1A
     - RadioButton_2A

RadioGroup_B
     - RadioButton_1B
     - RadioButton_2B

当我有 RadioButton_1A 时,要确定它属于 RadioGroup_A 吗?

我需要实现一个逻辑来忽略触发 CheckedChange 侦听器的特殊情况。当我知道 RadioButton 属于那个特定的 RadioGroup 时,只需要对一个 RadioGroup 进行限制。

谢谢

【问题讨论】:

  • 请解释更多案例,以便我们了解您想要实现的目标。如果 RadioButton_1A 具有唯一的 id,那么它的明确它将始终属于 RadioGroup_A。 (没有得到你的问题)
  • 嗨,Ramit,是的,所有 RadioGroups 和 RadioButtons 都有 ID(例如:android:id="@+id/RadioGroup_A" / android:id="@+id/RadioButton_1A")
  • 因此,如果您有唯一的 id,您想根据某处的单选按钮来识别单选组,可能会在单选按钮的 onclick 上选中更改。所以 RadioButton_1A 和 RadioButton_2A 可以直接使用 RadioGroup_A。我试图用我知道的信息来解释。其他明智的做法是准确告诉您要在哪里执行此类操作,首选示例代码。

标签: android android-radiogroup android-radiobutton


【解决方案1】:

radioGroup.indexOfChild 方法呢?文档说返回:

一个正整数,表示视图在组中的位置, 如果视图在组中不存在,则为 -1

也许您可以检查您的RadioButton 是否在RadioGroup 中退出:

int idx = radioGroup.indexOfChild(radioButton);

if (idx != -1)
{
   //radioButton is in the radioGroup
}
else {
   //radioButton isnt in the radioGroup
}

希望对你有帮助!

【讨论】:

    【解决方案2】:

    在我的一个应用程序中,我通过两个 RadioButon ArrayLists 来做到这一点,每个组一个。在布局膨胀后的 onCreate() 中,RadioButtons 被添加到每个组中,如this answer 中所述。

    然后,通过检查哪个 ArrayList 包含 RadioButton,就可以实现所需的逻辑。

    【讨论】:

      【解决方案3】:

      我描述了您问题的完整示例,请应用我的解决方案

      public class MyClass extends Activity implements RadioGroup.OnCheckedChangeListener
      {
          RadioGroup rg1,rg2,rg3;
          public void onCreate(Bundle b)
          {
              super.onCreate(b);
              setContentView(R.layout.main);
      
      
              rg1=(RadioGroup)findViewById(R.id.rg1);
              rg2=(RadioGroup)findViewById(R.id.rg2);
              rg3=(RadioGroup)findViewById(R.id.rg3);
      
      
              rg1.setOnCheckedChangeListener(this);
              rg2.setOnCheckedChangeListener(this);
              rg3.setOnCheckedChangeListener(this);
      
          }
      
          @Override
              public void onCheckedChanged(RadioGroup radioGroup, int i) {
      
              if(radioGroup==rg1)
              {
                  //First RadioGroup Clicked
                  RadioButton radio=(RadioButton)findViewById(i);
                  Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
              }
      
              if(radioGroup==rg2)
              {
                  //Second RadioGroup Clicked
                  RadioButton radio=(RadioButton)findViewById(i);
                  Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
              }
      
              if(radioGroup==rg3)
              {
                  //Third RadioGroup Clicked
                  RadioButton radio=(RadioButton)findViewById(i);
                  Toast.makeText(this,radio.getText().toString(),Toast.LENGTH_SHORT).show();
              }
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-06
        • 1970-01-01
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 2018-09-13
        • 1970-01-01
        相关资源
        最近更新 更多