1. 程式人生 > >Android開發(21)--有關Spinner控制元件的使用說明

Android開發(21)--有關Spinner控制元件的使用說明

下拉列表 Spinner,Spinner是一個每次只能選擇所有項的一個項的控制元件。它的項來自於與之相關聯的介面卡中。
Spinner的使用,可以極大提高使用者的體驗性。當需要使用者選擇的時候,可以提供一個下拉列表將所有可選的項列出來。供使用者選擇。

一、使用陣列作為資料來源

佈局檔案:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

MainActivity.java

package com.example.lesson16_spinner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class MainActivity extends Activity {
	// 宣告spinner物件
	private Spinner spinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		spinner = (Spinner) findViewById(R.id.spinner1);
		//使用陣列作為資料來源 
		final String arr[] = new String[] { "週一", "週二", "週三", "週四", "週五", "週六",
				"周七" };
		// adpater物件
		 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);

	
		List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("logo", R.drawable.ic_launcher);
		map.put("text", "xxxx說明操作");

		Map<String, Object> map2 = new HashMap<String, Object>();

		map2.put("logo", R.drawable.ic_launcher);
		map2.put("text", "xxx說明xxx");

		data.add(map);
		data.add(map2);

		SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
				R.layout.activity_item, new String[] { "logo", "text" },
				new int[] { R.id.imageView1, R.id.textView1 });
		// 設定顯示的資料
		 spinner.setAdapter(arrayAdapter);

		//spinner.setAdapter(simpleAdapter);

		Toast.makeText(getApplicationContext(),"main Thread"+ spinner.getItemAtPosition(spinner.getSelectedItemPosition()), Toast.LENGTH_LONG).show();

		// 註冊事件
		spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {

				// String content = arr[position];

				Spinner spinner = (Spinner) parent;

				Toast.makeText(getApplicationContext(),
						"xxxx" + spinner.getItemAtPosition(position),
						Toast.LENGTH_LONG).show();

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				Toast.makeText(getApplicationContext(), "沒有改變的處理",
						Toast.LENGTH_LONG).show();

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

效果如下:


二、使用XML作為資料來源(非本人程式碼)

佈局檔案:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<string-array name="plantes">
		<item>NOKIA</item>
		<item>MOTO</item>
		<item>HTC</item>
		<item>LG</item>
		<item>其他</item>
	</string-array>
</resources>


java程式碼:

import android.app.Activity;
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.TextView;

public class SpinnerActivity extends Activity {
	
	private TextView view2;
	private Spinner spinner2;
	private ArrayAdapter adapter2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.spinner);

		spinner2 = (Spinner) findViewById(R.id.spinner02);
		view2 = (TextView) findViewById(R.id.spinnerText02);

		//將可選內容與ArrayAdapter連線起來
		adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item);

		//設定下拉列表的風格	
 adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		//將adapter2 新增到spinner中
		spinner2.setAdapter(adapter2);

		//新增事件Spinner事件監聽  
		spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener());

		//設定預設值
		spinner2.setVisibility(View.VISIBLE);
		
	}
	
	//使用XML形式操作
	class SpinnerXMLSelectedListener implements OnItemSelectedListener{
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			view2.setText("你使用什麼樣的手機:"+adapter2.getItem(arg2));
		}

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