public class calculateActivity extends Activity {
    
    private Spinner input_v;
    private final static String[] option_vs={"110V","220V"};//Spinner內要放的項目
    private ArrayAdapter adapter;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        
        input_v=(Spinner) findViewById(R.id.input_v);
        
        
        adapter=new ArrayAdapter(this,android.R.layout.simple_spinner_item,option_vs);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        input_v.setAdapter(adapter);
        
    //如果要用到Spinner選取的項目用 input_v.getSelectedItem()  
 
    }
            
       
}

-------------------如果要監聽item選取事件-----------------------------------

unit_spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
		@Override
		public void onItemSelected(AdapterView arg0, View arg1,
		int which, long arg3) {
		
			textview_unit.setText(unit_spinner.getSelectedItem().toString());
		
		}
		
		@Override
		public void onNothingSelected(AdapterView arg0) {
		}
		        
});

文章標籤
創作者介紹

Mazs's Notes

cookiesp 發表在 痞客邦 PIXNET 留言(0) 人氣()