Android学习【10】PreferenceFragment偏好设置(二)

上一篇讲述了PreferenceFragment中的各种控件,接下来为各个控件添加监听事件。。。。

主要实现了3个接口OnPreferenceClickListener,OnSharedPreferenceChangeListener和OnPreferenceChangeListener。

onPreferenceClick为点击事件,当点击Preference控件后将触发该方法。但是EditTextPreference等修改内容后再次确认时,不会调用该方法。为了解决这一问题,onSharedPreferenceChanged登场了。当Preference控件内容发生改变时,将触发该方法。最后onPreferenceChange方法用于MultiSelectList和Ringtone的调用。

细节就直接上代码啦,这样比较清楚。。。

public class SettingFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener, SharedPreferences.OnSharedPreferenceChangeListener,Preference.OnPreferenceChangeListener {

    private CheckBoxPreference checkbox;
    private EditTextPreference edittext;
    private MultiSelectListPreference MultiSelectList;
    private ListPreference list;
    private RingtonePreference Ringtone;
    private SwitchPreference Switch;
    private Preference preference;

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

        //根据key值找到控件
        checkbox = (CheckBoxPreference) findPreference("checkbox");
        edittext = (EditTextPreference) findPreference("edittext");
        MultiSelectList = (MultiSelectListPreference)findPreference("MultiSelectList");
        list = (ListPreference)findPreference("list");
        Ringtone = (RingtonePreference) findPreference("Ringtone");
        Switch = (SwitchPreference) findPreference("switch");

        // 设置监听器
        checkbox.setOnPreferenceClickListener(this);
        edittext.setOnPreferenceClickListener(this);
        Ringtone.setOnPreferenceClickListener(this);
        MultiSelectList.setOnPreferenceChangeListener(this);
        Ringtone.setOnPreferenceChangeListener(this);

        // 得到我们的存储Preferences值的对象,然后对其进行相应操作
        SharedPreferences shp = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
        boolean apply_Checked = shp.getBoolean("checkbox", false);

        //获取历史设置
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
        edittext.setSummary(sp.getString("edittext", ""));
    }

    @Override
    // 点击事件触发
    public boolean onPreferenceClick(Preference preference) {
        // 对控件进行操作
        if (preference == checkbox) {
            Toast.makeText(this.getActivity(), " 点击了checkbox :" + checkbox.isChecked(), Toast.LENGTH_SHORT).show();
        } else if (preference == edittext) {
            edittext.setText("");
            Toast.makeText(this.getActivity(), " 点击了edittext :" + edittext.getText(), Toast.LENGTH_SHORT).show();
        } else if (preference == Ringtone) {
            Toast.makeText(this.getActivity(), " 点击了Ringtone :" + Ringtone.getRingtoneType(), Toast.LENGTH_SHORT).show();
        }
        return true;
    }

    //当Preference发生变化时调用
    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("edittext")) {
            edittext.setSummary(edittext.getText());
        }
        if (key.equals("list")) {
            list.setSummary(list.getValue());
        }
        if(key.equals("Ringtone")){
            Ringtone.setSummary(Ringtone.getRingtoneType());
        }
        if(key.equals("switch")){
            Switch.setSummary(String.valueOf(Switch.isChecked()));
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //注册事件
        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        //注销事件
        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
        super.onPause();
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference == MultiSelectList) {
            MultiSelectList.setSummary(newValue.toString());
            return true;
        }
        if (preference == Ringtone) {
            Ringtone.setSummary(newValue.toString());
            return true;
        }
        return false;
    }
}
源码戳这里

你可能感兴趣的:(偏好设置)