2012-05-18 29 views
3

我希望有人能够帮助我了解当方向更改时,Android radiogroup和onCheckedChanged回调会发生什么。方向变化时的radiogroup行为

我有一个收音机组,有三个单选按钮。通过将checked属性设置为true,将第二个按钮定义为默认值。我的收音机组的xml如下:

<RadioGroup 
     android:id="@+id/rgReportRange" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/One" /> 

     <RadioButton 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:checked="true" 
      android:text="@string/Two" /> 

     <RadioButton 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/Three" /> 
    </RadioGroup> 

RadioGroup有一个onCheckedChangedListener。当方向改变时,onCheckedChangedListener将根据在方向改变之前选择哪个按钮而被不同地调回。

如果选择了button1,我会看到一个回调函数,checkID等于button1的onCheckedChanged方法。

如果选择了button2,我看不到onCheckedChanged方法的回调。

如果选择button3,我会看到onCheckedChanged方法的两个回调。第一个回调的checkID等于button2。第二个回调的checkID等于button3。

我不明白第一个和第三个案例之间的行为差​​异。在两者中,都有一个单选按钮,而不是默认选中的。

+0

这个问题也引起了我的问题。我找到了解决方案,我的具体情况,并在这里发布答案http://stackoverflow.com/questions/22438780/oncheckedchanged-being-called-on-device-rotate – Chris

回答

-1

打开manifest.xml文件和活动修改<>标签:

<activity 
      android:configChanges="keyboardHidden|orientation"></activity> 

在此之后检查结果::)

+0

感谢您的建议。这确实避免了这个问题。但我仍然好奇它为什么会如此行事。 –