在Android中,可以使用“android.widget.Spinner”類來呈現(xiàn)一個下拉框,選擇列表。
在本教程中,我們將介紹如何執(zhí)行以下任務:
打開“res/values/strings.xml” 文件,定義將在Spinner(下拉列表)顯示的項目列表。
文件 : res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">選擇一個國家</string> <string name="country_prompt">Choose a country</string> <string-array name="country_arrays"> <item>China</item> <item>United States</item> <item>Indonesia</item> <item>France</item> <item>Italy</item> <item>Singapore</item> <item>New Zealand</item> <item>India</item> </string-array> </resources>
打開 “res/layout/main.xml” 文件,添加兩個spinner 組件和一個按鈕。
文件: res/layout/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" > <Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/country_arrays" android:prompt="@string/country_prompt" /> <Spinner android:id="@+id/spinner2" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnSubmit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Submit" /> </LinearLayout>
閱讀代碼以及代碼的注釋,應該是很容易理解的。
文件: MyAndroidAppActivity.java
package com.yiibai.spinnerdemo; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.Toast; public class MyAndroidAppActivity extends Activity { private Spinner spinner1, spinner2; private Button btnSubmit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addItemsOnSpinner2(); addListenerOnButton(); addListenerOnSpinnerItemSelection(); } // add items into spinner dynamically public void addItemsOnSpinner2() { spinner2 = (Spinner) findViewById(R.id.spinner2); List<String> list = new ArrayList<String>(); list.add("list 1"); list.add("list 2"); list.add("list 3"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner2.setAdapter(dataAdapter); } public void addListenerOnSpinnerItemSelection() { spinner1 = (Spinner) findViewById(R.id.spinner1); spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener()); } // get the selected dropdown list value public void addListenerOnButton() { spinner1 = (Spinner) findViewById(R.id.spinner1); spinner2 = (Spinner) findViewById(R.id.spinner2); btnSubmit = (Button) findViewById(R.id.btnSubmit); btnSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MyAndroidAppActivity.this, "OnClickListener : " + " Spinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + " Spinner 2 : "+ String.valueOf(spinner2.getSelectedItem()), Toast.LENGTH_SHORT).show(); } }); } }
文件: CustomOnItemSelectedListener.java
package com.yiibai.spinnerdemo; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.Toast; public class CustomOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) { Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }
運行該應用程序。
1. 結果,顯示兩個spinner:
2. 從spinner1選擇“France”,項目選擇監(jiān)聽器被觸發(fā):
3. 從 spinner2 選擇 “list2”,并點擊提交按鈕:
代碼下載地址:http://pan.baidu.com/s/1dD3ZzvN