1

我有一个复选框首选项,我想从另一个复选框设置它的值。现在我可以从复选框首选项中获取值并将其设置为外部复选框,但我不知道如何从该外部复选框中设置首选项值。请帮助这是我的代码

 getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());            musicpref = getPrefs.getBoolean("musicp", true);    
        CheckBox musiccheck = (CheckBox) findViewById(R.id.checkBox1); 
       //The external Checkbox, is checked will start service , if not will stop it
                musiccheck.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView,
                            boolean isChecked) {
                        if (isChecked) {

                            Intent musicstart = new Intent(MainActivity.this,
                                    ServiceMusic.class);
                            startService(musicstart);
                        } else {
                            Intent musicstart = new Intent(MainActivity.this,
                                    ServiceMusic.class);
                            stopService(musicstart);
                        }

                    }
                });


                // Preference checkbox , if is true will set the external checkbox to true 
                if (musicpref == true) {
                    musiccheck.setChecked(true);
                }else{
                    musiccheck.setChecked(false);
                }
4

1 回答 1

4

你可以尝试这样的事情:

SharedPreferences preferendecs = PreferenceManager.getDefaultSharedPreferences(getContext());
Editor editor = preferencess.edit();
editor.putBoolean(PREF_NAME, musiccheck.getValue());
editor.commit();

你也可以参考这个:

http://android-coding.blogspot.in/2011/08/example-of-using-checkboxpreference.html

如何在 android 开发中捕获 CheckBoxPreference 的变化?

于 2013-02-24T05:14:16.737 回答