コードは次のとおりです。
TextView textView=(TextView) findViewById(R.id.textView1);
Spinner spinner=(Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,quantity));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(arg2==0){
textView.setText("Strawberry = 10");
}else if(arg2==1){
textView.setText("Chocolate = 20");
}else if(arg2==2){
textView.setText("Vanilla = 30");
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
String[] quantity={"Strawberry","Chocolate","Vanilla"};
および xml ファイル:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>