1 27 13 19
1 import android.app.Activity; 2 import android.os.Bundle; 3 import android.view.View; 4 import android.widget.AdapterView; 5 import android.widget.ArrayAdapter; 6 import android.widget.Spinner; 7 import android.widget.TextView; 8 9 public class Activity01 extends Activity10 {11 private static final String[] m_Countries = { "O型", "A型", "B型", "AB型", "其他" };12 13 private TextView m_TextView;14 private Spinner m_Spinner;15 private ArrayAdapteradapter;16 17 18 /** Called when the activity is first created. */19 @Override20 public void onCreate(Bundle savedInstanceState)21 {22 super.onCreate(savedInstanceState);23 setContentView(R.layout.main);24 25 m_TextView = (TextView) findViewById(R.id.TextView1);26 m_Spinner = (Spinner) findViewById(R.id.Spinner1);27 28 //将可选内容与ArrayAdapter连接29 adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item, m_Countries);30 31 //设置下拉列表的风格32 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);33 34 //将adapter添加到m_Spinner中35 m_Spinner.setAdapter(adapter);36 37 //添加Spinner事件监听38 m_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {39 40 @Override41 public void onItemSelected(AdapterView arg0, View arg1, int arg2, long arg3)42 {43 m_TextView.setText("你的血型是:" + m_Countries[arg2]);44 //设置显示当前选择的项45 arg0.setVisibility(View.VISIBLE);46 }47 48 @Override49 public void onNothingSelected(AdapterView arg0)50 {51 // TODO Auto-generated method stub52 }53 54 });55 }56 }