android控件05---RadioButton(RadioGroup)

RadioButton表示一个单选按钮,它有选中和不选中2种状态。
RadioGroup组件被称为单选按钮组,因为它可以有很多个单选按钮,并且规定在这个组里面只能有一个单选按钮是选中状态。也就是说,但选择其中一个单选按钮时,会取消这个组中其他已经被选择的按钮的选中状态。

依旧附上一个小例子:


XML布局中的代码如下(采用的是RelativeLayout布局):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="下列哪个选项不属于android控件?"
        android:textSize="20dp" />
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true">
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="textView" />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="EditText" />
        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Android" />
    </RadioGroup>
</RelativeLayout>



Activity里的代码如下:

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private RadioGroup radioGroup;
    private RadioButton radio1;
    private RadioButton radio2;
    private RadioButton radio3;
    private RadioButton radio4;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView)findViewById(R.id.textView);
        radioGroup =(RadioGroup)findViewById(R.id.radioGroup);
        radio1 = (RadioButton)findViewById(R.id.radioButton1);
        radio2 = (RadioButton)findViewById(R.id.radioButton2);
        radio3 = (RadioButton)findViewById(R.id.radioButton3);
        radio4 = (RadioButton)findViewById(R.id.radioButton4);


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group,int checkedId) {
                if (checkedId == radio4.getId()){
                    DisplayToast("回答正确!");
                }else{
                    DisplayToast("回答错误!");
                }
            }
        });
    }
    public void DisplayToast(String str){
        //创建Toast对象,设置Toast显示的位置,显示Toast
        Toast toast = Toast.makeText(MainActivity.this, str , Toast.LENGTH_LONG);
        toast.setGravity(Gravity.TOP,0,500);
        toast.show();
    }
}


效果图如下:




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值