我通过在项目中扩展AppCompatSpinner制作了“自定义多选”微调器。当我选择一个项目时,它显示选定的项目。我不知道对用户显示的所选项目使用字体。请任何人启发我。 如何使用样式使用字体?可能吗?
<com.sws.ain.helpers.MultiSelectionSpinner
android:id="@+id/industries_spinner"
android:layout_width="200dp"
style="@style/mySpinnerItemStyle"
android:layout_height="40dp"
android:layout_below="@+id/select_country_btn"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:paddingBottom="10dp"
android:background="@drawable/interested_button_shape"
android:gravity="center"
android:textAlignment="center" />
这是自定义微调功能,效果很好。
public class MultiSelectionSpinner extends AppCompatSpinner implements
OnMultiChoiceClickListener {
String[] _items = null;
boolean[] mSelection = null;
boolean[] mSelectionAtStart = null;
String _itemsAtStart = null;
ArrayAdapter<String> simple_adapter;
private OnMultipleItemsSelectedListener listener;
public MultiSelectionSpinner(Context context) {
super(context);
simple_adapter = new ArrayAdapter<>(context,
R.layout.custom_spinner);
super.setAdapter(simple_adapter);
}
public MultiSelectionSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
simple_adapter = new ArrayAdapter<>(context,
R.layout.custom_spinner);
super.setAdapter(simple_adapter);
}
public void setListener(OnMultipleItemsSelectedListener listener) {
this.listener = listener;
}
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(),
R.style.AlertDialogCustom);
builder.setTitle("Please select!!!");
builder.setMultiChoiceItems(_items, mSelection, this);
_itemsAtStart = getSelectedItemsAsString();
AlertDialog alertDialog= builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length);
listener.selectedIndices(getSelectedIndices(), simple_adapter.getItem(0));
listener.selectedStrings(getSelectedStrings(), simple_adapter.getItem(0));
}
}).create();
//2. now setup to change color of the button
alertDialog.setOnShowListener( new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface arg0) {
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setBackgroundColor(ContextCompat.getColor(getContext(),R.color.colorPrimary));
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(getContext(),R.color.white));
}
});
alertDialog.show();
return true;
}
public void setItems(List<String> items, String title) {
_items = items.toArray(new String[0]);
mSelection = new boolean[_items.length];
mSelectionAtStart = new boolean[_items.length];
simple_adapter.clear();
simple_adapter.add(title);
Arrays.fill(mSelection, false);
mSelection[0] = false;
}
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();
}
public interface OnMultipleItemsSelectedListener {
void selectedIndices(List<Integer> indices, String spinnerName);
void selectedStrings(List<String> strings, String spinnerName);
}
}