Android 之 下拉框(Spinner)的使用


 
注意:Spinner的setAdapter方法可以設置為ArrayAdapter和BaseAdapter的子類,可以不使用體統的選擇,這里可以根據自己的喜好自行設置
 
 


一、使用數組作為數據源
 

1.新建一個android的工程 
2.工程的layout.xml文件如下:聲明一個TextView控件和一個Spinner控件 

<?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>
</LinearLayout>

 

3.java代碼 

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerActivity extends Activity {
    
    private static final String[] m={"A型","B型","O型","AB型","其他"};
    private TextView view ;
    private Spinner spinner;
    private ArrayAdapter<String> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner);
        
        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);
        
    }
    
    //使用數組形式操作
    class SpinnerSelectedListener implements OnItemSelectedListener{

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            view.setText("你的血型是:"+m[arg2]);
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
}

 

4、運行效果如下: 

 

二、使用XML作為數據源 
1.新建一個android的工程 
2.在values文件夾下新建一個arryas.xml文件:聲明一個TextView控件和一個Spinner控件,代碼如下: 

 

 

<?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>

 

3.java代碼 

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerActivity extends Activity {
    
    private TextView view2;
    private Spinner spinner2;
    private ArrayAdapter adapter2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.spinner);

        spinner2 = (Spinner) findViewById(R.id.spinner02);
        view2 = (TextView) findViewById(R.id.spinnerText02);

        //將可選內容與ArrayAdapter連接起來
        adapter2 = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item);

        //設置下拉列表的風格    
 adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        //將adapter2 添加到spinner中
        spinner2.setAdapter(adapter2);

        //添加事件Spinner事件監聽  
        spinner2.setOnItemSelectedListener(new SpinnerXMLSelectedListener());

        //設置默認值
        spinner2.setVisibility(View.VISIBLE);
        
    }
    
    //使用XML形式操作
    class SpinnerXMLSelectedListener implements OnItemSelectedListener{
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            view2.setText("你使用什么樣的手機:"+adapter2.getItem(arg2));
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            
        }
        
    }
}

 

 

 

4、運行效果如下: 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM