2

我的活动如下

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;

public class locationserviceSettings extends PreferenceActivity implements OnPreferenceChangeListener,OnSharedPreferenceChangeListener{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.locationservice);

    //    PreferenceManager.setDefaultValues(notificationSettings.this, R.xml.notification, true);
    }



    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        // TODO Auto-generated method stub
        System.out.println("Preference changed " + newValue);
        return false;
    }



    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        // TODO Auto-generated method stub
        System.out.println("Shared preference changed" + key);
    }

}




<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Location service settings">
        <CheckBoxPreference android:title="Location service"
            android:key="locationService" android:defaultValue="true"
            android:summary="checked for start location service in background"></CheckBoxPreference>
    </PreferenceCategory>
    <EditTextPreference android:title="Minimum time seconds"
        android:key="time" android:dependency="locationService"
        android:summary="Miminum time in seocnds for update location"></EditTextPreference>
    <EditTextPreference android:title="Minimum distance meters"
        android:key="distance" android:dependency="locationService"
        android:summary="Miminum distance in meters for update location"></EditTextPreference>
</PreferenceScreen>

当我破解 checkPreference 时,这两种方法都没有打印任何内容

4

2 回答 2

3

使用onPreferenceStartFragment(). onPreferenceTreeClick它成为了已弃用的()的替代品。

于 2011-03-28T11:13:15.007 回答
1

像这样注册你的监听器(例如在 onCreate() 中):

    timePref = getPreferenceScreen().findPreference("time");
    distancePref = getPreferenceScreen().findPreference("distance");


    timePref.setOnPreferenceChangeListener(this);
    distancePref.setOnPreferenceChangeListener(this);
于 2012-02-28T16:43:34.583 回答