溫馨提示×

使用spinner adapter實(shí)現(xiàn)多級聯(lián)動選擇

小樊
82
2024-09-02 15:52:20
欄目: 編程語言

要實(shí)現(xiàn)多級聯(lián)動選擇,你可以使用Spinner和Adapter。以下是一個簡單的示例,展示了如何使用Spinner Adapter實(shí)現(xiàn)兩級聯(lián)動選擇:

  1. 首先,在activity_main.xml布局文件中添加兩個Spinner:
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context=".MainActivity">

    <Spinner
        android:id="@+id/spinner_level1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spinner_level2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp" />

</LinearLayout>
  1. MainActivity.java中,創(chuàng)建兩個Spinner并設(shè)置適配器:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity {

    private Spinner spinnerLevel1;
    private Spinner spinnerLevel2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinnerLevel1 = findViewById(R.id.spinner_level1);
        spinnerLevel2 = findViewById(R.id.spinner_level2);

        // 設(shè)置第一級Spinner的數(shù)據(jù)
        ArrayAdapter<String> level1Adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, new String[]{"A", "B", "C"});
        level1Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerLevel1.setAdapter(level1Adapter);

        // 設(shè)置第二級Spinner的數(shù)據(jù)
        final ArrayAdapter<String> level2Adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item);
        level2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerLevel2.setAdapter(level2Adapter);

        // 設(shè)置第一級Spinner的選擇監(jiān)聽器
        spinnerLevel1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 根據(jù)第一級Spinner的選擇更新第二級Spinner的數(shù)據(jù)
                String selectedItem = parent.getItemAtPosition(position).toString();
                if (selectedItem.equals("A")) {
                    level2Adapter.clear();
                    level2Adapter.addAll(new String[]{"A1", "A2", "A3"});
                } else if (selectedItem.equals("B")) {
                    level2Adapter.clear();
                    level2Adapter.addAll(new String[]{"B1", "B2", "B3"});
                } else if (selectedItem.equals("C")) {
                    level2Adapter.clear();
                    level2Adapter.addAll(new String[]{"C1", "C2", "C3"});
                }
                level2Adapter.notifyDataSetChanged();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

這個示例中,我們創(chuàng)建了兩個Spinner,分別為spinnerLevel1spinnerLevel2。我們?yōu)?code>spinnerLevel1設(shè)置了一個包含"A"、"B"和"C"的適配器。當(dāng)用戶選擇spinnerLevel1中的一個選項(xiàng)時,我們會根據(jù)選項(xiàng)更新spinnerLevel2的數(shù)據(jù)。這樣就實(shí)現(xiàn)了兩級聯(lián)動選擇。

你可以根據(jù)需要擴(kuò)展此示例,以支持更多級別的聯(lián)動選擇。

0