How to open AlertDialog from preference screen?

大憨熊 提交于 2019-12-11 02:52:20

问题


My activity is below

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>

both of these method are not printing anything when i chacked the checkPreference


回答1:


use onPreferenceStartFragment(). It became a replacement for deprecated onPreferenceTreeClick().




回答2:


Register your listener (for example in onCreate()) like this:

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


    timePref.setOnPreferenceChangeListener(this);
    distancePref.setOnPreferenceChangeListener(this);


来源:https://stackoverflow.com/questions/5457944/how-to-open-alertdialog-from-preference-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!