Writing shared preference file for only one radio

2019-08-31 00:20发布

问题:

I am new to android development trying to understand how we save default preferences. 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. I wrote following code in main.java file:

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

My question is how to create pref.xml file for above?

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? Also how to create pref.xml for radio button?

回答1:

Please read Shared Preferences from this link. Inside the activity where you want to use this Shared Preference, declare the following lines before onCreate()

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

Inside onCreate() use the Shared Preference as following-

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

The 0 in the parameter specifies the Mode of the Preference. Just go through the android developer website or Google how to use Shared Preference. You will know by then that Shared Preferences work by key-value pairs, so in the 2nd line, "radio_value" is the key.

This function settings.getString(...) will try to get the current value of the key 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. So the default value "true" will be inside the string myval, use it with some if-else condition to check-uncheck your radio buttons. See this SO post in case you need help with toggling radio buttons by Java code.

Hope it helps.