標籤:android style blog http color io os 使用 java
Spinner 類圖
android.widget 類 Spinnerjava.lang.Object android.view.View android.view.ViewGroup android.widget.AdapterView<SpinnerAdapter> android.widget.AbsSpinner android.widget.Spinner
Spinner 意指下拉式清單組件。
以下為android官網文檔內容。
布局檔案中加入Spinner元素標籤
<Spinner android:id="@+id/planets_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" />
To populate the spinner with a list of choices, you then need to specify a SpinnerAdapter in your Activity orFragment source code.
為了給spinner增加選擇項,你需要在你的Activity或者Fragment中使用一個特定的SpinnerAdapter對象作為資料來源綁定到Spinner對象上。
The choices you provide for the spinner can come from any source, but must be provided through anSpinnerAdapter, such as an ArrayAdapter if the choices are available in an array or a CursorAdapter if the choices are available from a database query.
雖然你可以給spinner提供任一資料來源,但是這種資料來源必須是實現SpinnerAdapter介面的。例如ArrayAdapter。如果選擇從資料庫中查詢得到資料,那麼你可以使用CursorAdapter這種Adapter作為資料來源。
可以使用string數組。
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> <item>Jupiter</item> <item>Saturn</item> <item>Uranus</item> <item>Neptune</item> </string-array></resources>
With an array such as this one, you can use the following code in your Activity or Fragment to supply the spinner with the array using an instance of ArrayAdapter:
如果使用string數組,你需要在你的Activity或者Fragment中使用ArrayAdapter。
Spinner spinner = (Spinner) findViewById(R.id.spinner);// Create an ArrayAdapter using the string array and a default spinner layoutArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, android.R.layout.simple_spinner_item);// Specify the layout to use when the list of choices appearsadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);// Apply the adapter to the spinnerspinner.setAdapter(adapter);
靜態方法 createFromResource(Context context, int textArrayResId, int textViewResId )
參數context 這裡使用this 意指Activity
參數textArrayResId 這裡使用R.array.plants_array 資料來源
參數textViewResId 這裡使用android.R.layout.simple_spinner_item(android提供 也可以自己定製) Spinner的配置樣式
You should then call setDropDownViewResource(int) to specify the layout the adapter should use to display the list of spinner choices (simple_spinner_dropdown_item is another standard layout defined by the platform).
可以調用 setDropDownViewResource(int) 去指定spinner列表中選項的布局和樣式。
如何響應使用者的選擇,實現OnItemSelectedListener介面
public class SpinnerActivity extends Activity implements OnItemSelectedListener { ... public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { // An item was selected. You can retrieve the selected item using // parent.getItemAtPosition(pos) } public void onNothingSelected(AdapterView<?> parent) { // Another interface callback }}
The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected()callback methods.
其中此介面需要實現其中的兩個方法
Then you need to specify the interface implementation by calling setOnItemSelectedListener():(指定spinner對象實現OnItemSelectedListener介面)
Spinner spinner = (Spinner) findViewById(R.id.spinner);spinner.setOnItemSelectedListener(this);
Android Spinner控制項詳解