当前位置:   article > 正文

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

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
注:方法作用的对象是方法组
高级样式:
  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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
  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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    使用: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"/>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    注:若想改变框的大小,可以通过scaleX和scaleY来间接更改
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/733619
推荐阅读
相关标签
  

闽ICP备14008679号