2016-02-20 23 views
1

我在我的应用程序中使用了一个开关来启用和禁用通知。现在我正在创建一个活动。setChecked的开关不起作用

我想显示开关检查,如果任何通知设置为该事件,否则它应该是假的。但我对此没有任何改变。每次开关都关闭。

当我做了调试,如果通知不是null然后它去其他部分,但开关剂量不显示。

notificationTime = eventData.getNotificationTime(); 

     if(notificationTime.equals("")) 
     { 
      notify.setChecked(false); 
     } 
     else { 
      notify.setChecked(true); 
     } 

怎么回事?

回答

0

这是在Android中处理开关的正确方法。

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
     // do something, the isChecked will be 
     // true if the switch is in the On position 
    } 
}); 
+0

我知道这一点,但如何设置通知默认显示开关? – Sid

+0

如果我只是setChecked(true)它显示,但不显示在此代码中。 – Sid

+0

你可以添加'Log.w(“check”,“”+ working);'在代码中发布的if循环中,查看if循环是否被执行?如果它的工作,你应该接收logcat的日志! – OBX