Android应用程序中设置页面的实现

Android应用程序中设置页面的实现,android,settings,checkboxpreference,Android,Settings,Checkboxpreference,我是一名新开发人员,我正试图在Android中实现一个设置页面,但运气不太好 我正在使用CheckBoxPreference,当我选中/取消选中CheckBoxPreference时,我正在尝试更改一些SharedReferences 这是我的密码: package com.entu.bocterapp; //imports public class Settings extends PreferenceActivity implements SharedPreferences.OnShar

我是一名新开发人员,我正试图在Android中实现一个设置页面,但运气不太好

我正在使用CheckBoxPreference,当我选中/取消选中CheckBoxPreference时,我正在尝试更改一些SharedReferences

这是我的密码:

package com.entu.bocterapp;

//imports

public class Settings extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    CheckBoxPreference pushNotificationSetting;
    CheckBoxPreference locationSupportSetting;


    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings_design);
        initializeActionBar();
   }

    public void initializeActionBar(){
        ActionBar actionBar = getActionBar();
        actionBar.setBackgroundDrawable(new ColorDrawable(0xff50aaf1));

        getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getActionBar().setCustomView(R.layout.action_bar_center_image);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        if (key.matches("PUSH_NOTIFICATION_PREFERENCE")) {
            if (getPushNotificationsPreference().equals("true")) {
                setPushNotificationsPreference("false");
            } else {
                if (getPushNotificationsPreference().equals("false")) {
                    setPushNotificationsPreference("true");
                }
            }
           Toast.makeText(this, getPushNotificationsPreference(), Toast.LENGTH_SHORT).show();
        }

        if (key.matches("LOCATION_SUPPORT_PREFERENCE")) {
            if (getLocationSupportPreference().equals("true")) {
                setLocationSupportPreference("false");
            } else {
                if (getLocationSupportPreference().equals("false")) {
                    setLocationSupportPreference("true");
                }
            }
        }
        Toast.makeText(this, getLocationSupportPreference(), Toast.LENGTH_SHORT).show();
    }

    public void setPushNotificationsPreference(String value) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(getString(R.string.push_notification_preference), value);
        editor.commit();
    }

    public String getPushNotificationsPreference() {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        return sharedPref.getString(getString(R.string.push_notification_preference), "true");

    }

    public void setLocationSupportPreference(String value) {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(getString(R.string.location_support_preference), value);
        editor.commit();
    }

    public String getLocationSupportPreference() {
        SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
        return sharedPref.getString(getString(R.string.location_support_preference), "true");

    }
}
以下是我的XML:

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

    <PreferenceCategory
        android:title="BocterApp Settings">

        <CheckBoxPreference
            android:key="PUSH_NOTIFICATION_PREFERENCE"
            android:title="Push Notifications"
            android:summary="Uncheck this if you don't want to receive push notifications for alerts in your area (2km radius)."
            android:defaultValue="true"/>


        <CheckBoxPreference
            android:key="LOCATION_SUPPORT_PREFERENCE"
            android:title="Location Support"
            android:summary="Filters locations that are more than 15 km away. Uncheck this if you want to see all alerts."
            android:defaultValue="true"/>


    </PreferenceCategory>


</PreferenceScreen>


您能告诉我我做错了什么/如何正确实施吗?

我想,您想更改
复选框首选项
摘要。您可以使用
android:summaryOn
在其值为
true
时设置摘要。并使用
android:summaryOff
在其值为
false
时设置摘要。看起来是这样的:

<CheckBoxPreference
    android:key="LOCATION_SUPPORT_PREFERENCE"
    android:title="Location Support"
    android:summaryOn="Uncheck this if you don't want to receive push notifications for alerts in your area (2km radius)."
    android:summaryOff="Filters locations that are more than 15 km away. Uncheck this if you want to see all alerts."
    android:defaultValue="true" />


您的错误是什么?不是错误。根本不起作用。