简体   繁体   English

只为单选组下的一个单选按钮编写共享首选项文件

[英]Writing shared preference file for only one radio button under radio group

I am new to android development trying to understand how we save default preferences. 我是android开发的新手,试图了解我们如何保存默认首选项。 I have following settings layout file: 我有以下设置布局文件:

<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" />

I want that when user first opens the app and goes to settings view rb1 and ch1 should be checked by default. 我希望当用户首次打开应用程序并进入设置视图时,默认情况下应检查rb1和ch1。 I wrote following code in main.java file: 我在main.java文件中编写了以下代码:

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

My question is how to create pref.xml file for above? 我的问题是如何为上面创建pref.xml文件?

I have this so far: 到目前为止,我有:

<?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>

What are required fields for checkbox in the xml file? xml文件中复选框的必填字段是什么? Also how to create pref.xml for radio button? 还有如何为单选按钮创建pref.xml?

Please read Shared Preferences from this link. 请读取共享偏好 链接。 Inside the activity where you want to use this Shared Preference, declare the following lines before onCreate() 在要使用此共享首选项的活动中,在onCreate()之前声明以下几行

public static final String PREFS_NAME = "JUST-A-NAME";
SharedPreferences settings;

Inside onCreate() use the Shared Preference as following- onCreate()内,使用共享首选项,如下所示:

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

The 0 in the parameter specifies the Mode of the Preference. 参数中的0指定首选项模式。 Just go through the android developer website or Google how to use Shared Preference. 只需浏览android开发者网站或Google如何使用“共享首选项”即可。 You will know by then that Shared Preferences work by key-value pairs, so in the 2nd line, "radio_value" is the key. 届时您将知道共享首选项按键值对起作用,因此在第二行中,“ radio_value”是键。

This function settings.getString(...) will try to get the current value of the key radio_value . 此功能settings.getString(...)将尝试获取键radio_value的当前值。 If the user is opening your app for the first time, you may want to use a default value, that's when the second parameter "true" comes into action. 如果用户是第一次打开您的应用,则可能要使用默认值,这就是第二个参数“ true”生效的时间。 So the default value "true" will be inside the string myval, use it with some if-else condition to check-uncheck your radio buttons. 因此,默认值“ true”将在字符串myval内,将其与某些if-else条件一起使用,以选中-取消选中您的单选按钮。 See this SO post in case you need help with toggling radio buttons by Java code. 请参见 SO张贴在您需要通过Java代码切换单选按钮帮助的情况下。

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM