(1)Spinner的OnItemSelectedListener事件處理
(2)Spinner與setDropDownViewResource應用
(3)需要2個layout,1個layout放置spinner,另一個layout則顯示被setDropDownView自訂樣式選單
(4)其畫面如下
package COM.TQC.GDD01; 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 GDD01 extends Activity { private String[] types={"請選擇","HTC HERO","HTC MAGIC","HTC TATTOO", "NEXUS ONE","SONY X10","MOTO MILESTONE"}; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final TextView tv = (TextView)findViewById(R.id.text1); Spinner sp = (Spinner)findViewById(R.id.mySpinner); //建立一個ArrayAdapter物件,並放置下拉選單的內容 ArrayAdapteradapter = new ArrayAdapter (GDD01.this,android.R.layout.simple_spinner_item,types); //設定下拉選單的樣式simple_spinner_dropdown_item adapter.setDropDownViewResource(R.layout.myspinner_layout); sp.setAdapter(adapter); sp.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){ public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if(!types[arg2].equals("請選擇")) { tv.setText(getString(R.string.str1)+types[arg2]); }else{ tv.setText(getString(R.string.str1)); } } public void onNothingSelected(AdapterView arg0) { // TODO Auto-generated method stub } }); } }