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

时间:2014-11-07 13:16:34

标签: android settings checkboxpreference

我是一名新开发者,我正在尝试在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>

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

1 个答案:

答案 0 :(得分: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" />