单选按钮RadioButton

先来个例子感受一下


    
    
        
        
    
    

有图有真相

单选按钮RadioButton_第1张图片


1、在改变单选按钮组的值时获取

RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r = (RadioButton)findViewById(checkedId);
                r.getText(); //获取被选中的单选按钮的值
            }
        });

2、单击其他按钮时获

final RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i


实例上的java代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r = (RadioButton)findViewById(checkedId);//获取被选择的单选按钮
                Log.i("单选按钮","您选择的是:"+r.getText());
            }
        });
        Button button = (Button)findViewById(R.id.button1); //获取提交按钮
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i




你可能感兴趣的:(android)