界面设计:
<?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="wrap_content">
<Spinner android:id="@+id/spinner"
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
代码处理:
public class SpinnerActivity extends Activity {
private staticfinal String TAG = "SpinnerActivity";
@Override
public voidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
//第二个参数为下拉列表框每一项的界面样式,该界面样式由Android系统提供,当然您也可以自定义
ArrayAdapter<String>adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("java");
adapter.add("dotNet");
adapter.add("php");
Spinnerspinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
@Override
public voidonItemSelected(AdapterView<?> adapterView, View view, int position, longid) {
Spinner spinner =(Spinner)adapterView;
StringitemContent = (String)adapterView.getItemAtPosition(position);
}
@Override
public voidonNothingSelected(AdapterView<?> view) {
Log.i(TAG, view.getClass().getName());
}
});
}
}