gpt4 book ai didi

android - 在 Android 中使 ListPreference 多项选择的简单方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:18:00 27 4
gpt4 key购买 nike

我正在尝试创建一个首选项对话框窗口,允许用户在列表中选择多个项目。目前它只允许您选择一个项目。是否有捷径可寻?我已经在互联网上四处寻找,但到目前为止还没有找到方法。感谢您的帮助!

最佳答案

这里是您需要的所有代码!

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 - 在 Android 中使 ListPreference 多项选择的简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3446683/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com