<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/color" >
</TextView>
<Spinner android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/color"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ImageButton</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="color">选择色彩:</string>
<string-array name="colors">
<item>黑色 | Black</item>
<item>蓝色 | Blue</item>
<item>棕色 | Brown</item>
<item>绿色 | Green</item>
<item>灰色 | Grey</item>
<item>粉色 | Pink</item>
<item>紫色 | Purple</item>
<item>红色 | Red</item>
<item>白色 | White</item>
</string-array>
</resources>
package com.example.imagebutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MainSpinnerActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainspinner);
Spinner spinner=(Spinner) findViewById(R.id.spinner);
//准备一个数组适配器
ArrayAdapter adapter=ArrayAdapter.createFromResource(this, R.array.colors, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
OnItemSelectedListener oisl=new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "选择的色彩:"+parent.getItemIdAtPosition(position),1).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
};
spinner.setOnItemSelectedListener(oisl);
}
}