自定义spinner下拉框样式,并实现二级联动

public class MainActivity extends Activity {
    private Spinner provinceSpinner = null; // 省级(省、直辖市)
    private Spinner citySpinner = null; // 地级市
    ArrayAdapter provinceAdapter = null; // 省级适配器
    ArrayAdapter cityAdapter = null; // 地级适配器

    static int provincePosition = 3;

    // 省级选项值
    private String[] province = new String[] { "北京", "上海", "天津", "广东" };
    // 地级选项值
    private String[][] city = new String[][] { { "东城区", "西城区", "崇文区" },// 北京
            { "长宁区", "静安区", "普陀区", "闸北区", "虹口区" },// 上海
            { "和平区", "河东区", "河西区", "南开区", "河北区", "红桥区", "塘沽区", "汉沽区", "大港区", "东丽区" },// 天津
            { "广州", "深圳", "韶关" } // 广东
    };

    // 县级选项值
    private String[][][] county = new String[][][] { { { "无" }, { "无" }, { "无" } },// 北京
            { { "无" }, { "无" }, { "无" }, { "无" }, { "无" } },// 上海
            { { "无" }, { "无" }, { "无" }, { "无" } },// 天津
            { { "海珠区", "荔湾区" }, // 广州
                    { "宝安区", "福田区", "龙岗区" }, // 深圳
                    { "武江区", "浈江区", "曲江区", "乐昌市" } // 韶关
            } // 广东
    };

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

        setSpinner();
    }

    /*
     * 设置下拉框
     */
    private void setSpinner() {
        provinceSpinner = (Spinner) findViewById(R.id.spin_province);
        citySpinner = (Spinner) findViewById(R.id.spin_city);

        // 绑定适配器和值
        //自定义样式
        provinceAdapter = new ArrayAdapter(MainActivity.this, R.layout.spiner_item_layout, province);
        //provinceAdapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_spinner_item, province);
        provinceSpinner.setAdapter(provinceAdapter);
        provinceSpinner.setSelection(3, true); // 设置默认选中项,此处为默认选中第4个值

        cityAdapter = new ArrayAdapter(MainActivity.this,R.layout.spiner_item_layout, city[3]);
        citySpinner.setAdapter(cityAdapter);
        citySpinner.setSelection(0, true); // 默认选中第1个


        // 省级下拉框监听
        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            // 表示选项被改变的时候触发此方法,主要实现办法:动态改变地级适配器的绑定值
            @Override
            public void onItemSelected(AdapterView arg0, View arg1, int position, long arg3) {
                // position为当前省级选中的值的序号

                // 将地级适配器的值改变为city[position]中的值
                cityAdapter = new ArrayAdapter(MainActivity.this, R.layout.spiner_item_layout,
                        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) {
            }

            @Override
            public void onNothingSelected(AdapterView arg0) {

            }
        });
    }
}

布局:

<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_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="1" />


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


LinearLayout>

适配器 xml:


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dp"
    android:gravity="center_vertical"
    android:minHeight="30dp"
    android:singleLine="true"
    android:textColor="#58c1bb"
    android:textSize="15sp"
    tools:ignore="RtlHardcoded" />

千万别忘了 不然会覆盖显示框的哦

 android:theme="@android:style/Theme.Holo.Light.NoActionBar"
 或者spinner 布局中加入
  android:spinnerMode="dropdown"
                android:overlapAnchor="false"

你可能感兴趣的:(自定义spinner下拉框样式,并实现二级联动)