Android CheckBoxPreference - 取消/检查所有首选项

发布于 2024-11-05 07:14:32 字数 1203 浏览 2 评论 0原文

我有一个仅包含 CheckBoxPreferences (要选择的类别)的 PreferenceScreen 。我需要一个选项来选中/取消选中所有这些。我有以下代码可以完成这项工作,但有一个问题:屏幕上的复选框未更新 - 我需要在视图或其他内容上调用一些 invalidate 。

这是我现在的代码:

    private void checkAll() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for(String s : categories.keySet()) {
            editor.putBoolean(s, true);
        }
        editor.commit();
    }

    private void uncheckAll() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for(String s : categories.keySet()) {
            editor.putBoolean(s, false);
        }
        editor.commit();
        this.restart();
    }

该代码工作正常,但我需要以某种方式刷新视图才能立即查看结果(不仅仅是在关闭并重新启动设置活动之后)。

谢谢大家的建议!

I have a PreferenceScreen containing only CheckBoxPreferences (categories to select). I need an option to check/uncheck all of them. I have the following code that does the job but there is one problem: checkboxes on the screen are not updated - I'd need to call some invalidate on the view or something.

Here is my present code:

    private void checkAll() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for(String s : categories.keySet()) {
            editor.putBoolean(s, true);
        }
        editor.commit();
    }

    private void uncheckAll() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = settings.edit();
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for(String s : categories.keySet()) {
            editor.putBoolean(s, false);
        }
        editor.commit();
        this.restart();
    }

This code works fine but I'd need to refresh the view somehow to see the result imediatelly (not just after closing and re-starting the settings activity).

Thank You all for any advice!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最佳男配角 2024-11-12 07:14:32

这是因为您实际上并没有获取首选项对象,而只是获取实际值。在 for 循环中尝试一下:

 boolean theValue = param;
 for(String s : categories.keySet()) {
    Preference pref = findPreference(s);
    if (pref instanceof CheckBoxPreference) {
      CheckBoxPreference check = (CheckBoxPreference)pref;
      check.setChecked(theValue);
    }
  }

我认为您可以省略调用编辑器并提交,因为 setChecked() 会为您执行此操作。

也检查这个

That's because you're not actually grabbing the preference objects, just the actual values. Try this in your for loops:

 boolean theValue = param;
 for(String s : categories.keySet()) {
    Preference pref = findPreference(s);
    if (pref instanceof CheckBoxPreference) {
      CheckBoxPreference check = (CheckBoxPreference)pref;
      check.setChecked(theValue);
    }
  }

I think you can omit calling the editor and committing, as the setChecked() will do that for you.

Check this out as well.

如何视而不见 2024-11-12 07:14:32

现在我有了这个工作代码,也许它会对某人有所帮助:

import ...

public class CategoriesActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.category_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.categories_check_all:
                this.checkAll();
                return true;
            case R.id.categories_uncheck_all:
                this.uncheckAll();
                return true;
        }
        return false;
    }

    private void checkAll() {
        this.setCheckState(true);
    }

    private void uncheckAll() {
        this.setCheckState(false);
    }

    private void setCheckState(Boolean state) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for (String s : categories.keySet()) {
            Preference pref = findPreference(s);
            if (pref instanceof CheckBoxPreference) {
                CheckBoxPreference check = (CheckBoxPreference) pref;
                check.setChecked(state);
            }
        }

    }
}

Now I have this working code, maybe it will help somebody:

import ...

public class CategoriesActivity extends PreferenceActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.category_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.categories_check_all:
                this.checkAll();
                return true;
            case R.id.categories_uncheck_all:
                this.uncheckAll();
                return true;
        }
        return false;
    }

    private void checkAll() {
        this.setCheckState(true);
    }

    private void uncheckAll() {
        this.setCheckState(false);
    }

    private void setCheckState(Boolean state) {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        @SuppressWarnings("unchecked")
        Map<String, Boolean> categories = (Map<String, Boolean>) settings.getAll();
        for (String s : categories.keySet()) {
            Preference pref = findPreference(s);
            if (pref instanceof CheckBoxPreference) {
                CheckBoxPreference check = (CheckBoxPreference) pref;
                check.setChecked(state);
            }
        }

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文