步驟:
1. 在字串資源檔 strings.xml 中建立一個字串陣列,再把這個字串陣列設定給Spinner元件成為選單
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MyCardEmulation</string>
...
<string array name="sex_list">
<item>Male</item>
<item>Female</item>
</string array>
<string name="account_id_txt">Account Number:</string>
</resources>
2. 在 介面布局檔 layout.xml 中加入一個Spinner介面元件
<Spinner
android:id="@+id/spnSex"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/sex_list"
android:spinnerMode="dialog" // dialog or dropdown android:prompt="@string/spn_sex_list_prompt" /> // prompt為 mode為dialog時使用
3. 在程式檔中取得這個 Spinner元件,並為它建立一個 OnItemSeclectedListener()事件處理程序
private Spinner mSpnSex;
private String msSex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
mSpnSex = (Spinner) findViewById(R.id.spnSex);
mSpnSex.setOnItemSelectedListener(spnSexOnItemSelected);
}
private AdapterView.OnItemSelectedListener spnSexOnItemSelected =
new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
msSex = parent.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
};
沒有留言:
張貼留言