Toggle Buttons

来源:互联网 发布:降低音量软件 编辑:程序博客网 时间:2024/05/17 07:25
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.

0 0
原创粉丝点击