`

PreferenceFragment

 
阅读更多

public class SettingPrefsFragment extends PreferenceFragment implements OnPreferenceChangeListener,
        OnPreferenceClickListener {
    private CheckBoxPreference mAutoUpdate;
    private ListPreference mUpdateFrequency;
    private CheckBoxPreference mAutoLocate;
    private Preference mProductinfo;
    /*private CheckBoxPreference mSoundSetting;
    private CheckBoxPreference mLockscreenSetting;*/
   
    String mSummary;
    String mUpdateFreValue;
    String[] mUpdateFreArray;
    String[] mUpdateFreEntry;
   
    SimpleSharedPreference mSharedPreference;
    WeatherPresStorage mPresStorage;
    private PendingIntent mPendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.setting_preference);

        //mSharedPreference = new SimpleSharedPreference();
        mPresStorage = new WeatherPresStorage(getActivity());
        mSharedPreference = mPresStorage.getWeatherPreference();

        mUpdateFreValue = mSharedPreference.getUpdate_frequency();
        mUpdateFreArray = getResources().getStringArray(R.array.auto_update_frequency_value);
        mUpdateFreEntry = getResources().getStringArray(R.array.auto_update_frequency_entry);

        mAutoUpdate = (CheckBoxPreference) findPreference(SimpleSharedPreference.AUTO_UPDATE);
        mUpdateFrequency = (ListPreference) findPreference(SimpleSharedPreference.UPDATE_FREQUENCY);
        mAutoLocate = (CheckBoxPreference) findPreference(SimpleSharedPreference.AUTO_LOCATE);
        mProductinfo = (Preference) findPreference(SimpleSharedPreference.PRODUCT_INFO);
        /*mSoundSetting = (CheckBoxPreference) findPreference(SimpleSharedPreference.SOUND_SETTING);
        mLockscreenSetting = (CheckBoxPreference) findPreference(SimpleSharedPreference.LOCKSCREEN_SETTING);*/
       
        mAutoUpdate.setChecked(mSharedPreference.isAuto_update());
        mUpdateFrequency.setEnabled(mSharedPreference.isAuto_update());
        mAutoLocate.setChecked(mSharedPreference.isAuto_locate());
       
        mAutoUpdate.setOnPreferenceClickListener(this);
        mAutoUpdate.setOnPreferenceChangeListener(this);
        mUpdateFrequency.setOnPreferenceClickListener(this);
        mUpdateFrequency.setOnPreferenceChangeListener(this);
        mAutoLocate.setOnPreferenceClickListener(this);
        mAutoLocate.setOnPreferenceChangeListener(this);
        mProductinfo.setOnPreferenceClickListener(this);
        /*mSoundSetting.setOnPreferenceClickListener(this);
        mSoundSetting.setOnPreferenceChangeListener(this);
        mLockscreenSetting.setOnPreferenceClickListener(this);
        mLockscreenSetting.setOnPreferenceChangeListener(this);*/      
    }

    @Override
    public void onResume() {
        for (int i = 0; i < mUpdateFreArray.length; i++) {
            if (mUpdateFreValue.equals(mUpdateFreArray[i])) {
                mUpdateFrequency.setValueIndex(i);
                mSummary = mUpdateFreEntry[i];
            }
        }
        mUpdateFrequency.setSummary(String.format(
                getString(R.string.auto_update_frequency_summary), mSummary));
        super.onResume();
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference.getKey().equals(SimpleSharedPreference.PRODUCT_INFO)) {
            Log.d("xcl00","preference.getKey().equals(SimpleSharedPreference.PRODUCT_INFO)");
            new AlertDialog.Builder(getActivity())
            .setMessage(R.string.product_infomation)
            .setTitle(R.string.productinfo)
            .setNegativeButton(R.string.button_ok, null)
            .create()
            .show();
        }
        return true;
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference.getKey().equals(SimpleSharedPreference.AUTO_UPDATE)) {
            boolean value = (Boolean) newValue;
            mSharedPreference.setAuto_update(value);
            mPresStorage.saveWeatherPreference(mSharedPreference);
            if (value == false) {
                mUpdateFrequency.setEnabled(false);
                if (mPendingIntent!=null){
                    mPendingIntent.cancel();
                }
            } else {
                mUpdateFrequency.setEnabled(true);
                setAlarm();
            }
        } else if (preference.getKey().equals(SimpleSharedPreference.UPDATE_FREQUENCY)) {
            mSharedPreference.setUpdate_frequency((String) newValue);
            mPresStorage.saveWeatherPreference(mSharedPreference);
            for (int i = 0; i < mUpdateFreArray.length; i++) {
                mUpdateFreValue = (String) newValue;
                if (mUpdateFreValue.equals(mUpdateFreArray[i])) {
                    mUpdateFrequency.setValueIndex(i);
                    mSummary = mUpdateFreEntry[i];
                }
            }
            mUpdateFrequency.setSummary(String.format(
                    getString(R.string.auto_update_frequency_summary), mSummary));
            setAlarm();
        } else if (preference.getKey().equals(SimpleSharedPreference.AUTO_LOCATE)) {
            mSharedPreference.setAuto_locate((Boolean) newValue);
            mPresStorage.saveWeatherPreference(mSharedPreference);
        } /*else if (preference.getKey().equals(SimpleSharedPreference.SOUND_SETTING)) {
            mSharedPreference.setSound_setting((Boolean) newValue);
        } else if (preference.getKey().equals(SimpleSharedPreference.LOCKSCREEN_SETTING)) {
            mSharedPreference.setLockscreen_setting((Boolean) newValue);
        }*/else {
            return false;
        }
        return true;
    }
   
    private void setAlarm(){
        if (mPendingIntent!=null){
            mPendingIntent.cancel();
        }
        String mTimeString = mPresStorage.getWeatherPreference().getUpdate_frequency();
        long mTimeLong = System.currentTimeMillis() + Long.parseLong(mTimeString) * 3600
                * 1000;
        AlarmManager mManager = (AlarmManager) getActivity().getSystemService(
                Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(DataService.AUTO_UPDATE_ACTION);
        mPendingIntent = PendingIntent.getBroadcast(getActivity(), 0,
                alarmIntent, 1);
        mManager.set(AlarmManager.RTC_WAKEUP, mTimeLong, mPendingIntent);
    }
}

<PreferenceScreen

        
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>


    
<PreferenceCategory

            
android:title
=
"@string/inline_preferences"
>


        
<CheckBoxPreference

                
android:key
=
"checkbox_preference"

                
android:title
=
"@string/title_checkbox_preference"

                
android:summary
=
"@string/summary_checkbox_preference"
 
/>


    
</PreferenceCategory>


    
<PreferenceCategory

            
android:title
=
"@string/dialog_based_preferences"
>


        
<EditTextPreference

                
android:key
=
"edittext_preference"

                
android:title
=
"@string/title_edittext_preference"

                
android:summary
=
"@string/summary_edittext_preference"

                
android:dialogTitle
=
"@string/dialog_title_edittext_preference"
 
/>


        
<ListPreference

                
android:key
=
"list_preference"

                
android:title
=
"@string/title_list_preference"

                
android:summary
=
"@string/summary_list_preference"

                
android:entries
=
"@array/entries_list_preference"

                
android:entryValues
=
"@array/entryvalues_list_preference"

                
android:dialogTitle
=
"@string/dialog_title_list_preference"
 
/>


    
</PreferenceCategory>


    
<PreferenceCategory

            
android:title
=
"@string/launch_preferences"
>


        
<!-- This PreferenceScreen tag serves as a screen break (similar to page break
             in word processing). Like for other preference types, we assign a key
             here so it is able to save and restore its instance state. -->

        
<PreferenceScreen

                
android:key
=
"screen_preference"

                
android:title
=
"@string/title_screen_preference"

                
android:summary
=
"@string/summary_screen_preference"
>


            
<!-- You can place more preferences here that will be shown on the next screen. -->


            
<CheckBoxPreference

                    
android:key
=
"next_screen_checkbox_preference"

                    
android:title
=
"@string/title_next_screen_toggle_preference"

                    
android:summary
=
"@string/summary_next_screen_toggle_preference"
 
/>


        
</PreferenceScreen>


        
<PreferenceScreen

                
android:title
=
"@string/title_intent_preference"

                
android:summary
=
"@string/summary_intent_preference"
>


            
<intent
 
android:action
=
"android.intent.action.VIEW"

                    
android:data
=
"http://www.android.com"
 
/>


        
</PreferenceScreen>


    
</PreferenceCategory>


    
<PreferenceCategory

            
android:title
=
"@string/preference_attributes"
>


        
<CheckBoxPreference

                
android:key
=
"parent_checkbox_preference"

                
android:title
=
"@string/title_parent_preference"

                
android:summary
=
"@string/summary_parent_preference"
 
/>


        
<!-- The visual style of a child is defined by this styled theme attribute. -->

        
<CheckBoxPreference

                
android:key
=
"child_checkbox_preference"

                
android:dependency
=
"parent_checkbox_preference"

                
android:layout
=
"?android:attr/preferenceLayoutChild"

                
android:title
=
"@string/title_child_preference"

                
android:summary
=
"@string/summary_child_preference"
 
/>


    
</PreferenceCategory>



</PreferenceScreen>

 

分享到:
评论

相关推荐

    android-support-v4-preferencefragment-master

    在Android开发中,`android-support-v4-preferencefragment`是一个重要的库,主要目的是为了支持在API级别低于21(Lollipop)的设备上使用`PreferenceFragment`。`PreferenceFragment`是Android系统提供的一种用于...

    Android PreferenceFragment

    在Android开发中,`PreferenceFragment`是一个非常重要的组件,它主要用于构建设置界面。这个组件源自Android的偏好设置框架,允许开发者以简洁的方式展示各种可交互的设置项,如开关按钮、选择列表等。本篇文章将...

    PreferenceFragment偏好设置Demo

    PreferenceFragment偏好设置的Demo,它会自动保存用户数据,上次修改后下一次自动生效,不需要重新配置。用它来做app的设置界面或者保存用户的登陆状态等等会很方便。Android3.0以前我们使用PreferenceActivity基类...

    PreferenceFragment使用方法

    在Android开发中,PreferenceFragment是用于展示用户设置界面的一个关键组件。它允许开发者方便地创建具有各种设置选项的界面,这些选项通常与SharedPreferences对象交互,保存用户的偏好设置。本篇文章将深入探讨...

    android-support-v4-preferencefragment

    android-support-v4-preferencefragment 和 MyViewPagerActivity 相关链接 http://blog.csdn.net/fu222cs98/article/details/21053491

    PreferenceFragment的使用

    根据官方文档的说明以及自带的APIdemo中的例子,我自己使用PreferenceFragment写了一个例子,效果与之前使用继承PreferenceActivity差不多,步骤如下: ①创建一个工程 ②在MainAcitivity中添加菜单按钮及对应效果

    Support PreferenceFragment引用类换成原生

    源码PreferenceFragment,这个源码可以支持Support PreferenceFragment 兼容2.1及以上版本,这个库非常好,只需要修改下Style文件,全版本(Android 2.x-4.x)UI一致,使用方法与官方一摸一样,不一样的只是引用包名...

    PreferenceFragment:适用于Android 2.1及更高版本的非官方支持PreferenceFragment兼容性层

    ###Support PreferenceFragment非官方PreferenceFragment兼容库,支持Android2.1及更高版本。Unofficial PreferenceFragment compatibility layer for Android 2.1 and up.###About the status of the library弥补...

    Android PreferenceActivity与PreferenceFragment详解及简单实例

    PreferenceFragment在Android 3.0 (API level 11) 之后,引入了Fragment的概念,为了更好地支持碎片化编程,谷歌推出了PreferenceFragment,它是继承自Fragment的类,专门用来展示Preference。如果你的应用只针对...

    Android中Fragment子类及其PreferenceFragment的创建过程演示

    本篇将详细介绍如何创建Fragment子类以及如何使用PreferenceFragment来管理设置选项。 1. 创建Fragment子类 创建Fragment子类通常涉及到以下步骤: **第一步:定义Fragment子类** 首先,你需要继承Fragment类并...

    Conversions:一个 Android (4.2.2 Jelly Bean) 应用程序,用于进行通用单位转换,它扩展了使用 Eclipse 用 Ja​​va 编写的 PreferenceFragment

    本篇文章将围绕“Conversions”这个应用程序,深入探讨如何在Android 4.2.2 Jelly Bean系统环境下,利用Java编程语言和Eclipse IDE来构建一个通用的单位转换应用,特别是如何扩展PreferenceFragment以实现功能增强。...

    PreferenceDemo(包含回调方法)

    PreferenceFragment偏好设置的Demo,它会自动保存用户数据,上次修改后下一次自动生效,不需要重新配置。用它来做app的设置界面或者保存用户的登陆状态等等会很方便。Android3.0以前我们使用PreferenceActivity基类...

    ZJ_PreferenceFragment

    PreferenceFragment Demo 1.demo1:基本用法,switchCompat的用法2.demo2:自定义layout文件3.demo3:使用开源PreferenceFragment

    应用源码之Settings.zip

    Settings应用基于Activity和PreferenceFragment的组合构建,主要分为两大部分:UI界面和数据模型。UI界面负责展示设置项,数据模型则处理用户交互和设置值的存储。源码中的各个文件反映了这种模块化的结构,便于理解...

    FragmentPreference的使用Demo

    工作需要,写的Demo,可作为学习的参考,和理解FragmentPreference的实例。FragmentPreference使用的Demo,结合ScollTo,实现FragmentPreference的隐藏与显示。

    PreferenceActivity存储小dome

    1. 需要确保在AndroidManifest.xml中为PreferenceActivity或包含PreferenceFragment的Activity声明主题,通常使用`Theme.Material.Settings`或`Theme.AppCompat.Light.DarkActionBar`。 2. 对于Android 5.0(API级别...

    继承PreferenceActivity

    这个类在Android API Level 11及更高版本中可用,并且在API Level 14之后逐渐被`PreferenceFragment`取代,但仍然可以在一些较低版本的项目中看到。 `PreferenceActivity`是基于`ListActivity`的,它会将`...

    PreferenceActivity简介

    如果你正在开发针对新版本Android的应用,建议使用PreferenceFragment或者更现代的PreferenceFragmentCompat,它们提供了更好的兼容性和更简洁的API。 总之,PreferenceActivity是Android开发中的一个重要组件,它...

Global site tag (gtag.js) - Google Analytics