public class MainActivity extends Activity {
private Spinner
spinner=null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.city);
//参数一、指上下文对象
参数二、引用string中定义的数组
//参数三、指定spinner的样式
系统自带的
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this,
R.array.city,android.R.layout.simple_spinner_item);
//spinner当中每个条目的样式
同样是系统自带的
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setPrompt("市");
//为spinner绑定监听器
spinner.setOnItemSelectedListener(new
SpinnerOnSelectedListener());
}
//监听器
class
SpinnerOnSelectedListener implements
OnItemSelectedListener{
@Override
public void
onItemSelected(AdapterView<?> adapterView, View view, int
Position,
long id) {
String
selected=adapterView.getItemAtPosition(Position).toString();
System.out.println(selected);
}
@Override
public
void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated
method stub
System.out.println("nothing
selected");
}
}
}
原文:http://www.cnblogs.com/wangqianqiannb/p/3753936.html