选择微调器数据时如何显示复选框?

How can I showing checkbox when I choose the spinner data?

我开发了一个 Android 应用程序,当我选择微调器数据、值和显示的复选框数量时,我在显示复选框时遇到了一些问题,这些复选框来自我选择的微调器。谁能帮我举个例子?谢谢。

试试这个习惯 Class

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

ArrayAdapter<String> simple_adapter;

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

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

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

    simple_adapter = new ArrayAdapter<>(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);
    _itemsAtStart = getSelectedItemsAsString();
    builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length);
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            simple_adapter.clear();
            simple_adapter.add(_itemsAtStart);
            System.arraycopy(mSelectionAtStart, 0, mSelection, 0, mSelectionAtStart.length);
        }
    });
    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];
    mSelectionAtStart = new boolean[_items.length];
    simple_adapter.clear();
    simple_adapter.add(_items[0]);
    Arrays.fill(mSelection, false);
    mSelection[0] = true;
    mSelectionAtStart[0] = true;
}

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

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

public void setSelection(List<String> selection) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
        mSelectionAtStart[i] = false;
    }
    for (String sel : selection) {
        for (int j = 0; j < _items.length; ++j) {
            if (_items[j].equals(sel)) {
                mSelection[j] = true;
                mSelectionAtStart[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;
        mSelectionAtStart[i] = false;
    }
    if (index >= 0 && index < mSelection.length) {
        mSelection[index] = true;
        mSelectionAtStart[index] = true;
    } else {
        throw new IllegalArgumentException("Index " + index
                + " is out of bounds.");
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
}

public void setSelection(int[] selectedIndices) {
    for (int i = 0; i < mSelection.length; i++) {
        mSelection[i] = false;
        mSelectionAtStart[i] = false;
    }
    for (int index : selectedIndices) {
        if (index >= 0 && index < mSelection.length) {
            mSelection[index] = true;
            mSelectionAtStart[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<>();
    for (int i = 0; i < _items.length; ++i) {
        if (mSelection[i]) {
            selection.add(_items[i]);
        }
    }
    return selection;
}

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

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();
}

public String getSelectedItemsAsString() {
    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();
}}

在你的Activity中使用下面的代码

   private MultiSelectionSpinner multiSelectionSpinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String[] array = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
    multiSelectionSpinner = (MultiSelectionSpinner) findViewById(R.id.mySpinner);
    multiSelectionSpinner.setItems(array);
    multiSelectionSpinner.setSelection(new int[]{2, 6});
}

以及您必须在 xml 布局文件中使用 multispinner 而不是简单的 spinner

的主要内容