【问题标题】:Writing shared preference file for only one radio button under radio group为单选组下的一个单选按钮编写共享首选项文件
【发布时间】:2013-11-29 01:56:08
【问题描述】:

我是 android 开发的新手,试图了解我们如何保存默认首选项。我有以下设置布局文件:

<RadioGroup android:id="@+id/rg" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content">
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/rb1" android:checked="true" 
    android:text="RadioButton1">
    </RadioButton>
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/rb2" android:text="RadioButton2" android:checked="true">
    </RadioButton>
    <RadioButton android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/rb3" android:text="RadioButton3">
    </RadioButton>
</RadioGroup>
    <CheckBox
        android:id="@+id/ch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_ios" />

    <CheckBox
        android:id="@+id/ch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_android"
        android:checked="true" />

    <CheckBox
        android:id="@+id/ch3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/chk_windows" />

我希望当用户第一次打开应用程序并进入设置视图时,默认情况下应该检查 rb1 和 ch1。我在 main.java 文件中编写了以下代码:

PreferenceManager.setDefaultValues(this, R.xml.pref, false);

我的问题是如何为上面创建 pref.xml 文件?

到目前为止我有这个:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">  
  <CheckBoxPreference android:title="Function"
                      android:defaultValue="true"
                      android:summary="ch1 should be default checked"
                      android:key="functionDefault" />
</PreferenceScreen>

xml 文件中复选框的必填字段是什么?还有如何为单选按钮创建 pref.xml?

【问题讨论】:

  • 您是否考虑过以编程方式设置默认首选项值?我的意思是在 Java 代码中?
  • @Pro.metal - 我该怎么做?我在设置视图中设置这些值。但是我想在打开应用程序后立即设置默认值。如果我确实在 java 代码中设置它,我是否必须在设置视图和主视图上复制代码?

标签: android eclipse sharedpreferences


【解决方案1】:

请阅读this链接中的共享偏好。在要使用此共享首选项的活动中,在 onCreate()

之前声明以下行
public static final String PREFS_NAME = "JUST-A-NAME";
SharedPreferences settings;

onCreate() 中使用 Shared Preference 如下-

settings = getSharedPreferences(PREFS_NAME, 0);
String myval = settings.getString("radio_value", "true");

参数中的0指定Preference的Mode。只需浏览 android 开发者网站或 Google 如何使用 Shared Preference。 届时您将知道共享首选项通过键值对起作用,因此在第二行中,“radio_value”是键。

此函数settings.getString(...) 将尝试获取键radio_value 的当前值。如果用户是第一次打开您的应用程序,您可能希望使用默认值,即第二个参数“true”生效时。 因此默认值“true”将在字符串 myval 中,将其与一些 if-else 条件一起使用以选中或取消选中您的单选按钮。如果您需要有关通过 Java 代码切换单选按钮的帮助,请参阅this SO 帖子。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-10
    • 2017-07-17
    • 1970-01-01
    相关资源
    最近更新 更多