maplele 2012-07-27
先是在 layout 里面 建议个 Spinner 控件
<?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/spnner" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
然后在 主activity 里
package so.com; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class SoActivity extends Activity { /** Called when the activity is first created. */ private List<String> list = new ArrayList<String>(); private Spinner spinner; private ArrayAdapter<String> adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner = (Spinner)findViewById(R.id.spnner); list.add("北京"); list.add("上海"); list.add("天津"); list.add("南京"); adapter = new ArrayAdapter<String>(SoActivity.this, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); ///增加监听事件,其实在程序启动时就调用了该监听 spinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(SoActivity.this, adapter.getItem(arg2), Toast.LENGTH_LONG).show(); } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub Toast.makeText(SoActivity.this, "Selected None", Toast.LENGTH_LONG).show(); } }); } }