Android-RadioButton

RadioButton必须放在 RadioGroup 中才能实现单选。

<RadioGroup
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/radioGroup">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yes"
            android:checked="true"
            android:onClick="onRadioButtonClicked"
            android:id="@+id/radioButton1" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Maybe"
            android:onClick="onRadioButtonClicked"
            android:id="@+id/radioButton2" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:id="@+id/radioButton3"
            android:onClick="onRadioButtonClicked"/>
    </RadioGroup>

在.java中实现 onRadioButtonClicked 方法可以实现点击事件

 public void onRadioButtonClicked(View view){
        RadioButton button = (RadioButton) view;
        boolean isChecked = button.isChecked();
        switch (view.getId()){
            case R.id.radioButton1:
                break;
            case R.id.radioButton2:
                break;
            case R.id.radioButton3:
                break;
        }
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 最近在做项目的时候,设计小哥给到了如下需求: 看上去简单,xml写起来: 然而悲伤的事情来了,RadioGroup...
    vesselzhang阅读 5,787评论 2 50
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,836评论 25 709
  • 原文地址 https://liangdahong.com/2016/08/06/Cocoapods-%E7%9A%...
    liangdahong阅读 828评论 0 1
  • 那一天灯红酒绿下向你表白,结果是不留情面的拒绝,那一年万人广场下想你求婚,结果是无情嘲笑。同样都是万众瞩目,同样是...
    古映月阅读 275评论 0 0
  • 人生就像一场旅行,不必在乎目的地,在乎的,是沿途的风景以及看风景的心情,拥有快乐这份好心情,能为我们人生的漫漫旅...
    疯子Selina阅读 265评论 0 0