-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値が である間、 を使用して概要を設定できますtrueandroid: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 に答える