android - 如何从首选项屏幕打开 AlertDialog?

标签 android preferences preferenceactivity

我的 Activity 如下

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 时,这两种方法都没有打印任何内容

最佳答案

使用onPreferenceStartFragment() 。它取代了已弃用的 onPreferenceTreeClick()。

关于android - 如何从首选项屏幕打开 AlertDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5457944/

相关文章:

Android:从 PreferenceActivity 启动自定义 Preference

java - Eclipse 首选项(org.eclipse.core.runtime.preferences.InstanceScope): unwanted leading slashes

android - 是否有类似于铃声偏好的振动偏好?

android - 如何引用或 "find"一个PreferenceActivity?

android - 从 Firebase 数据库中删除特定值

android - 单击一个时如何继续添加首选项?

android - 不使用xml定义Android动画

android - 从 AsyncTask 填充数组列表

Android:如何以编程方式将键盘旋转 180 度?

android - 打包APK后是否可以编辑Androidmanifest.XML?