RadioGroup的應用

摘要:RadioGroup應用

點選radiogroup裡的選項會變動文字內容:

package tw.nkfust.jason;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class RadioSampleActivity extends Activity {
 /** Called when the activity is first created. */
 RadioGroup mgroup;
 RadioButton rdb1;
 RadioButton rdb2;
 TextView txtview;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mgroup = (RadioGroup) findViewById(R.id.radioGroup1);
  rdb1 = (RadioButton) findViewById(R.id.radio1);
  rdb2 = (RadioButton) findViewById(R.id.radio2);
  txtview =(TextView) findViewById(R.id.textView1);
  
  RadioGroup.OnCheckedChangeListener changeradio = new RadioGroup.OnCheckedChangeListener(){
   public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    // 透過id來辨認不同的radiobutton
    switch (checkedId) {
    case R.id.radio1:
     txtview.setText(rdb1.getText());
     break;
    case R.id.radio2:
     txtview.setText(rdb2.getText());
     break;
    default:
     break;
    }
   }   
  };
  
  mgroup.setOnCheckedChangeListener(changeradio);
 }
}