Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > 实现联动下拉框 下拉列表spinner

Android实现联动下拉框 下拉列表spinner的实例代码

投稿:shangke

这篇文章介绍了Android实现联动下拉框 下拉列表spinner的实例代码,有需要的朋友可以参考一下

主要实现办法:动态加载各级下拉值的适配器

在本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值

      

     

XML布局:

复制代码 代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="8dp" >
    <Spinner
        android:id="@+id/spin_province"
        android:layout_width="80dp"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spin_city"
        android:layout_width="112dp"
        android:layout_height="wrap_content" />

    <Spinner
        android:id="@+id/spin_county"
        android:layout_width="100dp"
        android:layout_height="wrap_content" />
</LinearLayout>


实现代码:

复制代码 代码如下:

package com.example.android.demo.spinner;

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

    /*
     * 设置下拉框
     */
    private void setSpinner()
    {       
        provinceSpinner = (Spinner)findViewById(R.id.spin_province);
        citySpinner = (Spinner)findViewById(R.id.spin_city);
        countySpinner = (Spinner)findViewById(R.id.spin_county);
       
        //绑定适配器和值
        provinceAdapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, province);
        provinceSpinner.setAdapter(provinceAdapter);
        provinceSpinner.setSelection(3,true);  //设置默认选中项,此处为默认选中第4个值
       
        cityAdapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, city[3]);
        citySpinner.setAdapter(cityAdapter);
        citySpinner.setSelection(0,true);  //默认选中第0个
       
        countyAdapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, county[3][0]);
        countySpinner.setAdapter(countyAdapter);
        countySpinner.setSelection(0, true);
       
       
        //省级下拉框
        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            // 表示选项被改变的时候触发此方法,主要实现办法:动态改变地级适配器的绑定值
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
            {
                //position为当前省级选中的值的序号
               
                //将地级适配器的值改变为city[position]中的值
                cityAdapter = new ArrayAdapter<String>(
                        MainActivity.this, android.R.layout.simple_spinner_item, city[position]);
                // 设置二级下拉列表的选项内容适配器
                citySpinner.setAdapter(cityAdapter);
                provincePosition = position;    //记录当前省级序号,留给下面修改县级适配器时用
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0)
            {
               
            }
           
        });
       
       
        citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
        {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3)
            {
                countyAdapter = new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_spinner_item, county[provincePosition][position]);
                countySpinner.setAdapter(countyAdapter);
            }

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


要获取下拉框spinner中选中的值,用下面这方法就OK了

provinceSpinner.getSelectedItem().toString();

citySpinner.getSelectedItem().toString();

countySpinner.getSelectedItem().toString();


THE END

阅读全文