Android Spinner有多种选择

如何创建允许选择多个项目的微调器,即带有复选框的微调器?



凤凰求蛊
浏览 719回答 3
3回答

慕的地6264312

我编写了MultiSpinner的自定义实现。它看起来类似于普通的微调器,但它有复选框而不是radiobuttons。选定的值显示在微调器上除以逗号。默认情况下会检查所有值。试试吧:package cz.destil.settleup.gui;public class MultiSpinner extends Spinner implements&nbsp; &nbsp; &nbsp; &nbsp; OnMultiChoiceClickListener, OnCancelListener {&nbsp; &nbsp; private List<String> items;&nbsp; &nbsp; private boolean[] selected;&nbsp; &nbsp; private String defaultText;&nbsp; &nbsp; private MultiSpinnerListener listener;&nbsp; &nbsp; public MultiSpinner(Context context) {&nbsp; &nbsp; &nbsp; &nbsp; super(context);&nbsp; &nbsp; }&nbsp; &nbsp; public MultiSpinner(Context arg0, AttributeSet arg1) {&nbsp; &nbsp; &nbsp; &nbsp; super(arg0, arg1);&nbsp; &nbsp; }&nbsp; &nbsp; public MultiSpinner(Context arg0, AttributeSet arg1, int arg2) {&nbsp; &nbsp; &nbsp; &nbsp; super(arg0, arg1, arg2);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onClick(DialogInterface dialog, int which, boolean isChecked) {&nbsp; &nbsp; &nbsp; &nbsp; if (isChecked)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected[which] = true;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected[which] = false;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onCancel(DialogInterface dialog) {&nbsp; &nbsp; &nbsp; &nbsp; // refresh text on spinner&nbsp; &nbsp; &nbsp; &nbsp; StringBuffer spinnerBuffer = new StringBuffer();&nbsp; &nbsp; &nbsp; &nbsp; boolean someUnselected = false;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < items.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (selected[i] == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerBuffer.append(items.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerBuffer.append(", ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; someUnselected = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; String spinnerText;&nbsp; &nbsp; &nbsp; &nbsp; if (someUnselected) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerText = spinnerBuffer.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (spinnerText.length() > 2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerText = spinnerText.substring(0, spinnerText.length() - 2);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerText = defaultText;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android.R.layout.simple_spinner_item,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new String[] { spinnerText });&nbsp; &nbsp; &nbsp; &nbsp; setAdapter(adapter);&nbsp; &nbsp; &nbsp; &nbsp; listener.onItemsSelected(selected);&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public boolean performClick() {&nbsp; &nbsp; &nbsp; &nbsp; AlertDialog.Builder builder = new AlertDialog.Builder(getContext());&nbsp; &nbsp; &nbsp; &nbsp; builder.setMultiChoiceItems(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; items.toArray(new CharSequence[items.size()]), selected, this);&nbsp; &nbsp; &nbsp; &nbsp; builder.setPositiveButton(android.R.string.ok,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DialogInterface.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onClick(DialogInterface dialog, int which) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dialog.cancel();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; builder.setOnCancelListener(this);&nbsp; &nbsp; &nbsp; &nbsp; builder.show();&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }&nbsp; &nbsp; public void setItems(List<String> items, String allText,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MultiSpinnerListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; this.items = items;&nbsp; &nbsp; &nbsp; &nbsp; this.defaultText = allText;&nbsp; &nbsp; &nbsp; &nbsp; this.listener = listener;&nbsp; &nbsp; &nbsp; &nbsp; // all selected by default&nbsp; &nbsp; &nbsp; &nbsp; selected = new boolean[items.size()];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < selected.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected[i] = true;&nbsp; &nbsp; &nbsp; &nbsp; // all text on the spinner&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android.R.layout.simple_spinner_item, new String[] { allText });&nbsp; &nbsp; &nbsp; &nbsp; setAdapter(adapter);&nbsp; &nbsp; }&nbsp; &nbsp; public interface MultiSpinnerListener {&nbsp; &nbsp; &nbsp; &nbsp; public void onItemsSelected(boolean[] selected);&nbsp; &nbsp; }}你在XML中使用它像这样:<cz.destil.settleup.gui.MultiSpinner android:id="@+id/multi_spinner" />你用Java将数据传递给它:MultiSpinner multiSpinner = (MultiSpinner) findViewById(R.id.multi_spinner);multiSpinner.setItems(items, getString(R.string.for_all), this);你还需要实现一个监听器,它将返回相同长度的数组,使用true或false来显示所选的未选择状态。public void onItemsSelected(boolean[] selected);

catspeake

谢谢你的帖子!好的解决方案 我对类(方法setItems)进行了一些小改动,以允许用户设置已选择的项目,而不是默认选择所有项目为true。public void setItems(&nbsp; &nbsp; List<String> items,&nbsp; &nbsp; List<String> itemValues,&nbsp;&nbsp; &nbsp; String selectedList,&nbsp; &nbsp; String allText,&nbsp; &nbsp; MultiSpinnerListener listener) {&nbsp; &nbsp; &nbsp; &nbsp; this.items = items;&nbsp; &nbsp; &nbsp; &nbsp; this.defaultText = allText;&nbsp; &nbsp; &nbsp; &nbsp; this.listener = listener;&nbsp; &nbsp; &nbsp; &nbsp; String spinnerText = allText;&nbsp; &nbsp; &nbsp; &nbsp; // Set false by default&nbsp; &nbsp; &nbsp; &nbsp; selected = new boolean[itemValues.size()];&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < itemValues.size(); j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected[j] = false;&nbsp; &nbsp; &nbsp; &nbsp; if (selectedList != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerText = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Extract selected items&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] selectedItems = selectedList.trim().split(",");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set selected items to true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < selectedItems.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < itemValues.size(); j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (selectedItems[i].trim().equals(itemValues.get(j))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selected[j] = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerText += (spinnerText.equals("")?"":", ") + items.get(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Text for the spinner&nbsp; &nbsp; &nbsp; &nbsp; ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android.R.layout.simple_spinner_item, new String[] { spinnerText });&nbsp; &nbsp; &nbsp; &nbsp; setAdapter(adapter);}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android