SwitchPreference onChecked/onClick侦听器 [英] SwitchPreference onChecked/onClick Listener

查看:127
本文介绍了SwitchPreference onChecked/onClick侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整夜都在绞尽脑汁,但似乎无法做到这一点.我想将SwitchPreference添加到我的应用程序的PreferenceActivity中.下面是一张图片.

I've been racking my brains all night but can't seem to accomplish this one small thing. I would like to add a SwitchPreference into my PreferenceActivity of an app. Below is a picture.

在我说太多之前,我的问题恰恰是这样:我似乎无法仅在首选项的Switch部分上放置一个侦听器.我可以在首选项上设置onPreferenceTreeClick和onPreferenceClick,如果按文本部分,效果很好.但是,当我将开关从OFF更改为ON时,Switch本身什么也不做.

Before I say too much, my problem is exactly this: I cannot seem to put a listener on just the Switch part of the preference. I am able to set an onPreferenceTreeClick and an onPreferenceClick on the preference, and that works fine if I press on the text portion. But When the Switch itself does nothing when I change it from OFF to ON.

我已阅读 SwitchPreference 上的文档.我还查看了android/packages/Settings,看起来AOSP使用的是Switch而不是Wi-Fi和Bluetooth的SwitchPreference.

I've read the documentation on SwitchPreference. I also looked at the android/packages/Settings and it looks like AOSP uses a Switch and not a SwitchPreference for Wi-Fi and Bluetooth.

这是我的尝试(如果您按了整个首选项,就可以了,但是如果您只按了Switch则没有用):

Here is my attempt (working if you press on the entire preference item, but not if you just press the Switch):

示例:

public class Preferences extends SherlockPreferenceActivity {

public static final String PREF_THEME = "pref_theme_interface";
public static final String PREF_ROOT = "pref_root";
public static final String PREF_APP = "pref_app";

public static SharedPreferences mTheme;
private static SharedPreferences mUpdate;
public static SharedPreferences.Editor mEditor;

public boolean SDK_COMPAT = true;

boolean pSwitch = false; 
boolean update = true;

Preference autoUpdate;

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            break;
    }
    return super.onOptionsItemSelected(item);
}

    @Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(MainActivity.THEME);
    super.onCreate(savedInstanceState);
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setIcon(R.drawable.ic_preferences);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        SDK_COMPAT = false;
    }

    mUpdate = PreferenceManager.getDefaultSharedPreferences(this);
    update = mUpdate.getBoolean("update", false);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);

    setPreferenceScreen(createPreferenceSDK());

}

private PreferenceScreen createPreferenceSDK() {
    // Root
    PreferenceScreen root = (PreferenceScreen)findPreference(PREF_ROOT);

    PreferenceCategory prefApp = (PreferenceCategory)findPreference(PREF_APP);

    //root.addPreference(prefApp);

    if (SDK_COMPAT == true) {
        pSwitch = true;
        autoUpdate = new SwitchPreference(this);
        autoUpdate.setKey("auto_update_pref");
        autoUpdate.setTitle(R.string.auto_update);
        //autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
        prefApp.addPreference(autoUpdate);
    } else {
        pSwitch = false;
        autoUpdate = new CheckBoxPreference(this);
        autoUpdate.setKey("auto_update_pref");
        autoUpdate.setTitle(R.string.auto_update);
        autoUpdate.setSummary(R.string.auto_update_summary);
        prefApp.addPreference(autoUpdate);
    }

    autoUpdate.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        public boolean onPreferenceClick(Preference preference) {
            mEditor = mUpdate.edit();
            boolean checked = ((SwitchPreference) preference)
                    .isChecked();
            if (checked) {
                update = true;
                mEditor.putBoolean("update", true);
                mEditor.commit();
                autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
            } else {
                update = false;
                mEditor.putBoolean("update", false);
                mEditor.commit();
                autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");
            }
            return true;
        }

    });

    return root;
}

如果我迷失了你,请重申我的问题.如何在SwitchPreference的Switch部分上设置侦听器?如果它是如此明显,请好心.昨晚很晚,当我尝试添加此内容时.

So to reiterate my question in case I lost you. How does one set a listener on the Switch portion of the SwitchPreference? Please be kind if it is something so obvious. It was pretty late last night when I tried to add this.

非常感谢您.

注意:1.我不反对坚持使用CheckBoxPreference,但是我更喜欢使用Switch,因为它看起来不错.

Notes: 1. I am not opposed to sticking with the CheckBoxPreference, but I prefer to use Switch because it looks nice.

  1. 是的,我知道有一种更轻松/更好的选择吗?使用res/xml和res/xml-v14添加动态首选项的方法,而不是执行SDK检查.我只是为了测试而做.

偏好设置"屏幕的图片

编辑

希望这对其他人有帮助!感谢Tushar的建议:-)

Hopefully this helps someone else! Thanks Tushar for suggestion :-)

autoUpdate.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            boolean switched = ((SwitchPreference) preference)
                    .isChecked();
            update = !switched;
            mEditor = mUpdate.edit();
            mEditor.putBoolean("update", update);
            mEditor.commit();
            autoUpdate.setSummary(update == false ? "Disabled" : "Enabled");

            return true;
        }

    });

推荐答案

使用 setOnPreferenceChangeListener()代替 setOnPreferenceClickListener().

这篇关于SwitchPreference onChecked/onClick侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆