如何手动处理复选框状态?

发布于 2024-11-19 19:47:53 字数 579 浏览 5 评论 0原文

我需要手动控制 CheckBoxPreference ;我必须检查自己数据中的条件以确定是否可以设置首选项。

我该怎么做呢?我当前的代码如下,但似乎不起作用。

CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {   
   if (something) {
     return true; // checkbox should be checked
   } else {
     return false; // checkbox should be unchecked
   }

我应该总是返回 false 然后使用

buyPref.setChecked(true);

I need to control a CheckBoxPreference manually; I have to check a condition in my own data to determine if the preference can be set or not.

How can I do it? My current code is as follows, but it doesn't seem to work.

CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {   
   if (something) {
     return true; // checkbox should be checked
   } else {
     return false; // checkbox should be unchecked
   }

Should I always return false and then use

buyPref.setChecked(true);

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

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

发布评论

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

评论(3

独自←快乐 2024-11-26 19:47:53

如果处理了点击,您应该始终返回 true。来自 API

public abstract boolean onPreferenceClick (Preference preference)

自: API 级别 1

单击首选项时调用。

参数

  • 首选项 - 单击的首选项。

返回

  • True - 如果点击已处理。

因此,您的代码应为:

CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {   
   if (buyPref.isChecked()) {
     // checkbox is checked, do something
   } else {
     // checkbox not checked, do something else
   }
   return true; 
}

首选项管理器应处理是否选中该项目,但如果您想自己执行此操作:

CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) { 
   // this will toggle the checkbox 
   buyPref.setChecked(!buyPref.isChecked());
   return true; 
}

You should always return true if the click was handled. From the API:

public abstract boolean onPreferenceClick (Preference preference)

Since: API Level 1

Called when a Preference has been clicked.

Parameters

  • preference - The Preference that was clicked.

Returns

  • True - if the click was handled.

So your code should read:

CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {   
   if (buyPref.isChecked()) {
     // checkbox is checked, do something
   } else {
     // checkbox not checked, do something else
   }
   return true; 
}

The preference manager should handle whether the item is checked or not, but if you want to do it yourself:

CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("pref_billing_buy");
buyPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) { 
   // this will toggle the checkbox 
   buyPref.setChecked(!buyPref.isChecked());
   return true; 
}
人│生佛魔见 2024-11-26 19:47:53

我认为你想要这样的东西:

final CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("logs");
buyPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
{
    @Override
    public boolean onPreferenceChange(final Preference preference,
        final Object newValue)
    {
        boolean condition = false;
        // set condition true or false here according to your needs.
        buyPref.setChecked(condition);
        Editor edit = preference.getEditor();
        edit.putBoolean("pref_billing_buy", condition);
        edit.commit();
        return false;
    }
});

你希望始终返回 false,这样 Android 就不会尝试编写首选项本身。请参阅 OnPreferenceChangeListener 的文档更多信息。

请注意,所有这些都将发生在 UI 线程上,因此如果您需要执行任何长时间运行的操作,我会将其放入 AsyncTask 带有 ProgressDialog 这样用户就不会感到沮丧。

I think you want something like this:

final CheckBoxPreference buyPref = (CheckBoxPreference) findPreference("logs");
buyPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener()
{
    @Override
    public boolean onPreferenceChange(final Preference preference,
        final Object newValue)
    {
        boolean condition = false;
        // set condition true or false here according to your needs.
        buyPref.setChecked(condition);
        Editor edit = preference.getEditor();
        edit.putBoolean("pref_billing_buy", condition);
        edit.commit();
        return false;
    }
});

You want to always return false from this so that Android doesn't attempt to write the preference itself. See the documentation for OnPreferenceChangeListener for more info.

Note that all of this will happen on the UI thread, so if you need to do anything long-running, I would throw it in an AsyncTask with a ProgressDialog so that the user doesn't get frustrated.

云醉月微眠 2024-11-26 19:47:53

您可以使用它来检查复选框是否被选中

if(buyPref.isChecked())

<前><代码>{

//做你想做的事

}
别的
{
做点别的事
}

设置复选框,

buypref.setChecked(true);

有帮助!

干杯
尼辛

You can use this to check if the checkboxes are checked or not

if(buyPref.isChecked())
{ 

//Do What you want

}
else
{
Do something else
}

To set a checkbox,

buypref.setChecked(true);

Hope it helps!

Cheers
Nithin

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