Android入门之RadioGroup(纯java)

 

import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class RadioGroupTest extends Activity {
    /** Called when the activity is first created. */
    
	private LinearLayout mainLayout=null;
	private TextView tv=null;
	private RadioGroup rg=null;
	
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainLayout_init();
        setContentView(mainLayout);
    }
    
    void mainLayout_init(){
    	mainLayout=new LinearLayout(this);
    	LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(-1,-1);
    	mainLayout.setLayoutParams(lp);
    	mainLayout.setOrientation(LinearLayout.VERTICAL);
    	rg_init();
    	mainLayout.addView(rg);
    	mainLayout.addView(tv);
    }
    
    void rg_init(){
    	rg=new RadioGroup(this);
    	RadioButton rb1=new RadioButton(this);
    	rb1.setText("选择1");
    	rb1.setId(1);
    	rg.addView(rb1);
    	RadioButton rb2=new RadioButton(this);
    	rb2.setText("选择2");
    	rb2.setId(2);
    	rg.addView(rb2);
    	RadioButton rb3=new RadioButton(this);
    	rb3.setText("选择3");
    	rb3.setId(3);
    	rg.addView(rb3);
    	tv_init();
    	OnCheckedChangeListener listener=new OnCheckedChangeListener(){
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				tv.setText("选项"+checkedId+"被选中了\n");
			}
    	};
    	rg.setOnCheckedChangeListener(listener);
    }
    
    void tv_init(){
    	tv=new TextView(this);
    	tv.setText("开始");
    }  
}


 

你可能感兴趣的:(Android)