下拉列表---Spinner用于显示列表项,类似于一组单选按钮RadioButton。Spinner的使用,

       可以极大的提升用户的体验性。当需要用户选择的时候,可以提供一个下拉列表项给用户选择。

               具体来说下拉列表是如何实现的呢?

               通过查阅API知道Spinner继承AdapterView,因此它的数据源需要通过Adapter实现。

               一般来说Spinner的数据源可以是数组,也可以是一个XML文件。

              一、以数组作为数据源

                       这种实现方式比较简单,先上效果图:

                   xml文件代码;

                  
                MainActivity文件代码:

package com.kiritor.ui_spinner;  import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView;  public class MainActivity extends Activity {  	private TextView result = null; 	private Spinner spinner = null; 	private ArrayAdapter adapter = null; 	private static final String [] langurage ={"机器语言","汇编","c语言","c++语言","java语言"}; 	@Override 	protected void onCreate(Bundle savedInstanceState) { 		super.onCreate(savedInstanceState); 		setContentView(R.layout.activity_main); 		result = (TextView)findViewById(R.id.result); 		spinner = (Spinner)findViewById(R.id.spinner); 		adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,langurage); 		//设置下拉列表风格 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 		//将适配器添加到spinner中去 		spinner.setAdapter(adapter); 		spinner.setVisibility(View.VISIBLE);//设置默认显示 		spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 		@Override 		public void onItemSelected(AdapterView arg0, View arg1, 				int arg2, long arg3) { 			// TODO Auto-generated method stub 			result.setText("你的选择是:"+((TextView)arg1).getText()); 			 			 		} 		@Override 			public void onNothingSelected(AdapterView arg0) { 				// TODO Auto-generated method stub 				 			} 		}); 		 	}  	@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文件:

          

               机器语言         汇编语言         c语言         c++语言         java语言         Android学习      
               xml文件:

                  
                MainActivity

package com.kiritor.ui_spinner;  import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.TextView;  public class MainActivity extends Activity {  	private TextView result = null; 	private Spinner spinner = null; 	private ArrayAdapter adapter = null; 	 	@Override 	protected void onCreate(Bundle savedInstanceState) { 		super.onCreate(savedInstanceState); 		setContentView(R.layout.activity_main); 		result = (TextView)findViewById(R.id.result); 		spinner = (Spinner)findViewById(R.id.spinner); 		adapter = ArrayAdapter.createFromResource(this,R.array.langurage,android.R.layout.simple_spinner_item); 		//设置下拉列表风格 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 		//将适配器添加到spinner中去 		spinner.setAdapter(adapter); 		spinner.setVisibility(View.VISIBLE); 		spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 		@Override 		public void onItemSelected(AdapterView arg0, View arg1, 				int arg2, long arg3) { 			// TODO Auto-generated method stub 			result.setText("你的选择是:"+((TextView)arg1).getText()); 			 			 		} 		@Override 			public void onNothingSelected(AdapterView arg0) { 				// TODO Auto-generated method stub 				 			} 		}); 		 	}  	@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; 	}  } 
              今天就到这里了,至于spinner的自定义用法和一些Spinner的实际用法举例,留待明天再说!

        Over!