欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安卓:下拉列表spinner的联动

程序员文章站 2022-07-14 12:53:57
...

结果展示:

安卓:下拉列表spinner的联动
安卓:下拉列表spinner的联动
即点击第一个下拉列表内容时,第二个下拉列表的内容那个会根据第一个下拉列表内容改变而改变

代码部分

xml:
activity_spinner_test.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
  >
    <TextView
        android:text="请选择学院:"
        android:textSize="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Spinner
        android:id="@+id/spinnerDepartment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Spinner>
    <TextView
        android:text="请选择班级:"
        android:textSize="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Spinner
        android:id="@+id/spinnerClass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    </Spinner>
    <TextView
        android:id="@+id/txtResult"

        android:textSize="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    </LinearLayout>

item_select.xml 下拉列表初始风格

安卓:下拉列表spinner的联动

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:textSize="40dp"
    android:typeface="serif"
    android:gravity="center"
    android:textColor="#000000"
    android:layout_height="match_parent"
    android:background="@color/colorAccent"
    android:orientation="vertical">
</TextView>

item_drop.xml 列表下拉后的风格

安卓:下拉列表spinner的联动

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp"
    android:background="@color/colorPrimary"
    android:gravity="center"
    android:orientation="vertical"
    android:textColor="#000000"
    android:textSize="40dp">
</TextView>

java部分
SpinnerTestActivity.java

package com.example.firstapplication.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.example.firstapplication.R;


public class SpinnerTestActivity extends Activity {

    Spinner spinnerDepartment, spinnerClass;
    TextView txtResult;
    String[] department = {"计算机学院", "土木学院", "信电学院"};
    String[][] class1 = {{"计科151", "计科152", "计科153"}, {"土木151", "土木152", "土木153"}, {"信电151", "信电152", "信电153"}};

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner_test);

        spinnerDepartment = (Spinner) findViewById(R.id.spinnerDepartment);
        spinnerClass = (Spinner) findViewById(R.id.spinnerClass);
        txtResult = (TextView) findViewById(R.id.txtResult);

        ArrayAdapter adapterDepartment = new ArrayAdapter(SpinnerTestActivity.this, R.layout.item_select, department);
        adapterDepartment.setDropDownViewResource(R.layout.item_drop);
        spinnerDepartment.setAdapter(adapterDepartment);
        spinnerDepartment.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayAdapter adapterClass = new ArrayAdapter(SpinnerTestActivity.this, R.layout.item_select, class1[position]);
                adapterClass.setDropDownViewResource(R.layout.item_drop);
                spinnerClass.setAdapter(adapterClass);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        spinnerClass.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                txtResult.setText("你选择的是" + spinnerClass.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


    }

}


java代码分析:

1、为下拉列表设置新的下拉项目数据


//String[] department = {"计算机学院", "土木学院", "信电学院"};
//SpinnerTestActivity  java项目本身
//R.layout.item_select   R.layout_item_drop上面讲过

ArrayAdapter adapterDepartment = new ArrayAdapter(SpinnerTestActivity.this, R.layout.item_select, department);
        adapterDepartment.setDropDownViewResource(R.layout.item_drop);
        spinnerDepartment.setAdapter(adapterDepartment);

2、两个下拉列表联动关键代码块

spinnerDepartment.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                ArrayAdapter adapterClass = new ArrayAdapter(SpinnerTestActivity.this, R.layout.item_select, class1[position]);
                adapterClass.setDropDownViewResource(R.layout.item_drop);
                spinnerClass.setAdapter(adapterClass);
            }

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

3、下拉列表项目的选定

   spinnerClass.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                txtResult.setText("你选择的是" + spinnerClass.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
相关标签: java android