我希望有人能帮助我了解 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。
我不明白第一种情况和第三种情况之间的行为差异。在两者中,都有一个单选按钮而不是默认选中的。
最佳答案
对 xml 文件进行如下更改:
<activity android:name=".YourActivityName"
android:configChanges="orientation|screenSize" />
关于android - 方向改变时的 radio 组行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10647001/