Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用单个复选框创建首选项,但不扩展首选项活动_Android_Eclipse_Adt - Fatal编程技术网

Android 使用单个复选框创建首选项,但不扩展首选项活动

Android 使用单个复选框创建首选项,但不扩展首选项活动,android,eclipse,adt,Android,Eclipse,Adt,在我的应用程序中,我只使用了一个复选框的一个基本设置,我希望它能像扩展preferenceactivity时的preferences一样保持不变,除非不这样做。我能找到的所有偏好示例都扩展了preferenceactivity 是否可以在主UI中仅使用一个基本复选框并使用其逻辑来实现首选项功能?请提供简短示例。您可以使用手动保存首选项。更改复选框后,您可以保存/加载设置 CheckBox checkBox = ( CheckBox ) findViewById( R.id.checkbox );

在我的应用程序中,我只使用了一个复选框的一个基本设置,我希望它能像扩展preferenceactivity时的preferences一样保持不变,除非不这样做。我能找到的所有偏好示例都扩展了preferenceactivity


是否可以在主UI中仅使用一个基本复选框并使用其逻辑来实现首选项功能?请提供简短示例。

您可以使用手动保存首选项。更改复选框后,您可以保存/加载设置

CheckBox checkBox = ( CheckBox ) findViewById( R.id.checkbox );
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            // get the preference manager
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

            // get the editor
            SharedPreferences.Editor editor = prefs.edit();

            // put the new setting
            editor.putBoolean(PREF_NAME, true);

            // IMPORTANT - save the new settings
            editor.commit();

         }  
      }
    }
});
然后,您可以在您喜欢使用的任何位置检索您的设置

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
if (prefs.getBoolean(PREF_NAME, false)) {
   // setting dependent code goes here
}

希望有帮助:)

您可以在任何活动中访问共享首选项

SharedPreferences preferences = getSharedPreferences( NameAsString, Context.MODE_PRIVATE );
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean( keyAsString, value );
editor.apply();

请注意,editor.apply()是异步的,仅在GB及以上版本中可用。对于低于android 2.3的版本,请使用editor.commit()

Argh,直到我发布了我的答案,Argh才看到您的答案。哎呀。哈哈
CheckBox checkBox = (CheckBox) findViewById(R.id.checkbox1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean("checkbox_key", isChecked);
        editor.commit();
    }
});