Android开发基础之控件RadioButton、RadioGroup

       

一、基础属性

RadioButton

1、layout_width 宽度
2、layout_height 高度
3、id 设置组件id
4、text 设置显示的内容
5、textColor 设置字体颜色
6、textStyle 设置字体风格:normal(无效果)、bold(加粗)、italic(斜体)
7、textSize 字体大小,单位常用sp
8、background 控件背景颜色
9、checked 默认选中该选项

       
1、layout_width
2、layout_height

        组件宽度和高度有4个可选值,如下图:
在这里插入图片描述

       
3、id

// activity_main.xml
android:id="@+id/btn1"  // 给当前控件取个id叫btn1
// MainActivity.java
Button btn1=findViewById(R.id.btn1);  // 按id获取控件
btn1.setText("hh");                       // 对这个控件设置显示内容

        如果在.java和.xml文件中对同一属性进行了不同设置,比如.java中设置控件内容hh,.xml中设置内容为aa,最后显示的是.java中的内容hh。

       
4、text
       可以直接在activity_main.xml中写android:text="嘻嘻",也可以在strings.xml中定义好字符串,再在activity_main.xml中使用这个字符串。

// strings.xml
<string name="str1">嘻嘻</string>
// activity_main.xml
android:text="@string/str1"

       
5、textColor
       与text类似,可以直接在activity_main.xml中写android:textColor="#FF0000FF",也可以在colors.xml中定义好颜色,再在activity_main.xml中使用这个颜色。

       
       

9、checked
       checked=“true”,默认这个RadioButton是选中的。该属性只有在RadioGroup中每个RadioButton都设置了id的条件下才有效。

       

程序示例:

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="@color/blue_700"
        android:textSize="50sp"
        android:background="@color/blue_50">
    </RadioButton>
    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="@color/i_purple_500"
        android:textSize="50sp"
        android:background="@color/i_purple_200">
    </RadioButton>
    <RadioButton
        android:id="@+id/rb3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content
  • 6
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值