简体   繁体   中英

Listen preference change and transfer boolean in checkbox to MainActivity

I have 2 file:

control_preferences.xml:`

<PreferenceCategory
    android:title="@string/control_category">


    <CheckBoxPreference
        android:title="Debug Information ON/OFF"
        android:defaultValue="false"
        android:summary="Show debug information when ON"
        android:key="checkboxPref" />

</PreferenceCategory>

`

SettingsActivity.java:

package com.sample;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;



public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {


    public static final String KEY_PREF_DEBUG = "checkboxPref";



    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
                                          String key) {
        if (key.equals(KEY_PREF_DEBUG)) {
            Toast.makeText(getApplicationContext(), "CLICK",
                    Toast.LENGTH_SHORT).show();
        }
    }





    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }


    public static class ControlPrefsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // load the preferences from an XML resource
            addPreferencesFromResource(R.xml.fragmented_control_preferences);
        }
    }}

I have 2 questions:

  1. Why when selecting or deselecting Checkbox Toast doesn't display?
  2. How to store in SharedPreferences boolean from this CheckBox and later read this boolean in MainActivity.

1) Why when selecting or deselecting Checkbox Toast doesn't display?

Add below lines in on resume of your activity

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(prefs);

2)How to store in SharedPreferences boolean from this CheckBox and later read this boolean in MainActivity? Use below code to get value

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.getBoolean("key_value_given_in_preference_xml_file") 

Use this instead of implementing interface

checkboxPref = (CheckBoxPreference) getPreferenceScreen().findPreference(your_key);

checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() 
{

     @Override
     public boolean onPreferenceChange(Preference preference, Object newValue) 
     {

          YOUR CODE                    

          return false;
     }
});

@Jawegiel

I insert this to:

public static class ControlPrefsFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // load the preferences from an XML resource
            addPreferencesFromResource(R.xml.fragmented_control_preferences);

            final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference("checkboxPref");


            checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
            {

                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue)
                {

                    Toast.makeText(getActivity().getApplicationContext(), "CLICK",
                            Toast.LENGTH_SHORT).show();

                    return false;
                }
            });
        }
    }

and it works :)

Thank you both :)

@Jawegiel

what if I would use Toast like "Debug: " + "boolean from this actually changed checkbox" In MainActivity i read this boolean through code:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplication‌​Context());
 boolean check = prefs.getBoolean("checkboxPref",false);

How read this boolean in in SettingsActivity same like in MainActivity?

I try through this code:

checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
            {

                @Override
                public boolean onPreferenceChange(Preference preference, Object newValue)
                {
                    boolean check = checkboxPref.getSharedPreferences().getBoolean("checkboxPref",false);
                    Toast.makeText(getActivity().getApplicationContext(), "Debug " + check,
                            Toast.LENGTH_SHORT).show();

                    return false;
                }
            });

But now I can't unclick Checkbox or click if false :d

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM