下拉列表框是一種常見的圖形組件,與其他選擇組件相比,可以有效的節(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ù)。
<?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)出來了,小實例也就有應用的價值了。
修改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ù),更為靈活。
運行實例如下:
<string-array>
元素指定;android:entries="@array/city_labels"
載入文本資源;String[] cities = getResources().getStringArray(R.array.city_labels);//獲取資源數(shù)據(jù)的方法
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)的!spinnerCountry.setPrompt("選擇國籍:");// 在列表中顯示
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);//來設置顯示風格