Android
Android スピナー デザイン性のあるスピナー サンプルコード
Androidのくるくる回って複数項目から一つの項目が選択できるアレをスピナー(Spinner)と言いますが、ネット上のサンプルには、一行だけのシンプルなスピナーが多かったので、字の大きさや色を変えたり、複数行にするスピナーのサンプルコードを掲載しておきます。
画面はこれです。

車の点検項目を選択する、というやつです。
以前、
Android ListViewの項目をタップして関連する内容を入力してListViewに戻る サンプルコード
ではリストビューとチェックボックスでやっていましたが、仕様変更により、スピナーでの選択式にかわりました(^_^;
InspectionActivity
public class InspectionsActivity extends ListActivity implements OnItemSelectedListener {
public static final String TAG = "InspectionsActivity";
private static final int REQUEST_CODE = 1000; // 適当な数字 ProblemDetailActivityを識別するためだけの数字
private Spinner inspectionSpinner;
private String[] inspectionList;
private ArrayList<HashMap<String, String>> mylist;
private SimpleAdapter adapter;
private HashMap<String, String> map_temporary;
private SimpleAdapter list_adapter;
private ArrayList<HashMap<String, String>> problemlist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inspections);
inspectionSpinner = (Spinner) findViewById(R.id.selection);
mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map1 = new HashMap<String, String>();
map1.put("item_name", "燃料");
map1.put("detail", "量・漏れ");
map1.put("label", "fuel_leaks");
mylist.add(map1);
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("item_name", "エンジン");
map2.put("detail", "かかり具合・異音");
map2.put("label", "engine");
mylist.add(map2);
HashMap<String, String> map3 = new HashMap<String, String>();
map3.put("item_name", "ブレーキペダル");
map3.put("detail", "踏みしろ・きき具合・片ぎさ");
map3.put("label", "brake_pedal");
mylist.add(map3);
HashMap<String, String> map4 = new HashMap<String, String>();
map4.put("item_name", "ブレーキレバー");
map4.put("detail", "引きしろ・きき具合");
map4.put("label", "brake_lever");
mylist.add(map4);
adapter = new SimpleAdapter(this, mylist,
R.layout.inspection_sipinner_item,
new String[] {
"item_name", "detail", "problem_comment"
},
new int[] {
R.id.item_name, R.id.detail, R.id.problem_comment
});
adapter.setDropDownViewResource(R.layout.inspection_sipinner_item);
inspectionSpinner.setAdapter(adapter);
inspectionSpinner.setOnItemSelectedListener(this);
// 初回起動時の対応
inspectionSpinner.setFocusable(false);
problemlist = new ArrayList<HashMap<String, String>>();
}
// スピナー内のアイテムが選択された時の動作
@Override
public void onItemSelected(
AdapterView<?> arg0,
View arg1,
int arg2,
long arg3) {
// 初回起動時の動作
if (inspectionSpinner.isFocusable() == false) {
inspectionSpinner.setFocusable(true);
return;
}
int item_id = arg0.getSelectedItemPosition();
mylist.get(item_id);
String item_name = (String) mylist.get(item_id).get("item_name");
String detail = (String) mylist.get(item_id).get("detail");
startIntent(item_id, item_name, detail);
}
protected void startIntent(int item_id, String item_name, String detail) {
Log.d(TAG, item_name);
Intent problem_detail = new Intent(this, ProblemDetailActivity.class);
problem_detail.putExtra("item_id", item_id);
problem_detail.putExtra("detail", detail);
problem_detail.putExtra("item_name", item_name);
startActivityForResult(problem_detail, REQUEST_CODE);
}
inspection_spinner_item.xnl
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:orientation="horizontal" >
<TextView
android:id="@+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="1dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="13sp"
android:textColor="@color/dark_blue" />
</LinearLayout>
</LinearLayout>
inspections.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dip"
android:entries="@array/inspectionList"
android:prompt="@string/inspection" >
</Spinner>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="@+id/finish"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/reserved_button"
android:onClick="onClickButton"
android:padding="10dip"
android:text="@string/inspection_finished"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>