开发者

How to handle checkbox status manually?

开发者 https://www.devze.com 2023-03-19 13:35 出处:网络
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.

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.setOnPreferenceClickListe开发者_如何学运维ner(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);


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; 
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号