开发者

Android preferences screen with interdependent entries

开发者 https://www.devze.com 2023-04-02 22:04 出处:网络
I have an Android preferences screen where some of the preferences are interdependent. In other words, if one of the preferences is set to a certain value(s) then another two of the others are availa

I have an Android preferences screen where some of the preferences are interdependent.

In other words, if one of the preferences is set to a certain value(s) then another two of the others are available. If not, then they are not because they are meaningless.

Specifically, I have an option with 3 possible values: Prompt, Yes and No. When the value is set to No I want to lock the other开发者_运维知识库 2 options.

How do I do this in Android 2.1?


Basically just call setEnabled() on the preferences you want to enable/disable in the OnPreferenceChangeListener for the 3-way preference. e.g.:

otherPrefOne = (ListPreference)findPreference("OTHER_PREF_1");
otherPrefTwo = (ListPreference)findPreference("OTHER_PREF_2");

ThreeWayPref = (ListPreference)findPreference("3WAY_PREF");
ThreeWayPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (((String)newValue).equals("No")) {
            otherPrefOne.setEnabled(false);
            otherPrefTwo.setEnabled(false);
        } else {
            otherPrefOne.setEnabled(true);
            otherPrefTwo.setEnabled(true);
        }
        return true;
    }
});
0

精彩评论

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

关注公众号