关于获取RadioGroup里面的RadioButton的值,多行RadioGroup展示清除选中效果

本文介绍如何使用Android中的RadioGroup实现单选功能,并在选择改变时更新TextView显示所选项的值。提供了完整的Java代码示例及XML布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

源代码地址:http://download.youkuaiyun.com/download/wb935419471/9911736
RadioGroup实现单选并获得所选项值:
代码片段:

public class MainActivity extends Activity {
public TextView mTextView1;
public RadioGroup mRadioGroup1,mRadioGroup2;
public RadioButton mRadio1, mRadio2,mRadio3,mRadio4;
public Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 取得 TextView、RadioGroup、RadioButton对象
mTextView1 = (TextView) findViewById(R.id.myTextView);
submit = (Button) findViewById(R.id.submit);
mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);
mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);
mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
mRadioGroup2 = (RadioGroup) findViewById(R.id.myRadioGroup2);
mRadio3 = (RadioButton) findViewById(R.id.myRadioButton3);
mRadio4 = (RadioButton) findViewById(R.id.myRadioButton4);
submit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(mTextView1.getText().toString().equals("")){
                  Toast toast = Toast.makeText(MainActivity.this, "mTextView1为空"+mTextView1.getText().toString(), Toast.LENGTH_SHORT);                    
                    toast.show();                
            }else{
                  Toast toast = Toast.makeText(MainActivity.this, "mTextView1选中的值为:"+mTextView1.getText().toString(), Toast.LENGTH_SHORT);                    
                    toast.show(); 
            }

        }
    });


    mRadioGroup1.setOnCheckedChangeListener(new OnMyManholeStateOneCheckedChangeListener());

    mRadioGroup2.setOnCheckedChangeListener(new OnMyManholeStateTwoCheckedChangeListener());

}


private class OnMyManholeStateOneCheckedChangeListener implements
        RadioGroup.OnCheckedChangeListener {

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int position) {

        switch (position) {
        case R.id.myRadioButton1:
            if (mRadio1.isChecked())
                mRadioGroup2.clearCheck();//清除RadioGroup2的选中状态
              mTextView1.setText(mRadio1.getText());

            break;
        case R.id.myRadioButton2:
            if (mRadio2.isChecked())
                mRadioGroup2.clearCheck();
             mTextView1.setText(mRadio2.getText());

            break;


        default:
            break;
        }
    }
}

private class OnMyManholeStateTwoCheckedChangeListener implements
        RadioGroup.OnCheckedChangeListener {

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int position) {
        switch (position) {
        case R.id.myRadioButton3:
            if (mRadio3.isChecked())
                mRadioGroup1.clearCheck();//清除RadioGroup1的选中状态
             mTextView1.setText(mRadio3.getText());

            break;
        case R.id.myRadioButton4:
            if (mRadio4.isChecked())
                mRadioGroup1.clearCheck();
             mTextView1.setText(mRadio4.getText());
            break;


        default:
            break;
        }

    }
}

}

xml布局:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值