radiogroup behavior on orientation change

自作多情 提交于 2019-12-23 13:26:14

问题


I am hoping that someone can help me understand what's going on with respect to an Android radiogroup and the onCheckedChanged callbacks when the orientation changes.

I have a radio group with three radio buttons. The second button is defined as the default by setting the checked attribute to true. My xml for the radio group is as follows:

    <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>

The RadioGroup has an onCheckedChangedListener. When the orientation changes, the onCheckedChangedListener is being called back differently based on which button is selected before the orientation change.

If button1 is selected, I see one callback to the onCheckedChanged method with checkedID equal to button1.

If button2 is selected, I see no callback to the onCheckedChanged method.

If button3 is selected, I see two callbacks to the onCheckedChanged method. The first callback has checkedID equal to button2. The second callback has checkedID equal to button3.

I don't understand the difference in behavior between the first and third cases. In both, there is a radio button other than the default selected.


回答1:


Make changes to the xml file like :

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



回答2:


open your manifest.xml file and modify in Activity<> tag :

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

after this check your result : :)



来源:https://stackoverflow.com/questions/10647001/radiogroup-behavior-on-orientation-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!