TQC-108 下拉式選單

  • 2143
  • 0

TQC-108 下拉式選單

這是TQC的108題,

程式碼為自行撰寫,

僅供參考。

 

主要練習下拉式選單,及setOnItemSelectedListener事件


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;

public class GDD01 extends Activity {
  private String[] types={"請選擇1","HTC HERO","HTC MAGIC","HTC TATTOO",
      "NEXUS ONE","SONY X10","MOTO MILESTONE"};
  
  Spinner spinner;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);    
    findview();
    setspinner();
  }
  
  public void findview()
  {
	  spinner = (Spinner) findViewById(R.id.mySpinner);
  }
  public void setspinner()
  {
	  ArrayAdapter adapter=new ArrayAdapter (this,android.R.layout.simple_spinner_item,types);
	  adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
	  spinner.setAdapter(adapter);
	  
	  spinner.setPrompt("Phone"); 
	  spinner.setOnItemSelectedListener( new OnItemSelectedListener(){

		@Override
		public void onItemSelected(AdapterView arg0, View arg1, int arg2,
				long arg3) {
			String selected = arg0.getItemAtPosition(arg2) .toString();
				   System.out.println(selected);
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onNothingSelected(AdapterView arg0) {
			// TODO Auto-generated method stub
			
		}});
  }
  
}

 

 

自我LV~