Spinner 加上 ArrayAdapter 的基本使用
1.MainActivity.java
import java.util.Arrays; import java.util.List; 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 MainActivity extends Activity { private Spinner mSpinner; private String[] mSpinnerItems = new String[] { "item1", "item2", "item3" }; private TextView mShowSelectedItemInSpinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initLayout(); } private void initLayout() { mShowSelectedItemInSpinner = (TextView) findViewById(R.id.selected_item); initSpinner(); } private void initSpinner() { mSpinner = (Spinner) findViewById(R.id.exam_spinner); ArrayAdapter adapter = createArrayAdapterByStringArrayXml(); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); mSpinner.setAdapter(adapter); mSpinner.setOnItemSelectedListener(new ItemSelectedListener()); } private void showSelectedItem() { Object selectItem = mSpinner.getSelectedItem().toString(); for (String item : mSpinnerItems) { if (selectItem.equals(item)) { mShowSelectedItemInSpinner.setText(item); break; } } } private ArrayAdapter<String> getArrayAdapterByStringArray() { return new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mSpinnerItems); } private ArrayAdapter<String> getArrayAdapterByStringArrayXml() { String[] items = getResources().getStringArray(R.array.spinner_items); return new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); } private ArrayAdapter<CharSequence> createArrayAdapterByStringArrayXml() { return ArrayAdapter.createFromResource(this, R.array.spinner_items, android.R.layout.simple_spinner_item); } private ArrayAdapter<String> getArrayAdapterByList() { List<String> itemList = Arrays.asList(mSpinnerItems); return new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, itemList); } private final class ItemSelectedListener implements OnItemSelectedListener { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { showSelectedItem(); } @Override public void onNothingSelected(AdapterView<?> parent) { } } }
Note:
注意各種不同取得ArrayAdapter的方式
getArrayAdapterByStringArray() 直接使用Hard Coding帶入Spinner item, android.R.layout.simple_spinner_item為 android 預設的 spinner layout
getArrayAdapterByStringArrayXml() 使用Soft Coding帶入Spinner itme, 其中item定義在R.array.spinner_items中
createArrayAdapterByStringArrayXml() 也是使用Soft Coding帶入Spinner itme
getArrayAdapterByList() 也可以使用 List 帶入 spinner item
這裡只列出常用的幾種,其他請參考官網
setDropDownViewResource() 用來設定點擊Spinner後出現的下拉式選單
ItemSelectedListener類別則是用來監聽Spinner選擇的Item
2./res/values/arrays.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="spinner_items"> <item>item1</item> <item>item2</item> <item>item3</item> </string-array> </resources>
Note:
使用Soft Coding定義Spinner Items
3.activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Spinner android:id="@+id/exam_spinner" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/selected_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/exam_spinner" android:layout_marginLeft="50dp" android:layout_marginTop="20dp"/> </RelativeLayout>