1. 程式人生 > >Android基礎----Spanner(下拉列表)

Android基礎----Spanner(下拉列表)

                          Spanner(下拉列表)


實現:activity_main.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="match_parent"
    android:orientation="horizontal"
    tools:context="${relativePackage}.${activityClass}" >

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

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

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

</LinearLayout>
package com.example.jilian_spanner;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Context context = MainActivity.this;

	private Spinner shengfen_sn, chengshi_sn, quxian_sn;

	ArrayAdapter<String> s1, s2, s3;

	private String[] shengfen = { "江西省", "江蘇省" };
	private String[][] chengshi = { { "南昌" }, { "南京" } };
	private String[][][] quxian = { { { "紅谷灘", "青山湖區" } }, { { "蘇子湖", "秦淮區" } } };

	private int shengfenposition;

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

		shengfen_sn = (Spinner) findViewById(R.id.shengfen);
		chengshi_sn = (Spinner) findViewById(R.id.chengshi);
		quxian_sn = (Spinner) findViewById(R.id.quxian);

		s1 = new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, shengfen);
		s2 = new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, chengshi[0]);
		s3 = new ArrayAdapter<String>(this,
				android.R.layout.simple_expandable_list_item_1, quxian[0][0]);

		shengfen_sn.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				s2 = new ArrayAdapter<String>(context,
						android.R.layout.simple_expandable_list_item_1,
						chengshi[position]);
				shengfenposition = position;
				chengshi_sn.setAdapter(s2);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

		chengshi_sn.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				s3 = new ArrayAdapter<String>(context,
						android.R.layout.simple_expandable_list_item_1,
						quxian[shengfenposition][position]);
				quxian_sn.setAdapter(s3);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});

		shengfen_sn.setAdapter(s1);
		chengshi_sn.setAdapter(s2);
		quxian_sn.setAdapter(s3);

	}

	public void zhengxiang(View v) {
		// TODO Auto-generated method stub
		Toast.makeText(context, "賴隊吊炸天!!!!!!", Toast.LENGTH_LONG).show();
	}

}

簡單點的就是:


主函式入口:

package com.example.xialaliebiao;

import android.app.Activity;

import com.example.xialaliebiao.R;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class MainActivity extends Activity {

    private Spinner Spinner;

    private String[] menu = { "南昌", "上海", "廣州" };

    private ArrayAdapter<String> ArrayAdapter;

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

        Spinner = (android.widget.Spinner) findViewById(R.id.spinner);

        ArrayAdapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_expandable_list_item_1, menu);

        Spinner.setAdapter(ArrayAdapter);
    }
}

佈局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

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

</RelativeLayout>