安卓Spinner的三级联动菜单效果

今天练习了一个spinner三级联动的效果,下面是代码详情,希望能对其他人有帮助:

MainActivity.java

public class MyActivity extends Activity {
    
    private String county[]=new String[]{"美国","中国","日本"};
    
    private String provice[][] = new String[][] {
            { "洛杉矶", "檀香山", "纽约", "华盛顿" }, { "北京", "上海", "山东", "广东" },
            { "东京", "八户", "秋田", "青森" } };

    private String[][][] city = new String[][][] {
            { { "洛杉矶1", "洛杉矶2", "洛杉矶3" }, { "檀香山1", "檀香山2", "檀香山3" },
                    { "纽约1", "纽约2", "纽约3" } },
            { { "东城", "朝阳", "大兴", "天安门" }, { "徐泾东", "浦东", "陆家嘴" },
                    { "郓城", "牡丹区", "曹县" } },
            { { "东京1", "东京2", "东京3" }, { "八户1", "八户2", "八户3" },
                    { "秋田1", "秋田2", "秋田3" }, { "秋1", "秋2", "秋3" } }

    };

    static int post = 1;

    private Spinner countrySp;
    private Spinner provinveSp;
    private Spinner citySp;

    private ArrayAdapter countyAda = null;
    private ArrayAdapter proviceAda = null;
    private ArrayAdapter cityAda = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        countrySp = (Spinner) this.findViewById(R.id.sp_country);
        provinveSp = (Spinner) this.findViewById(R.id.sp_province);
        citySp = (Spinner) this.findViewById(R.id.sp_city);
        setAdapter();
        SpinnerListener();
    }

    private void SpinnerListener() {
        countrySp.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView arg0, View view,
                    int position, long id) {
                proviceAda = new ArrayAdapter(MyActivity.this,
                        android.R.layout.simple_list_item_1, provice[position]);
                provinveSp.setAdapter(proviceAda);
                post = position;
            }

            @Override
            public void onNothingSelected(AdapterView arg0) {
            }
        });
        
        provinveSp.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView arg0, View arg1,
                    int arg2, long arg3) {
                cityAda = new ArrayAdapter(MyActivity.this,
                        android.R.layout.simple_list_item_1, city[post][arg2]);
                citySp.setAdapter(cityAda);
            }

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

    private void setAdapter() {
        countyAda = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1,county);
        countrySp.setAdapter(countyAda);

        proviceAda = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, provice[1]);
        provinveSp.setAdapter(proviceAda);
        provinveSp.setSelection(0, true);
        cityAda = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, city[1][1]);
        citySp.setAdapter(cityAda);
        citySp.setSelection(0, true);
    }

Layout布局


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

            android:id="@+id/sp_country"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/country" />

            android:id="@+id/sp_province"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

            android:id="@+id/sp_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


怎么样,是不是很简单啊,如果觉得对你有帮助,请关注我的博客,我会不间断的上传小案例代码,你们的支持是我最大的鼓励!







你可能感兴趣的:(Android)