RadioButton Text“Recycled”?

时间:2012-11-26 02:19:56

标签: android xml radio-button android-viewpager

我在xml中使用以下代码生成两个RadioButtons,它们之间有一个分隔符:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:paddingLeft="60dp"
            android:text="Male" />

        <View
            android:id="@+id/firstDivider"
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="40dp"
            android:background="#939393" />

          <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:paddingLeft="60dp"
            android:text="Female" />

    </LinearLayout>

这很好用,并且正在ViewPager中使用(在其中一个标签中)。

有趣的是,如果我在ViewPager中来回滚动,其中一个按钮上的文字会变为女性。所以,最后,我得到2个女性RadioButtons。

这看起来与View Recycling有关,但由于它们是2个明显硬编码的按钮(例如,而不是ListView项目),我不确定它为什么会发生。

当然,我可以在java中添加button.setText(...)希望能解决它),但我刚注意到这一点,我没有解释它。

编辑:正如预期的那样,当我强制关闭并重新启动应用程序时,它会切换回正常状态 - 我必须重复此过程才能获得两个Female按钮。如果我切换到另一个活动/ etc / etc,它仍然会停留在两个“女性”按钮上。

1 个答案:

答案 0 :(得分:1)

RadioButtons的资源ID均为android:id="@+id/radioButton1"

Android会自动保存UI元素(小部件)的“状态”,如果在布局文件中创建并且它们有ID。

第二个RadioButton文本为“女性”的事实意味着它会覆盖第一个文本“男性”的保存,但是由于相同的id,两个文本在重新创建时都会收到相同的文本

相关问题