鍍金池/ 教程/ Android/ 百戰(zhàn)經(jīng)典第二戰(zhàn)-好玩的Spinner控件
百戰(zhàn)經(jīng)典第二十戰(zhàn)-ListView中點擊button跳轉(zhuǎn)到撥號界面實例
百戰(zhàn)經(jīng)典第十一戰(zhàn)-GridView動態(tài)添加Item
百戰(zhàn)經(jīng)典第二戰(zhàn)-好玩的Spinner控件
百戰(zhàn)經(jīng)典第五戰(zhàn)-各種對話框Dialog精彩薈萃
百戰(zhàn)經(jīng)典第八戰(zhàn)-BitmapFactory.Options對資源圖片進行縮放
百戰(zhàn)經(jīng)典第四戰(zhàn)-玩轉(zhuǎn)ListView
百戰(zhàn)經(jīng)典第十五-竊聽風云之短信監(jiān)聽
前言
百戰(zhàn)經(jīng)典第十四戰(zhàn)-網(wǎng)絡交互,基于Baas用戶表查詢功能實現(xiàn)
百戰(zhàn)經(jīng)典第九戰(zhàn)-ViewFlipper實現(xiàn)幻燈效果
百戰(zhàn)經(jīng)典第三戰(zhàn)-實現(xiàn)畫圖板
百戰(zhàn)經(jīng)典第十七戰(zhàn)-基于加速度傳感器的搖一搖功能實例
百戰(zhàn)經(jīng)典第十戰(zhàn)-LayoutAnimation布局動畫效果
百戰(zhàn)經(jīng)典第七戰(zhàn)-顯示倒計時的Button按鈕
百戰(zhàn)經(jīng)典第六戰(zhàn)-Activity啟動模式小樣
百戰(zhàn)經(jīng)典第十二戰(zhàn)-GridView動態(tài)刪除Item
百戰(zhàn)經(jīng)典第十六戰(zhàn)-圖片或頭像設置功能
百戰(zhàn)經(jīng)典第十九戰(zhàn)-短信監(jiān)聽實現(xiàn)驗證碼自動填入
百戰(zhàn)經(jīng)典第一戰(zhàn)—聽話的TextView
百戰(zhàn)經(jīng)典第十八戰(zhàn)-自定義控件實現(xiàn)一鍵清空輸入框
百戰(zhàn)經(jīng)典第十三戰(zhàn)-網(wǎng)絡交互,基于Baas實現(xiàn)用戶注冊功能

百戰(zhàn)經(jīng)典第二戰(zhàn)-好玩的Spinner控件

下拉列表框是一種常見的圖形組件,與其他選擇組件相比,可以有效的節(jié)省屏幕空間,在Android中可以使用android.widget.Spinner類來實現(xiàn)。

下拉列表框中的列表項主要有以下兩種配置方式。

方式一、通過資源文件配置,例如定義一個values\city_data.xml的文件,在定義數(shù)據(jù)內(nèi)容時需要使用<string-array>元素指定,定義內(nèi)容如下:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <string-array name="city_labels">  
        <item>北京 </item>  
        <item>上海 </item>  
        <item>廣州 </item>  
        <item>深圳 </item>  
    </string-array>  
</resources>

方式二、通過android.widget.ArrayAdapter類讀取資源文件或者指定具體的數(shù)據(jù)。

方式一實現(xiàn)

定義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/text"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="請選擇您喜歡的城市:" />  
    <Spinner  
        android:id="@+id/spinner"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:entries="@array/city_labels" ><!-- 載入數(shù)據(jù) -->  
    </Spinner>  
</LinearLayout>  

運行模擬器:

這里寫圖片描述

這時可以看到數(shù)據(jù)已經(jīng)添加到Spinner中,我們發(fā)現(xiàn)這時的Spinner控件只是徒有其表,沒有什么交互,下面實現(xiàn)交互,讓代碼更有趣。改動main.xml文件,添加一個TextView用于信息提示,一個TextView用于選擇信息的顯示。代碼如下:

<?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:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="請選擇您喜歡的城市:" />  
    <Spinner  
        android:id="@+id/spinner"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:entries="@array/city_labels" > <!-- 載入數(shù)據(jù) -->  
    </Spinner>  
    <TextView  
        android:id="@+id/text"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" />  
</LinearLayout>  

再次編輯 MainActivity.java,添加監(jiān)聽:

package org.yayun.demo;  
//省略導入包
public class MainActivity extends Activity {  
    private Spinner spinner;  
    private TextView textView;  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); // 生命周期方法  
        super.setContentView(R.layout.main); // 設置要使用的布局管理器  
        spinner = (Spinner) findViewById(R.id.spinner);  
        textView = (TextView) findViewById(R.id.text);  
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  
            public void onItemSelected(AdapterView<?> parent, View view,  
                    int position, long id) {  
                String[] cities = getResources().getStringArray(  
                        R.array.city_labels);//獲取列表數(shù)據(jù)  
                textView.setText("您喜歡的城市是:" + cities[position]);//顯示選擇項  
            }  
            public void onNothingSelected(AdapterView<?> parent) {  
                    //什么都沒選觸發(fā)
            }  
        });  
    }  
}  

實現(xiàn)了OnItemSelectedListener接口,并覆寫了里面的onItemSelected方法,里面的position參數(shù)決定了單擊的是哪一些,借助這個position就可以獲取字符串數(shù)組中的字符串,將字符串信息通過TextView顯示出來。 運行實例如下:

這里寫圖片描述

可以選擇Spinner中的選項“廣州”,選項將打印在TextView上,互動性就體現(xiàn)出來了,小實例也就有應用的價值了。

方式二實現(xiàn)

修改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:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:text="請選擇您喜歡的城市:" />  
    <Spinner  
        android:id="@+id/spinner"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content"  
        android:entries="@array/city_labels" > <!-- 載入數(shù)據(jù) -->  
    </Spinner>  
    <TextView  
        android:id="@+id/text"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" />  
    <Spinner  
        android:id="@+id/spinnerCountry"  
        android:layout_width="fill_parent"  
        android:layout_height="wrap_content" > <!-- 動態(tài)載入數(shù)據(jù) -->  
    </Spinner>  
</LinearLayout> 

MainActivity.java:

package org.yayun.demo;  
//省略導入包
public class MainActivity extends Activity {  
    private Spinner spinner, spinnerCountry;  
    private TextView textView;  
    private List<CharSequence> data = null;  
    private ArrayAdapter<CharSequence> adapter;  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState); // 生命周期方法  
        super.setContentView(R.layout.main); // 設置要使用的布局管理器  
        spinner = (Spinner) findViewById(R.id.spinner);  
        textView = (TextView) findViewById(R.id.text);  
        spinnerCountry = (Spinner) findViewById(R.id.spinnerCountry);  
        spinnerCountry.setPrompt("選擇國籍:");// 在列表中顯示  
        data = new ArrayList<CharSequence>();  
        data.add("中國");  
        data.add("美國");  
        data.add("日本");  
        adapter = new ArrayAdapter<CharSequence>(this,  
                android.R.layout.simple_spinner_dropdown_item, this.data);//定義下拉列表  
        spinnerCountry.setAdapter(adapter);  
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {  

            public void onItemSelected(AdapterView<?> parent, View view,  
                    int position, long id) {  
                String[] cities = getResources().getStringArray(  
                        R.array.city_labels);// 獲取列表數(shù)據(jù)  
                textView.setText("您喜歡的城市是:" + cities[position]);// 顯示  

            }  
            public void onNothingSelected(AdapterView<?> parent) {  
            }  
        });  
        spinnerCountry.setOnItemSelectedListener(new OnItemSelectedListener() {  
            public void onItemSelected(AdapterView<?> parent, View view,  
                    int position, long id) {  
                String[] countries = data.toArray(new String[data.size()]);// 獲取列表數(shù)據(jù)  
                Toast.makeText(MainActivity.this, "您的國籍是:"+countries[position], Toast.LENGTH_SHORT).show();         
            }  
            public void onNothingSelected(AdapterView<?> parent) {        
            }  
        });  
    }  
} 

setPrompt()方法可以在下拉控件的上欄顯示提示信息,就不用單獨的TextView提示了。這里還用到了ArrayAdapter動態(tài)填充了第二個下拉控件的數(shù)據(jù),更為靈活。

運行實例如下:

這里寫圖片描述

總結

  1. 定義數(shù)據(jù)內(nèi)容時需要使用<string-array>元素指定;
  2. android:entries="@array/city_labels"載入文本資源;
  3. String[] cities = getResources().getStringArray(R.array.city_labels);//獲取資源數(shù)據(jù)的方法
  4. String 和 CharSequence 關系 String 繼承于CharSequence,也就是說String也是CharSequence類型。 CharSequence是一個接口,它只包括 length(), charAt(int index), subSequence(int start, int end)這幾個API接口。除了String實現(xiàn)了CharSequence之外,StringBuffer和StringBuilder也實現(xiàn)了CharSequence接口。 需要說明的是,CharSequence就是字符序列,String, StringBuilder和StringBuffer本質(zhì)上都是通過字符數(shù)組實現(xiàn)的!
  5. 提示信息的設置:spinnerCountry.setPrompt("選擇國籍:");// 在列表中顯示
  6. 此外可以用adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//來設置顯示風格