gpt4 book ai didi

android - 添加一个按钮以选择多选微调器中的所有项目

转载 作者:行者123 更新时间:2023-11-29 21:10:31 25 4
gpt4 key购买 nike

早上好,我有一个 Android 应用程序,我添加了一个我找到的 MultiSelectSpinner 并进行了一些编辑,现在我想添加一个按钮来选择所有项目,我搜索了很多但我还没有找到方法,所有我可想的很复杂。有人必须实现类似的东西吗?感谢您的帮助。祝你有美好的一天。

这是我的 MultiSelectSpinner 的代码:

public class MultiSelectionSpinner extends Spinner implements
OnMultiChoiceClickListener {
String[] _items = null;
boolean[] mSelection = null;

ArrayAdapter<String> simple_adapter;

public MultiSelectionSpinner(Context context) {
super(context);

simple_adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}

public MultiSelectionSpinner(Context context, AttributeSet attrs) {
super(context, attrs);

simple_adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}

public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if (mSelection != null && which < mSelection.length) {
mSelection[which] = isChecked;

simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
} else {
throw new IllegalArgumentException(
"Argument 'which' is out of bounds.");
}
}

@Override
public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(_items, mSelection, this);
builder.show();
return true;
}

@Override
public void setAdapter(SpinnerAdapter adapter) {
throw new RuntimeException(
"setAdapter is not supported by MultiSelectSpinner.");
}

public void setItems(String[] items) {
_items = items;
mSelection = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add("Ningun sector seleccionado");
Arrays.fill(mSelection, false);
}

public void setItems(List<String> items) {
_items = items.toArray(new String[items.size()]);
mSelection = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add(_items[0]);
Arrays.fill(mSelection, false);
}

public void setSelection(String[] selection) {
for (String cell : selection) {
for (int j = 0; j < _items.length; ++j) {
if (_items[j].equals(cell)) {
mSelection[j] = true;
}
}
}
}

public void setSelection(List<String> selection) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
}
for (String sel : selection) {
for (int j = 0; j < _items.length; ++j) {
if (_items[j].equals(sel)) {
mSelection[j] = true;
}
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}

public void setSelection(int index) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
}
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
} else {
throw new IllegalArgumentException("Index " + index
+ " is out of bounds.");
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}

public void setSelection(int[] selectedIndicies) {
for (int i = 0; i < mSelection.length; i++) {
mSelection[i] = false;
}
for (int index : selectedIndicies) {
if (index >= 0 && index < mSelection.length) {
mSelection[index] = true;
} else {
throw new IllegalArgumentException("Index " + index
+ " is out of bounds.");
}
}
simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
}

public List<String> getSelectedStrings() {
List<String> selection = new LinkedList<String>();
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
selection.add(_items[i]);
}
}
return selection;
}

public List<Integer> getSelectedIndicies() {
List<Integer> selection = new LinkedList<Integer>();
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
selection.add(i);
}
}
return selection;
}

public boolean haveSelectedItems() {
return (mSelection != null && mSelection.length > 0);
}

public boolean haveItems() {
return (_items != null && _items.length > 0);
}

private String buildSelectedItemString() {
StringBuilder sb = new StringBuilder();
boolean foundOne = false;
for (int i = 0; i < _items.length; ++i) {
if (mSelection[i]) {
if (foundOne) {
sb.append(", ");
}
foundOne = true;
sb.append(_items[i]);
}
}
return sb.toString();
}
}

谢谢

最佳答案

使用下面的代码(更改您的 performeClick 方法并添加此 selectAll 方法)在您的应用程序上启用全选。我添加了两个按钮(setNegativeButton、setPositiveButton)及其文本(全部清除、全选),我不想在用户单击这些按钮后立即关闭对话框。享受!

@Override
public boolean performClick() {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(_items, mSelection, this);
builder.setTitle(title);
builder.setNegativeButton("Clear All"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
}).setPositiveButton("Select All"),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
});

final AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean wantToCloseDialog = false;
selectAll(false, dialog);

if (wantToCloseDialog)
dialog.dismiss();
}
});
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Boolean wantToCloseDialog = false;
selectAll(true, dialog);

if (wantToCloseDialog)
dialog.dismiss();
}
});

return true;
}

protected void selectAll(boolean isSelectAll, AlertDialog dialog) {
if (mSelection != null) {
for (int i = 0; i < _items.length; i++) {
mSelection[i] = isSelectAll;
((AlertDialog) dialog).getListView().setItemChecked(i, isSelectAll);

}

simple_adapter.clear();
simple_adapter.add(buildSelectedItemString());
} else {
throw new IllegalArgumentException("mSelection is null");
}
}

关于android - 添加一个按钮以选择多选微调器中的所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021413/

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