RadioButton

一、RadioButton

1、常用属性

//RadioGroup能允许多个RadioButton同时存在
<RadioGroup
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:gravity="center"//使text在RadioButton内居中
            android:text="男"
            android:button="@null"//要自定义Button的样式就要把该属性设为null
            android:background="@drawable/bg_btn4_radiobutton"//这个是昨天设计的样式
            android:checked="true"
            android:textSize="16sp"
            android:textColor="#000000"/>
        <RadioButton
            android:id="@+id/rb_2"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:gravity="center"
            android:text="女"
            android:background="@drawable/bg_btn4_radiobutton"
            android:button="@null"
            android:textSize="16sp"
            android:textColor="#000000"
            android:layout_marginLeft="10dp"/>
    </RadioGroup>

效果图
在这里插入图片描述

2、监听事件

private RadioGroup mRg1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        mRg1 = findViewById(R.id.rg_1);
        mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioGroup radioButton = radioGroup.findViewById(i);
                Toast.makeText(RadioButtonActivity.this,radioButton。getText,Toast.LENGTH_SHORT).show();
            }
        });
    }

讲道理,这个是没问题,但是我的Androidstudio无法识别getText()这个函数,所以我没办法用Toast监听返回radioButton的信息,希望有大神能教教我,是不是应该自己写一个getText()函数呢

二、复选框CheckBox

1、常用属性

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你会那些移动开发:"
        android:textSize="26sp" />
    <CheckBox
        android:layout_marginTop="10dp"
        android:layout_below="@id/tv_1"
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"
        android:textSize="16sp"/>
    <CheckBox
        android:layout_marginTop="10dp"
        android:layout_below="@id/cb_1"
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="IOS"
        android:textSize="16sp"/>
    <CheckBox
        android:layout_marginTop="10dp"
        android:layout_below="@id/cb_2"
        android:id="@+id/cb_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="其他"
        android:textSize="16sp"/>
    //下面是直接用了线性布局写的
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:orientation="vertical"
        android:layout_below="@id/cb_3">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你的兴趣:"
            android:textSize="26sp" />
        <CheckBox
            android:id="@+id/cb_4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="打游戏"
            android:textSize="16sp"/>
        <CheckBox
            android:id="@+id/cb_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编程"
            android:textSize="16sp"/>
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="其他"
            android:textSize="16sp"/>
    </LinearLayout>

效果图
在这里插入图片描述
PS:当然了,CheckBox也能自定义样式,把图片放到drawable文件里,然后用Button属性来调用就可以了

2、监听事件

public class CheckBoxActivity extends AppCompatActivity {
    private CheckBox mCb4,mCb5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        mCb4 = findViewById(R.id.cb_4);
        mCb5 = findViewById(R.id.cb_5);
        mCb5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(CheckBoxActivity.this,b?"5选中":"5未选中",Toast.LENGTH_SHORT).show();
            }
        });
        mCb4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Toast.makeText(CheckBoxActivity.this,b?"4选中":"4未选中",Toast.LENGTH_SHORT).show();
            }
        });
    }
}

效果图
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值