监听首选项更改并将复选框中的布尔值传递给MainActivity

时间:2017-12-09 14:59:20

标签: android android-sharedpreferences

我有2个文件:

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

我有两个问题:

  1. 为什么选择或取消选中Checkbox Toast时不会显示?
  2. 如何在此CheckBox中存储SharedPreferences布尔值,稍后在MainActivity中读取此布尔值。

4 个答案:

答案 0 :(得分:1)

1)选择或取消选中Checkbox Toast时为什么不显示?

在活动的简历中添加以下行

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

2)如何在此CheckBox中存储SharedPreferences布尔值,然后在MainActivity中读取此布尔值? 使用以下代码获取值

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

答案 1 :(得分:1)

使用此代替实现接口

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

checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() 
{

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

          YOUR CODE                    

          return false;
     }
});

答案 2 :(得分:0)

@Jawegiel

我将其插入:

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

它有效:)

谢谢你们两个:)

答案 3 :(得分:0)

@Jawegiel

如果我会像Toast那样使用Toast怎么办?+“这个实际更改的复选框中的布尔值”在MainActivity中我通过代码读取了这个布尔值:

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

如何在SettingsActivity中读取此布尔值,就像在MainActivity中一样?

我尝试了这段代码:

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

但现在我无法取消选中Checkbox或点击false:d