java 如何在 Android 中为 SwitchPreference 设置默认值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12583695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:26:46  来源:igfitidea点击:

How to set default value for SwitchPreference in Android?

javaandroidandroid-preferences

提问by Lidia

Does anyone used SwitchPreferenceclass from Android and knows how to set the default value? I have implemented it programmatically:

有没有人使用SwitchPreference过 Android 的类并且知道如何设置默认值?我已经以编程方式实现了它:

SwitchPreference switch = new SwitchPreference(this);
switch.setKey("preference_my_key");
switch.setTitle(R.string.preference_title_my_title);
switch.setSummary(R.string.preference_summary_my_summary);
Boolean isChecked = Manager.myMethodIsChecked(MyActivity.this);
switch.setChecked( isChecked ); 

switch.setOnPreferenceChangeListener(new OnPreferenceChangeListener()  {                
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
    try {
            boolean selected =   Boolean.parseBoolean(newValue.toString());      
        if ( !selected ) {
            //do something
        }
    } catch (Throwable e) {
       e.printStackTrace();
    }               
   return true;
   }
});         
category.addPreference(switch);

Preferences saves all values into its XML file: app_package_name_preferences.xml. First time when app is loaded, switch has default "false" values. But I need sometimes to make default value "true". I tried few methods,but nothing works.

首选项将所有值保存到其 XML 文件中:app_package_name_preferences.xml. 第一次加载应用程序时,开关具有默认的“ false”值。但有时我需要将默认值设为“ true”。我尝试了几种方法,但没有任何效果。

switch.setChecked( true );  
switch.setDefaultValue(true);

采纳答案by Lidia

As I told, I write preferences programmatically:

正如我所说,我以编程方式编写首选项:

PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory catView = new PreferenceCategory(this);
catView.setTitle(R.string.preference_category_view);
root.addPreference(catView);

final SwitchPreference switchSplash= new SwitchPreference(this);
switchSplash.setKey(PreferenceKeys.SPLASH); 

//-----the above code----
switchSplash.setChecked(false);       // LINE 1
catView.addPreference(switchSplash);  // LINE 2

While debugging I found that truevalue is set in LINE 1, but when I add switchSplashinto catView, the values of switchSplashis reset to false, because catViewsets values from preferences.xml.
That's why I changed all needed values into the XML

在调试时,我发现trueLINE 1 中设置了值,但是当我添加switchSplash到 时catView, 的值switchSplash被重置为false,因为catView设置了来自首选项.xml 的值。
这就是我将所有需要的值更改为 XML 的原因

SharedPreferences.Editor editor = root.getPreferenceManager().getSharedPreferences().edit();
editor.putBoolean(PreferenceKeys.SPLASH, true);  
editor.commit();

回答by kamasuPaul

Incase anyone is still looking for the answer here,you can use this attribute app:defaultvalue="true"on your <Switchpreferencecompat/>in the xml

柜面任何人仍在寻找答案在这里,你可以使用这个属性 app:defaultvalue="true"在你<Switchpreferencecompat/>的XML

回答by erbsman

If you trying to get the boolean out of newValue

如果您试图从 newValue 中获取布尔值

        boolean selected =   Boolean.parseBoolean(newValue.toString());      

your going about this in a strange and I guess incorrect way. If newValue is a Boolean, (check with instanceof) then just cast newValue to be a Boolean.

你以一种奇怪的方式处理这件事,我猜是不正确的方式。如果 newValue 是一个布尔值,(用 instanceof 检查)然后将 newValue 转换为一个布尔值。

        boolean selected =   (Boolean) newValue;

Is that what your trying to achieve?

这就是你想要达到的目标吗?