如何知道用户是否已更改切换按钮的状态? [英] How to know whether user has changed the state of toggle button?

查看:143
本文介绍了如何知道用户是否已更改切换按钮的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有十个切换按钮。我想点击主页按钮时节省五个这些按钮的状态。但我要救它,只有当用户已经进行了更改任何按钮的状态。有没有办法知道在状态的改变,而无需使用setOnClickListener()?

I have ten toggle buttons. I want to save the state of five of those buttons when clicking the home button. But I want to save it only if the user has made a change to any of the buttons' state. Is there any way to know the change in states without using setOnClickListener()?

推荐答案

我也做了以下内容,它不是很好,但它的工作原理:

I have done the following, its not so nice, but it works:

ttsButton = (ToggleButton) findViewById(R.id.solution_ttsbutton);
ttsButton.setOnCheckedChangeListener(toggleButtonChangeListener);
...
// gets called, if the button state changes
final CompoundButton.OnCheckedChangeListener toggleButtonChangeListener = new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // The user changed the button, do something
    }
};

和我这样做,如果我想以编程方式更改按钮,不执行变更监听器:

and I do the following, if I want to change the button programatically without executing the change listener:

ttsButton.setOnCheckedChangeListener(null);
ttsButton.setChecked(false);
ttsButton.setOnCheckedChangeListener(toggleButtonChangeListener);

这篇关于如何知道用户是否已更改切换按钮的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆