【Android开发--新手必看篇】RadioButton 单选框(含高级样式)

Android笔记

​ ——各类控件的使用(控件)-单选框
若对该知识点有更多想了解的,欢迎私信博主~~

单选框:RadioButton
属性:
XML样式说明
layout_width布局宽度
layout_height布局高度
text文本值
textColor字体颜色
textSize字体大小
textStyle字体风格
buttonTint按钮色调
background背景
scaleXX轴拉伸比例
scaleYY轴拉伸比例
checkedButton默认选中(填入id)
checkedtrue为选中
button@null时只显示文字
注:一般配合RadioGroup(单选按钮组)使用,才有单选效果
方法:
常用方法说明
isChecked()被选中
getText()获取单选框的值
高级方法:改选选项时触发
RadioGroup rg;
    RadioButton rb,rb1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main26);
        //绑定控件
        rg=findViewById(R.id.rg);
        rb=findViewById(R.id.rb);
        rb1=findViewById(R.id.rb1);
        //改变选项触发
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb:
                        Toast.makeText(Main26Activity.this, ""+rb.getText().toString(), Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb1:
                        Toast.makeText(Main26Activity.this, ""+rb1.getText().toString(), Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
    }
注:方法作用的对象是方法组
高级样式:
  1. 圆圈的颜色(此文件名为radiobtn_circlecolor.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true"
            android:color="#0000ff"/>
    
        <item android:state_checked="false"
            android:color="#ff0000"/>
    </selector>
    
  2. 点击时的波纹颜色(此文件名为radiobtn_ripplecolor.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:color="#0000ff"
        tools:targetApi="lollipop">
    </ripple>
    
  3. 文字的颜色(此文件名为radiobtn_textcolor.xml)

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true"
            android:color="#0000ff"/>
    
        <item android:state_checked="false"
            android:color="#ff0000"/>
    </selector>
    
    使用:XML样式文件中
    <RadioButton
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:text="男"
            android:textSize="30sp"
            android:buttonTint="@drawable/radiobtn_circlecolor"
            android:background="@drawable/radiobtn_ripplecolor"
            android:textColor="@drawable/radiobtn_textcolor"
            android:textStyle="bold"
            android:scaleX="1.5"
            android:scaleY="1.5"
            android:id="@+id/rb"/>
    
    注:若想改变框的大小,可以通过scaleX和scaleY来间接更改
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个单选框、复选框的Android Studio注册样式的代码示例: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/editTextEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" /> <EditText android:id="@+id/editTextPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Gender" /> <RadioGroup android:id="@+id/radioGroupGender" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/radioButtonMale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" /> <RadioButton android:id="@+id/radioButtonFemale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" /> </RadioGroup> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hobbies" /> <CheckBox android:id="@+id/checkBoxReading" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reading" /> <CheckBox android:id="@+id/checkBoxTravelling" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Travelling" /> <Button android:id="@+id/buttonRegister" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Register" /> </LinearLayout> ``` 在这个布局中,我们使用了一个EditText来输入邮箱和密码,一个RadioGroup来选择性别(男/女),以及两个CheckBox来选择爱好(阅读/旅行)。最后,我们使用了一个Button来注册。你可以根据需要进行修改和调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值