Android 方向改变时的放射组行为

Android 方向改变时的放射组行为,android,radio-button,screen-orientation,Android,Radio Button,Screen Orientation,我希望有人能帮助我理解当方向改变时,Android radiogroup和onCheckedChanged回调发生了什么 我有一个有三个单选按钮的收音机组。通过将checked属性设置为true,第二个按钮被定义为默认按钮。我的无线组xml如下所示: <RadioGroup android:id="@+id/rgReportRange" android:layout_width="wrap_content" android:layou

我希望有人能帮助我理解当方向改变时,Android radiogroup和onCheckedChanged回调发生了什么

我有一个有三个单选按钮的收音机组。通过将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,我会看到一个对onCheckedChanged方法的回调,checkedID等于button1

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

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


我不明白第一个和第三个案例在行为上的区别。在这两个选项中,都有一个非默认选择的单选按钮。

打开manifest.xml文件并在活动标记中修改:



检查结果后::)

对xml文件进行如下更改:

         <activity android:name=".YourActivityName"
        android:configChanges="orientation|screenSize" />


谢谢你的建议。这确实避免了这个问题。但我仍然很好奇为什么它会这样,这个问题也给我带来了麻烦。我找到了针对我的具体情况的解决方案,并在这里发布了答案欢迎使用堆栈溢出。虽然此代码可能会回答该问题,但提供有关此代码为什么和/或如何回答该问题的附加上下文可提高其长期价值。
         <activity android:name=".YourActivityName"
        android:configChanges="orientation|screenSize" />