0

正如您从我的 xml 和类中看到的那样,我正在处理我的设置。当第一个切换按钮设置为 false 时,我的 onPreferenceChange 没有将第二个和第三个切换按钮设置为 false。

日志猫

07-23 16:10:42.879: I/PROJECTCARUSO(11579): preference: Cervical Mucus Are you observing MucusnewValue: true
07-23 16:10:42.879: I/PROJECTCARUSO(11579): True
07-23 16:10:44.601: I/PROJECTCARUSO(11579): preference: Cervical Mucus Are you observing MucusnewValue: false
07-23 16:10:44.611: I/PROJECTCARUSO(11579): True

班级:

public class UserSettingActivity extends PreferenceActivity implements OnPreferenceChangeListener{


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * Populate the activity with the top-level headers.
     */
    @Override
    public void onBuildHeaders(List<Header> target) {
        Log.i("PROJECTCARUSO","onBuildHeaders");
        loadHeadersFromResource(R.xml.preference_headers, target);

    }

    /**
     * This fragment shows the preferences for the first header.
     */
    public static class Prefs1Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.notification_settings);
        }
    }

    /**
     * This fragment shows the preferences for the second header.
     */
    public static class Prefs2Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.charting_settings);

            findPreference("cervical_mucus").setOnPreferenceChangeListener(
                    new OnPreferenceChangeListener() {

                           @Override
                           public boolean onPreferenceChange(Preference preference, Object newValue) {
                               Log.i("PROJECTCARUSO","preference: "+ preference + "newValue: " + newValue);
                               SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

                               // string test w/o "==" usage.
                               if ("false".equals(newValue)) {
                                   prefs.edit().putBoolean("mucus_stamps", false).commit();
                                   prefs.edit().putBoolean("fertile_infertil", false).commit();
                               } else {
                                   prefs.edit().putBoolean("mucus_stamps", true).commit();
                                   prefs.edit().putBoolean("fertile_infertil", true).commit();
                               }

                               // remove all and reload
                               getPreferenceScreen().removeAll();

                               //NAMEOFXML is the same you have in your fragement's oncreate
                               addPreferencesFromResource(R.xml.preference_headers);

                               // true instead of false so the new value gets kept
                               return true;
                           };
                    });
        }
    }

    protected void onPause()
    {
        super.onPause();
    }

    protected void onResume()
    {
        super.onResume();
    }

    protected void onStop() {
        super.onStop();
        finish();
    }

    @Override
    public boolean onPreferenceChange(Preference arg0, Object arg1) {
        // TODO Auto-generated method stub
        return true;
    }


}

XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory 
         android:title="@string/pref_chart_profile" 
         android:textSize="20px">

        <SwitchPreference  
                android:title="@+string/pref_symptothermal"
                android:summary="@+string/pref_symptothermal_summary"
                android:key="symptothermal"
                android:defaultValue="true"
                android:layout="@layout/pref_layout"/>

        <SwitchPreference 
                android:id="@+id/cervical_mucus"
                android:title="@+string/pref_cervical_mucus"
                android:summary="@+string/pref_cervical_mucus_summary"
                android:key="cervical_mucus"
                android:defaultValue="true"
                android:layout="@layout/pref_layout" />    

        <SwitchPreference 
                android:id="@+id/mucus_stamps"
                android:title="@+string/pref_mucus_stamps"
                android:summary="@+string/pref_mucus_stamps_summary"
                android:key="mucus_stamps"
                android:defaultValue="true"
                android:layout="@layout/pref_layout" />

        <SwitchPreference 
                android:id="@+id/fertile_infertil"
                android:title="@+string/pref_fertile_infertile"
                android:summary="@+string/pref_fertile_infertile_summary"
                android:key="fertile_infertil" 
                android:defaultValue="true"
                android:layout="@layout/pref_layout" />
    </PreferenceCategory>

</PreferenceScreen>
4

2 回答 2

1
        findPreference("cervical_mucus").setOnPreferenceChangeListener(
             new OnPreferenceChangeListener() {

                    @Override
                    public boolean onPreferenceChange(Preference preference, Object newValue) {
                        Log.i("PROJECTCARUSO","preference: "+ preference + "newValue: " + newValue);
                        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

                        // string test w/o "==" usage.
                        if ("false".equals(newValue)) {
                            prefs.edit().putBoolean("mucus_stamps", false).commit();
                            prefs.edit().putBoolean("fertile_infertil", false).commit();
                        } else {
                            prefs.edit().putBoolean("mucus_stamps", true).commit();
                            prefs.edit().putBoolean("fertile_infertil", true).commit();
                        }

                        // remove all and reload
                        getPreferenceScreen().removeAll();
                        //NAMEOFXML is the same you have in your fragement's oncreate
                        addPreferencesFromResource(R.xml.NAMEOFXML);

                        // true instead of false so the new value gets kept
                        return true;
                    };
             });
于 2013-07-23T20:45:53.227 回答
1

行。

在你的类下面定义 SwitchPreferences,如下所示:

  SwitchPreference mucus_stamps;
  SwitchPreference cervical_mucus;

然后给开关一个视图:cervical_mucus = (SwitchPreference) findPreference("cervical_mucus"); mucus_stamps = (SwitchPreference) findPreference("mucus_stamps");

现在设置 onchange 监听器:

mucus_stamps.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            // TODO Auto-generated method stub

            mucus_stamps.setChecked(true);
            cervical_mucus.setChecked(false);
                            //here you set the others to false.


            return true;
        }

    });
于 2013-07-23T22:10:36.367 回答