Android APP開發活用範例速查大辭典(Ch0123-使用Spinner)
GUI-XML片段
<Spinner android:id=”@+id/spinner” android:layout_width=”fill_parent” android:layout_height=”wrap_content” />
|
程式碼片段
// 建立Adapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); adapter.add(“red”); adapter.add(“green”); adapter.add(“blue”);
// 在Spinner設定Adapter Spinner spinner = (Spinner) findViewById(R.id.spinner); spinner.setAdapter(adapter);
// 取得Spinner的選擇事件 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String item = (String) parent.getSelectedItem(); Toast.makeText(Ch0123.this, item + “被選擇了”, Toast.LENGTH_SHORT).show(); }
@Override public void onNothingSelected(AdapterView<?> arg0) { } });
|
『Ch0124-自訂Spinner』-的內容有需要請留言