android中下拉框控件——Spinner

android中,用spinner控件很容易实现下拉框的功能。

下面是具体实现的步骤:

  1. 在layout中添加spinner控件:
        <Spinner 
            android:id="@+id/SpinnerAPDU" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            
        </Spinner>

  2. 在Activity中添加如下代码:
    	private static final String[] mAPDU={"0x00, 0x84, 0x00, 0x00, 0x08 ","0x00, 0xc0, 0x00, 0x00, 0x0a","..."};
    	private Spinner spinner;
    	private ArrayAdapter<String> adapter;
    	private static int mApduIndex = -1;
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);  
            setContentView(R.layout.iccard);
            mQuery = (EditText) findViewById(R.id.textViewQuery);
            mRuternData = (EditText) findViewById(R.id.textViewRuternData);
            spinner = (Spinner) findViewById(R.id.SpinnerAPDU);
    	//将可选内容与ArrayAdapter连接起来
    	adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mAPDU);
    		
    	//设置下拉列表的风格
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    		
    	//将adapter 添加到spinner中
    	spinner.setAdapter(adapter);
    		
    	//添加事件Spinner事件监听  
    	spinner.setOnItemSelectedListener(new SpinnerSelectedListener(){});
    		
    	//设置默认值
    	spinner.setVisibility(View.VISIBLE);
    
    	
    	//使用数组形式操作
    	abstract class SpinnerSelectedListener implements OnItemSelectedListener{
    
    		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
    				long arg3) {
    			mApduIndex = arg2;
    		}
    
    		public void onNothingSelected(AdapterView<?> arg0) {
    		}
    	}
    
    	
    
    
    


你可能感兴趣的:(android中下拉框控件——Spinner)