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