我正在尝试创建一个首选项对话框窗口,允许用户在列表中选择多个项目。目前它只允许您选择一项。是否有捷径可寻?我已经查看了整个互联网,但还没有看到任何方法。任何帮助表示赞赏!
4 回答
这是您需要的所有代码!
http://blog.350nice.com/wp/wp-content/uploads/2009/07/listpreferencemultiselect.java
public class ListPreferenceMultiSelect extends ListPreference {
//Need to make sure the SEPARATOR is unique and weird enough that it doesn't match one of the entries.
//Not using any fancy symbols because this is interpreted as a regex for splitting strings.
private static final String SEPARATOR = "OV=I=XseparatorX=I=VO";
private boolean[] mClickedDialogEntryIndices;
public ListPreferenceMultiSelect(Context context, AttributeSet attrs) {
super(context, attrs);
mClickedDialogEntryIndices = new boolean[getEntries().length];
}
@Override
public void setEntries(CharSequence[] entries) {
super.setEntries(entries);
mClickedDialogEntryIndices = new boolean[entries.length];
}
public ListPreferenceMultiSelect(Context context) {
this(context, null);
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
CharSequence[] entries = getEntries();
CharSequence[] entryValues = getEntryValues();
if (entries == null || entryValues == null || entries.length != entryValues.length ) {
throw new IllegalStateException(
"ListPreference requires an entries array and an entryValues array which are both the same length");
}
restoreCheckedEntries();
builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices,
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean val) {
mClickedDialogEntryIndices[which] = val;
}
});
}
public static String[] parseStoredValue(CharSequence val) {
if ( "".equals(val) )
return null;
else
return ((String)val).split(SEPARATOR);
}
private void restoreCheckedEntries() {
CharSequence[] entryValues = getEntryValues();
String[] vals = parseStoredValue(getValue());
if ( vals != null ) {
for ( int j=0; j<vals.length; j++ ) {
String val = vals[j].trim();
for ( int i=0; i<entryValues.length; i++ ) {
CharSequence entry = entryValues[i];
if ( entry.equals(val) ) {
mClickedDialogEntryIndices[i] = true;
break;
}
}
}
}
}
@Override
protected void onDialogClosed(boolean positiveResult) {
// super.onDialogClosed(positiveResult);
CharSequence[] entryValues = getEntryValues();
if (positiveResult && entryValues != null) {
StringBuffer value = new StringBuffer();
for ( int i=0; i<entryValues.length; i++ ) {
if ( mClickedDialogEntryIndices[i] ) {
value.append(entryValues[i]).append(SEPARATOR);
}
}
if (callChangeListener(value)) {
String val = value.toString();
if ( val.length() > 0 )
val = val.substring(0, val.length()-SEPARATOR.length());
setValue(val);
}
}
}
}
您想要哪个 Android-Api-Level ?
当您使用 API-Level 11 时,您可以使用此 MultiSelect Preference for Android 3.0 或更高版本
当您使用 API 级别较小的 11 时,您可以使用 MuliiSelect Preference 的自定义实现
谢谢你的帖子,它对我帮助很大。我对类进行了一些更改,以允许用户使用所选值更新首选项中显示的摘要。
这样,用户无需打开 Spinner 就可以看到他的选择。
以下是添加的方法:
// Fills the Entry Values List
@Override
public void setEntryValues(CharSequence[] entryValues) {
super.setEntryValues(entryValues);
restoreCheckedEntries();
}
// Updates the Summary
@Override
public void setSummary(CharSequence entries) {
String s = "";
for (int x = 0; x < getEntryValues().length; x++)
if (mClickedDialogEntryIndices[x])
s += (s.equals("") ? "" : ", ") + getEntries()[x];
super.setSummary(s);
}
必须在 SettingsActivity.java 中调用 setSummary 方法:
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener
= new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreferenceMultiSelect) {
ListPreferenceMultiSelect listPreference =
(ListPreferenceMultiSelect) preference;
listPreference.setSummary("");
}
return true;
}
};
方法 setEntryValues 可以与 setEntries 一起调用。
它对我有用!与 Android 2.2 或更高版本兼容。
根据定义,首选项是单个值。如果你想实现多项选择ListPreference
,你可能必须继承这个类或者android.preference.Preference
创建你自己的实现。
或者,您可以从简单的首选项屏幕调用活动并自己处理所有事情。
您可以将值存储在带有分隔符的字符串首选项中,或者更好的是,作为各种布尔首选项。