public class MainActivity extends Activity { public Spinner m_spnStart; public Spinner m_spnEnd; public Button m_btnRun; public EditText m_edt01; public TextView m_txt01; public RadioButton m_rb01; public RadioButton m_rb02; public RadioButton m_rb03; public RadioGroup m_rgroup; public String[] m_strPark = {“台北”, “台中”, “高雄”}; private Context mContext; private ArrayAdapter m_listPark; public String m_strOut; public String m_strBuf01,m_strBuf02,m_strBuf03; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this.getApplicationContext(); m_listPark = new ArrayAdapter(this,android.R.layout.simple_spinner_item, m_strPark); m_spnStart = (Spinner)findViewById(R.id.spinner1); m_spnEnd = (Spinner)findViewById(R.id.spinner2); m_btnRun=(Button)findViewById(R.id.button1); m_edt01=(EditText)findViewById(R.id.editText1); m_txt01=(TextView)findViewById(R.id.textView4); m_rb01=(RadioButton)findViewById(R.id.radio0); m_rb02=(RadioButton)findViewById(R.id.radio1); m_rb03=(RadioButton)findViewById(R.id.radio2); m_rgroup=(RadioGroup)findViewById(R.id.radioGroup1); m_rgroup.setOnCheckedChangeListener(listener); m_spnStart.setAdapter(m_listPark); m_spnEnd.setAdapter(m_listPark); m_btnRun.setOnClickListener(new ButListener()); m_strBuf03=”成人”; m_spnStart.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override public void onNothingSelected(AdapterView> arg0) { // TODO Auto-generated method stub
} @Override public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(mContext, “你選的是”+m_strPark[arg2], Toast.LENGTH_SHORT).show(); m_strBuf01=m_strPark[arg2]; } }); m_spnEnd.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(mContext, “你選的是”+m_strPark[arg2], Toast.LENGTH_SHORT).show(); m_strBuf02=m_strPark[arg2]; } @Override public void onNothingSelected(AdapterView> arg0) { // TODO Auto-generated method stub } }); } private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio0: m_strBuf03=”成人”; break; case R.id.radio1: m_strBuf03=”老人”; break; case R.id.radio2: m_strBuf03=”兒童”; break; } } };
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } class ButListener implements OnClickListener { public void onClick(View v) { m_strOut=m_strBuf03+”:”+m_strBuf01+”~”+m_strBuf02+”共”+m_edt01.getText().toString()+”張數”; m_txt01.setText(m_strOut); } } }