1. 程式人生 > >Spinner與介面卡Adapter使用總結

Spinner與介面卡Adapter使用總結

Spinner的使用離不開和Adapter結合使用,下面將基本的常用法介紹一下,主要還是為初學者提供些幫助

第一步:建立佈局檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@+id/mylinearlayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<Spinner  
	android:id="@+id/spinner1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:entries="@array/countrys"
    />
 <Spinner  
	android:id="@+id/spinner2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
  <Spinner  
	android:id="@+id/spinner3"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
   <Spinner  
	android:id="@+id/spinner4"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />

</LinearLayout>

第二步:在資原始檔中定義如下:

    <string-array name="countrys">
       <item>aaa</item>
       <item>bbb</item>
       <item>ccc</item>
       <item>ddd</item>
    </string-array>

第三步:寫測試類和Spinner結合使用

public class AndroidPracticeTestActivity extends Activity {	
	private Spinner spinner1=null;
	private Spinner spinner2=null;
	private Spinner spinner3=null;
	private Spinner spinner4=null;
	private String obj[]={"abc","cbd","acd","efg"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner_main);
        //獲取Spinner物件
        spinner1=(Spinner)findViewById(R.id.spinner1);
        spinner2=(Spinner)findViewById(R.id.spinner2);
        spinner3=(Spinner)findViewById(R.id.spinner3);
        spinner4=(Spinner)findViewById(R.id.spinner4);
        //Spinner新增監聽
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				System.out.println("spinner1arg3="+arg3);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
        
        //定義介面卡
        ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, obj);
        adapter2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				System.out.println("spinner2arg3="+arg3);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
        //定義介面卡
        ArrayAdapter<CharSequence> adapter3=ArrayAdapter.createFromResource(this, R.array.countrys, android.R.layout.simple_list_item_1);
        adapter3.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        spinner3.setAdapter(adapter3);
        spinner3.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				System.out.println("spinner3arg3="+arg3);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
        //自定義介面卡
        BaseAdapter ma= new BaseAdapter(){

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return 5;
			}

			@Override
			public Object getItem(int arg0) {
				if(arg0==0){
					return "a";
				}else if(arg0==1){
					return "b";
				}else if(arg0==2){
					return "c";
					
				}else if(arg0==3){
					return "d";
					
				}else if(arg0==4){
					return "e";
				}else{
					return "jjj";
				}
			}

			@Override
			public long getItemId(int arg0) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				TextView tv=new TextView(AndroidPracticeTestActivity.this);
				tv.setText((String)getItem(position));
				tv.setTextColor(Color.RED);
				return tv;
			}
        	
        };
        spinner4.setAdapter(ma);
    }
    
    
}
第四步:演示效果