Android:切换按钮侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11776423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: ToggleButton Listener
提问by lemoncodes
BY THE WAY THIS THREAD IS ALREADY SOVLED BY ME, AND WITH THE HELP OF CDR. POWELL... please refer to the edited post below
顺便说一下,我已经在 CDR 的帮助下解决了这个问题。鲍威尔...请参阅下面编辑过的帖子
I have this code here
我这里有这个代码
ToggleButton toggleAlarm = (ToggleButton) d.findViewById(R.id.toggle_alarm);
toggleAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else
{
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
I have to keep track if its ON or OFF.. but when i logged something to logcat where its on or off it wont do a thing.. i dunno whats wrong cuz on my other code same, syntax but it works i just copy paste it and change only the ToggleButton variable.
我必须跟踪它是打开还是关闭..但是当我将某些内容记录到 logcat 时,它的打开或关闭它不会做任何事情..我不知道有什么问题,因为我的其他代码相同,语法但它有效,我只是复制粘贴它并仅更改 ToggleButton 变量。
EDIT
编辑
So i have observed, with the help of cdr. powell of course, that when u put this code block, the one that i have posted, inside another anonymous listener, say listener for a save button, the checkOnChangedListener is broken, it doesn't function well inside another anonymous listener, but the one thing that i don't understand is that, there is also a outer listener in my code, its like a button to display a dialog box and inside that dialog box, there is an add button that opens another dialog box which has that toggle button and another button for save or add which closes that dialog and returns to the previous dialog which will then display the newly added record, so anyone of you have an idea why is it broken when i put it inside a listener for a save button but works fine in a outer listener.
所以我在 cdr 的帮助下观察到。鲍威尔当然,当你把这个代码块,我发布的代码块放在另一个匿名侦听器中,说一个保存按钮的侦听器,checkOnChangedListener 坏了,它在另一个匿名侦听器中不能正常工作,但是那个我不明白的是,我的代码中还有一个外部侦听器,它就像一个显示对话框的按钮,在该对话框内,有一个添加按钮可以打开另一个具有该切换按钮的对话框和另一个用于保存或添加的按钮关闭该对话框并返回到上一个对话框,然后将显示新添加的记录,所以你们中的任何人都知道为什么当我把它放在一个保存按钮的监听器中时它被破坏了但有效在外部听众中很好。
采纳答案by lemoncodes
So i have observed, with the help of cdr. powell of course, that when u put this code block, the one that i have posted, inside another anonymous listener, say listener for a save button, the checkOnChangedListener is broken, it doesn't function well inside another anonymous listener, but the one thing that i don't understand is that, there is also a outer listener in my code, its like a button to display a dialog box and inside that dialog box, there is an add button that opens another dialog box which has that toggle button and another button for save or add which closes that dialog and returns to the previous dialog which will then display the newly added record, so anyone of you have an idea why is it broken when i put it inside a listener for a save button but works fine in a outer listener.
所以我在 cdr 的帮助下观察到。鲍威尔当然,当你把这个代码块,我发布的代码块放在另一个匿名侦听器中,说一个保存按钮的侦听器,checkOnChangedListener 坏了,它在另一个匿名侦听器中不能正常工作,但是那个我不明白的是,我的代码中还有一个外部侦听器,它就像一个显示对话框的按钮,在该对话框内,有一个添加按钮可以打开另一个具有该切换按钮的对话框和另一个用于保存或添加的按钮关闭该对话框并返回到上一个对话框,然后将显示新添加的记录,所以你们中的任何人都知道为什么当我把它放在一个保存按钮的监听器中时它被破坏了但有效在外部听众中很好。
回答by Mohsin Naeem
try this, May be the problem is with import
试试这个,可能是问题出在 import
toggleAlarm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else
{
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});
回答by Todd Davies
Try toggleAlarm.isChecked()
too see if the button is checked or not.
也试试看toggleAlarm.isChecked()
按钮是否被选中。
回答by alexm
In case toggleAlarm.isChecked() does not work for you you could always.
如果 toggleAlarm.isChecked() 对您不起作用,您总是可以的。
boolean _isChecked = false;
((ToggleButton) d.findViewById(R.id.toggle_alarm)).setOnClickListener(new OnOnClickListener() {
public void onClick(View arg0) {
_isChecked = !isChecked;
if(_isChecked()) {
Log.d("alarmCheck","ALARM SET TO TRUE");
sched.setAlarm(true);
}
else {
Log.d("alarmCheck","ALARM SET TO FALSE");
sched.setAlarm(false);
}
}
});