Làm cách nào để tạo một ListPreference
với checkbox
? Tôi biết cách sử dụng ListPreference
, nhưng tôi cần nhiều lựa chọn như trong ứng dụng Báo thức theo tùy chọn "lặp lại".Cách tạo ListPreference với hộp kiểm
như ảnh chụp màn hình này:
Làm cách nào để tạo một ListPreference
với checkbox
? Tôi biết cách sử dụng ListPreference
, nhưng tôi cần nhiều lựa chọn như trong ứng dụng Báo thức theo tùy chọn "lặp lại".Cách tạo ListPreference với hộp kiểm
như ảnh chụp màn hình này:
Không có built-in thích cho AFAIK đó. ListPreference
chỉ chọn một lần.
Bạn có thể tạo lớp tùy chỉnh Preference
tùy chỉnh của riêng mình, tuy nhiên, bằng cách mở rộng DialogPreference
.
Kể từ API 11 bạn có thể sử dụng MultiSelectListPreference
String[] selections = {"selection1","Selection2"};
Set<String> selectionSet = new HashSet<String>();
selectionSet.addAll(Arrays.asList(selections));
MultiSelectListPreference multiSelectPref = new MultiSelectListPreference(this);
multiSelectPref.setKey("multi_pref");
multiSelectPref.setTitle("Multi Select List Preference");
multiSelectPref.setEntries(selections);
multiSelectPref.setEntryValues(selections);
multiSelectPref.setDefaultValue(selectionSet);
getPreferenceScreen().addPreference(multiSelectPref);
Đối với các giá trị boolean bạn phải sử dụng một SwitchPreference, như sau:
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="@string/pref_description_social_recommendations"
android:title="@string/pref_title_social_recommendations" />