在单选组编写的共享preference文件只有一个单选按钮 [英] Writing shared preference file for only one radio button under radio group

查看:666
本文介绍了在单选组编写的共享preference文件只有一个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想,当用户第一次打开应用程序,并进入设置查看RB1和CH1应在默认情况下进行检查。我写了main.java文件以下code:

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);

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

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

我有这个至今:

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

What are required fields for checkbox in the xml file? Also how to create pref.xml for radio button?

推荐答案

请从的共享preferences =htt​​p://developer.android.com/guide/topics /data/data-storage.html相对=nofollow>此链接。要使用此共享preference活动里面,申报前的的onCreate()

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;

的onCreate()使用共享preference如下 -

Inside onCreate() use the Shared Preference as following-

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

0 的参数指定preference的模式。刚去通过Android开发者网站或谷歌如何使用共享preference。
你会被后来才知道,共享preferences通过键 - 值对的工作,所以在第2行,radio_value是关键。

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.

此功能的 settings.getString(...)会尽力拿到钥匙的 radio_value 的当前值。如果用户打开您的应用是第一次,你可能需要使用默认值,这时候第二个参数真来付诸行动。
所以默认值为true,将字符串里面设为myVal,用它与一些if-else条件检查,取消你的单选按钮。请参见,以便在情况后您需要在Java $ C $切换单选按钮帮助角

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.

希望它帮助。

这篇关于在单选组编写的共享preference文件只有一个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆