2011-10-07 64 views
0

我试图使用给定答案的这个Android: How to get a radiogroup with togglebuttons?代码的孩子为什么我不能访问无线电集团

static final RadioGroup.OnCheckedChangeListener ToggleListener = new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(final RadioGroup radioGroup, final int i) { 
      for (int j = 0; j < radioGroup.getChildCount(); j++) { 
       final ToggleButton view = (ToggleButton) radioGroup.getChildAt(j); 
       view.setChecked(view.getId() == i); 
      } 
     } 
    }; 

符合

最终切换按钮查看= (ToggleButton)radioGroup.getChildAt(j);

它总是崩溃。在Logcat中我没有看到任何消息。

我试过了我能想到的所有东西,但无法解决问题 - 非常感谢!

PS这是我的RadioGroup中的xml:

<RadioGroup android:id="@+id/radioGroup2" android:layout_width="150sp" android:layout_height="wrap_content" 
      android:paddingLeft = "10sp" android:layout_alignBottom="@+id/a2" > 
     <RadioButton android:layout_width="wrap_content" android:id="@+id/Settings_otherSettingsT2Yes" android:layout_height="wrap_content" 
      android:textColor="#000000" android:textSize="18sp" 
     android:text="@string/Settings_otherSettingsT2Yes" android:checked="false"></RadioButton> 
     <RadioButton android:layout_width="wrap_content" android:id="@+id/Settings_otherSettingsT2No" android:layout_height="wrap_content" 
      android:textColor="#000000" android:textSize="18sp" 
     android:text="@string/Settings_otherSettingsT2No"></RadioButton> 
    </RadioGroup> 

......这里没有什么特别

我登录radioGroup.getChildCount()检查的儿童人数,并让2如预期

回答

0

在您将RadioButtons投射到ToggleButton的代码。这很可能是你崩溃的原因。我不明白你为什么在logcat中找不到异常。

但是,你说你想使用链接中的代码,但是你的XML并不靠近链接。您使用RadioButtons链接使用ToggleButtons。

如果你只是想要RadioButtons,然后完全忽视该链接。 如果你想使用ToggleButtons,那么该链接不是最好的方式。将ToggleButtons添加到RadioGroup只是混淆了代码的意图。

+0

非常感谢您的评论 - 事实上,我自己并不确定.. – user387184

+0

顺便说一句,请解释一下这些单选按钮的setOnCheckedChangeListener和setOnClickListener的用法/区别。当我触摸屏幕时,只触发setOnCheckedChangeListener。 setOnClickListener永远不会触发 - 虽然有很多使用此示例的示例..谢谢! – user387184

相关问题