sp_job = (Spinner) findViewById(R.id.sp_job);
tw_job = (TextView)findViewById(R.id.tw_job);
final String[] strJobs = { "teacher", "worker", "programmer" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.worker_sp_item, strJobs);
adapter
.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp_job.setAdapter(adapter);
sp_job.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Log.i(TAG, "===========onItemSelected==========");
tw_job.setText(strJobs[position]);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Log.i(TAG, "===========onNothingSelected==========");
}
});
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Spinner android:layout_width="match_parent" android:id="@+id/sp_job"
android:layout_height="wrap_content"></Spinner>
<TextView android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:id="@+id/tw_job"
android:text="show "
android:layout_height="fill_parent"></TextView>
</LinearLayout>
worker_sp_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:id="@+id/tw_job"
android:layout_height="wrap_content" android:text="@string/hello" />
效果: