我希望有人能夠幫助我瞭解當方向更改時,Android radiogroup和onCheckedChanged回調會發生什麼。方向變化時的radiogroup行爲
我有一個收音機組,有三個單選按鈕。通過將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,我會看到一個回調函數,checkID等於button1的onCheckedChanged方法。
如果選擇了button2,我看不到onCheckedChanged方法的回調。
如果選擇button3,我會看到onCheckedChanged方法的兩個回調。第一個回調的checkID等於button2。第二個回調的checkID等於button3。
我不明白第一個和第三個案例之間的行爲差異。在兩者中,都有一個單選按鈕,而不是默認選中的。
這個問題也引起了我的問題。我找到了解決方案,我的具體情況,並在這裏發佈答案http://stackoverflow.com/questions/22438780/oncheckedchanged-being-called-on-device-rotate – Chris