android - 方向改变时的 radio 组行为

标签 android radio-button screen-orientation

我希望有人能帮助我了解 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/

相关文章:

JavaFX RadioButton 无法正常工作?

javascript - 单选按钮不会选中/取消选中

Android如何防止特定对话框 fragment 的屏幕旋转

ios - ionic 屏幕方向 Cordova 插件在 iOS 环境中未锁定为纵向模式

android - 命名空间前缀 “app” 找不到声明

android - 从 Firestore 获取数据

android - Adview 内容与我的应用主题不匹配

Swift - 在 tableView 中选择单个 radioButton

iOS+SpriteKit : How to force portrait mode?

java - Android gcm 通知无法打开应用程序