gpt4 book ai didi

android - 单选按钮文本 "Recycled"?

转载 作者:行者123 更新时间:2023-11-30 03:54:36 24 4
gpt4 key购买 nike

我在 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 中来回滚动足够多,其中一个按钮上的文本会变为 Female。所以,最后,我得到了 2 个 Female RadioButtons。

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

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

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

最佳答案

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

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

事实上,第二个 RadioButton 有文本“Female”,这意味着它会覆盖第一个文本“Male”的保存,但由于以下原因,重新创建时两者都会收到相同的文本相同的 ID。

关于android - 单选按钮文本 "Recycled"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13557816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com