Android 之 下拉框(Spinner)的使用

doomvsjing 2012-01-07

参考:http://imshare.iteye.com/blog/770950

获得被选中的选项的文本,Index,ID

String value =  spinner.getSelectedItem().toString();
                int index = spinner.getSelectedItemPosition();
                long id = spinner.getSelectedItemId();

第一个下拉框是在java里面设定数据

第二个是在xml提供数据:res/values/arryas.xml

第三个Spinner添加了OnItemSelectedListener事件,当单击下拉菜单后,将值带到上方的Text-View。上一个范例在newadapter时传入String数组,这次因为要添加及删除adapter,所以要传入的是ArrayList,否则,在添加删除时会出现错误。

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="plantes">
        <item>NOKIA</item>
        <item>MOTO</item>
        <item>HTC</item>
        <item>LG</item>
        <item>其他</item>
    </string-array>

</resources>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/spinnerText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextView>

    <Spinner
        android:id="@+id/Spinner01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </Spinner>
    
    <Spinner
        android:id="@+id/spinner02"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </Spinner>
    
    <Spinner
        android:id="@+id/spinner03"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </Spinner>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AddItem" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RemoverItem" />

</LinearLayout>

java代码:

package com.pandy;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class UI4Activity extends Activity {
    /** Called when the activity is first created. */
	private static final String[] m={"A型","B型","O型","AB型","其他"};  
    private TextView view ;  
    
    private Spinner spinner;  
    private ArrayAdapter<String> adapter;  
    
    private Spinner spinner2;  
    private ArrayAdapter adapter2;  
    
    private Button button1;
    private Button button2;
    
    private static List<String> list; 
    private ArrayAdapter<String> adapter3; 
    private Spinner spinner3; 
    
    private int count = 1;
    
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        // TODO Auto-generated method stub  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        view = (TextView) findViewById(R.id.spinnerText);  
        spinner = (Spinner) findViewById(R.id.Spinner01);  
        //将可选内容与ArrayAdapter连接起来  
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,m);  
        //设置下拉列表的风格  
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
        //将adapter 添加到spinner中  
        spinner.setAdapter(adapter);  
        //添加事件Spinner事件监听    
        spinner.setOnItemSelectedListener(new SpinnerSelectedListener());  
        //设置默认值  
        spinner.setVisibility(View.VISIBLE);  
        
        
        
        spinner2 = (Spinner)findViewById(R.id.spinner02);
        adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener());
        spinner2.setVisibility(View.VISIBLE);
        
        
        
        list = new ArrayList<String>();
        for(String s : getResources().getStringArray(R.array.plantes)){
        	list.add(s);
        }
        adapter3 = new ArrayAdapter(this,android.R.layout.simple_spinner_item,list);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner3 = (Spinner)findViewById(R.id.spinner03);
        spinner3.setAdapter(adapter3);
        spinner3.setVisibility(View.VISIBLE);
        
        button1 = (Button)findViewById(R.id.button1);
        button2 = (Button)findViewById(R.id.button2);
        button1.setOnClickListener(new OnClickListener(){
        	@Override
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		try {
        			adapter3.add("Add Item "+(count++));
				} catch (Exception e) {
					// TODO: handle exception
					Toast.makeText(UI4Activity.this, "Error.", Toast.LENGTH_LONG).show();
				}
        	}
        });
        button2.setOnClickListener(new OnClickListener(){
        	@Override
        	public void onClick(View v) {
        		// TODO Auto-generated method stub
        		try {
        			adapter3.remove(spinner3.getSelectedItem().toString());
				} catch (Exception e) {
					// TODO: handle exception
					Toast.makeText(UI4Activity.this, "Error.", Toast.LENGTH_LONG).show();
				}
        		
        	}
        });
        
        
          
    }  
      
    //使用数组形式操作  
    class SpinnerSelectedListener implements OnItemSelectedListener{  
  
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,  
                long arg3) {  
            view.setText("你的血型是:"+m[arg2]);  
        }  
        public void onNothingSelected(AdapterView<?> arg0) {  
        }  
    } 
    class SpinnerXMLSelectedListener implements OnItemSelectedListener{    
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,    
                long arg3) {    
        	view.setText("你使用什么样的手机:"+adapter2.getItem(arg2));    
        }    
    
        public void onNothingSelected(AdapterView<?> arg0) {    
                
        }    
            
    } 
}

相关推荐