0

这似乎是一个经常被问到的问题,但我仍然无法解决,使用 Stack Overflow 上提出的非常方法。我有一个preference.xml,如下所示:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="isBlock"
        android:layout="@layout/custom_checkbox_layout"
        android:summaryOff="Let strange calls in"
        android:summaryOn="Unlisted calls will be blocked"
        android:title="Block Strange Calls" />
.....

我想在我的代码中更改复选框的状态。所以我的Java代码贴在下面:

public class Prefs extends PreferenceActivity implements OnSharedPreferenceChangeListener {

public static boolean blockOrNot;
private MySharedPreference mySharedPreference;

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preference);
    setContentView(R.layout.custom_preference_layout);

    ImageButton save = (ImageButton) findViewById(R.id.save_prefs);
    save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (blockOrNot) {
                Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
            }
        }
    });
}

@Override
protected void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    blockOrNot = sp.getBoolean("isBlock", false);
}

@Override
protected void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    // TODO Auto-generated method stub
    if (key.equals("isBlock")) {
        blockOrNot = sharedPreferences.getBoolean(key, false);
    }
}

}

但是每当我按下按钮时,我只能得到“true”,这是复选框的原始值,以为我没有选中它。我搜索了这个问题,之前在 SOF 上尝试过一种方法,如下所示:

public class Prefs extends PreferenceActivity implements OnSharedPreferenceChangeListener {

    public static boolean blockOrNot;
    private MySharedPreference mySharedPreference;

    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preference);
        setContentView(R.layout.custom_preference_layout);

        ImageButton save = (ImageButton) findViewById(R.id.save_prefs);
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (blockOrNot) {
                    Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(Prefs.this, "Saved! " + blockOrNot, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, final String key) {
        // TODO Auto-generated method stub
        CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager().findPreference(key);
        checkboxPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if (key.equals("isBlock")) {
                    blockOrNot = (Boolean) newValue;
                }
                return true;
            }
        });
    }
}

但仍然没有任何改变。有人可以给点建议吗?谢谢大家!!

4

0 回答 0