在Android中,Spinner默認只支持單選。要實現多選功能,可以使用CheckBox
或者第三方庫。下面是兩種方法的詳細說明:
方法一:使用CheckBox
Spinner
和CheckBox
。<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="多選" />
</LinearLayout>
Spinner
和CheckBox
。public class MainActivity extends AppCompatActivity {
private Spinner spinner;
private CheckBox checkBox;
private List<String> items;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = findViewById(R.id.spinner);
checkBox = findViewById(R.id.checkBox);
items = new ArrayList<>();
items.add("選項1");
items.add("選項2");
items.add("選項3");
items.add("選項4");
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (checkBox.isChecked()) {
// 保存選中的項
List<String> selectedItems = new ArrayList<>();
for (int i = 0; i < items.size(); i++) {
CheckBox checkBox = findViewById(R.id.checkBox);
if (i == position && checkBox.isChecked()) {
selectedItems.add(items.get(i));
}
}
// 在這里處理選中的項,例如保存到數據庫或SharedPreferences
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
方法二:使用第三方庫(如:Android-MultiSelect-Spinner
)
build.gradle
文件中添加依賴。implementation 'com.github.jorgecastilloprz:fabprogresscircle:1.01@aar'
implementation 'com.github.jorgecastilloprz:android-multiselectspinner:1.06@aar'
MultiSelectSpinner
。<com.jorgecastilloprz.fabprogresscircle.FabProgressCircle
android:id="@+id/multiSelectSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:spinner_color="#FF5722"
app:fab_progress_circle_color="#FFC107"
app:fab_progress_circle_stroke_width="4dp"
app:fab_progress_circle_radius="50dp"
app:fab_progress_circle_text="多選"
app:fab_progress_circle_text_color="#FFFFFF"
app:fab_progress_circle_text_size="16sp" />
MultiSelectSpinner
。public class MainActivity extends AppCompatActivity {
private MultiSelectSpinner multiSelectSpinner;
private List<String> items;
private List<String> selectedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
multiSelectSpinner = findViewById(R.id.multiSelectSpinner);
items = new ArrayList<>();
items.add("選項1");
items.add("選項2");
items.add("選項3");
items.add("選項4");
selectedItems = new ArrayList<>();
multiSelectSpinner.setItems(items);
multiSelectSpinner.setOnItemSelectedListener(new MultiSelectSpinner.OnItemSelectedListener() {
@Override
public void onItemSelected(MultiSelectSpinner parent, int position, String item) {
if (!selectedItems.contains(item)) {
selectedItems.add(item);
}
}
@Override
public void onNothingSelected(MultiSelectSpinner parent) {
}
});
}
}
這樣,你就可以在Android Spinner中實現多選功能了。