Toggle Buttons

本文介绍了如何在Android应用中使用Togglebutton实现开关功能。包括在布局文件中添加组件、通过编程方式改变状态及监听用户点击事件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Toggle button给用户提供两选一的方案,例如on/off。只允许用户选择其中的一个.你可以直接在layout文件中添加一个Toggle button。
Toggle button 同样也是CompoundButton的子类,可以调用CompoundButton.setCheck() 或者CompoundButton.toggle()来自己改变Toggle button的状态。
在activity中要检测用户的点击事件,可以重写CompoundButton。OncheckeckListener方法来检测toggle button 是enable还是disable.
code 如下所示:
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            // The toggle is enabled
        } else {
            // The toggle is disabled
        }
    }
});
先通过findViewById 找到ToggleButton,然后重写onCheckedChanged,通过形参的第二个参数boolean isChecked来判断togglebutton是enable还是disable.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值