我有一个SwitchPreference和两个CheckBoxPreference,它们都是SwitchPreference的依赖项。
我真正想做的是,如果两个CheckBoxPreference都未选中,则应自动取消选中SwitchPreference,但我的代码无效。
这些是我目前正在做的事情:
fragment_settings
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/set_network">
<SwitchPreference
android:defaultValue="true"
android:key="@string/set_image"
android:title="@string/set_image"/>
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/set_image_rock"
android:title="@string/set_image_rock"
android:dependency="@string/set_image"
android:summaryOn="@string/enabled"
android:summaryOff="@string/disabled"/>
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/set_image_rock_details"
android:title="@string/set_image_rock_details"
android:dependency="@string/set_image"
android:summaryOn="@string/enabled"
android:summaryOff="@string/disabled"/>
</PreferenceCategory>
</PreferenceScreen>
SettingsFragment
public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{
public SettingsFragment() {
// Required empty public constructor
}
//private ListPreference mListPreference;
private SwitchPreference loadImages;
private CheckBoxPreference rockImages, rockDetailsImages;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false);
view.setBackgroundColor(Color.WHITE);
loadImages = (SwitchPreference) getPreferenceManager().findPreference(getString(R.string.set_image));
rockImages = (CheckBoxPreference) getPreferenceManager().findPreference(getString(R.string.set_image_rock)););
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.fragment_settings);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (!rockImages.isChecked() && !rockDetailsImages.isChecked()) {
loadImages.setChecked(false); //This refuses to happen.
}
}
}
请问我该如何实现?