【问题标题】:I have 4 radio buttons. I want that my last selected radio button get selected everytime我有 4 个单选按钮。我希望我最后选择的单选按钮每次都被选中
【发布时间】:2018-04-13 10:10:47
【问题描述】:

我有 4 个单选按钮。我希望我最后选择的单选按钮每次都被选中。直到我不改变它。 (当我重新打开我的应用时,发现它已经被选中)

【问题讨论】:

  • 请显示 HTML...不要让我们猜测
  • 带问题分享你的代码
  • 欢迎来到 StackOverflow 社区。如果您对某些非常具体的问题有疑问,您应该包含您的代码。如果您还没有尝试实现某些东西,我强烈建议您更多地搜索您的问题。看起来你想做的事情很常见。 (提示:搜索SharedPreference
  • 将该按钮状态保存在shared preferences 中,然后当您打开应用时根据shared preferences saved state 简单设置其状态。
  • android:checked="true" 将此行添加到您的最后一个(第四个)单选按钮。并且每次都会选择最后一个。如果任何其他单选按钮有此行,请将其从其他单选按钮中删除。我在 android 的上下文中回答,因为你添加了 android 的标签。

标签: android


【解决方案1】:

您可以使用android:checked="true" 保留您希望默认选中的任何单选按钮。在这种情况下,默认情况下会选中第二个按钮。

<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton android:id="@+id/radio_pirates"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pirates"
    android:onClick="onRadioButtonClicked"/>
<RadioButton android:id="@+id/radio_ninjas"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ninjas"
    android:checked="true"
    android:onClick="onRadioButtonClicked"/>

【讨论】:

    猜你喜欢
    • 2016-01-22
    • 2020-03-27
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多