-1

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

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

这是我的代码:

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>

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

4

1 回答 1

1

我认为,您想更改CheckBoxPreference摘要。您可以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" />
于 2015-01-31T13:33:40.930 回答