Android开发笔记----radioGroup的使用

radioGroup

radioGroup是单项选择按钮,用来实现单选功能。

简单地创建一个radioGroup,和建立一个button,textview等基本相同,就是要写xml文件,只不过radioGroup需要在其中在写入radioButton,代码如下

 <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >


        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" />


        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />


        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    </RadioGroup>

只要在radioGroup中添加多个radioButton即可,这样便可以使用了。


对radioButton单选的操作

首先我们需要通过findviewbyid来将radioButton与activity中的变量想对应,然后我们在通过isChecked方法来确定是否被选中,但是由于radioGroup仅仅是个单选的控件,并不能实现我选中一个选项而进行接下来的操作,所以我们需要一个button来提交我们所选中的radioButton。代码如下:

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.radio_layout);

radiobutton=(RadioButton) findViewById(R.id.radio0);
findViewById(R.id.btnSubmit).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(radiobutton.isChecked())
{
new AlertDialog.Builder(radioAty.this).setTitle("判断").setMessage("回答正确").setPositiveButton("关闭",null).show();//这个是用来弹出一个小的对话框用于显示信息
}
else
{
new AlertDialog.Builder(radioAty.this).setTitle("判断").setMessage("回答错误").setPositiveButton("关闭",null).show();
}
}
});

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值