保存开关按钮状态不起作用

时间:2017-05-22 06:59:01

标签: android sharedpreferences toggle togglebutton android-togglebutton

我有一个开关按钮,用于设置应用程序开启和关闭的声音,虽然它正常工作,但是当我关闭应用程序然后再次打开它时...开关自动变为false ..!? 我试图通过共享首选项库Hawk保存最后一个状态,但我不清楚如何正确执行它。!

当我尝试使用Hawk

  // ....

   bool soundON = Hawk.get("sound"); // NPE !!!

  if (soundON) {
    soundSwitch.setChecked(true);
    soundSwitch.setSelected(true);
  } else {
    soundSwitch.setChecked(false);
    soundSwitch.setSelected(false);
  }


soundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
      isSoundEnabled = true;
      Hawk.put("sound", true);
     } else {
      isSoundEnabled = false;
    }
    Log.i("is checked sound", isChecked + "");
    }});

更新 - 解决方案

我使用了@Shrikant回答,但只做了一点改动。我使用Boolean代替boolean,因此我可以检查null允许您执行的Boolean值,因为它可以是truefalsenullboolean不同,可以是truefalse

这是代码

Boolean isSound = Hawk.get("sound");

if (isSound == null) {
  // I want the sound to be ON by default so I set it to true
  soundSwitch.setChecked(true);
  soundSwitch.setSelected(true);
} else {
  soundSwitch.setChecked(isSound);
  soundSwitch.setSelected(isSound);
  Log.i("Sound State > ", isSound + "");

}

3 个答案:

答案 0 :(得分:1)

GroupOdd WHERE Teacher_Name AND Standard AND Subject方法中,您必须从共享首选项中检查isSoundEnabled,无论您是从本地变量中检查过它。

试试这个:

        cmd.CommandText = "SELECT Teacher_Name, Standard, Subject, Balance_Slot FROM GroupOdd order by sum_weekly_slot desc";

        DataTable dt = new DataTable();
        SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
        da.Fill(dt);
        foreach (DataRow dr in dt.Rows)
        {
            int BalanceSlot = Convert.ToInt32(dr["Balance_Slot"].ToString());

            if (BalanceSlot > 0)
            {

                for (int j = 1; j <= 9;)
                {
                    //bool TeacherNameExist = true;
                    DataTable dt2 = new DataTable();
                    cmd2.CommandText = "SELECT * FROM group_odd_" + j + " WHERE (Teacher_Name = @Teacher_Name OR Standard = @Standard OR Subject = @Subject)";
                    cmd2.Parameters.AddWithValue("@Teacher_Name", dr["Teacher_Name"].ToString());
                    cmd2.Parameters.AddWithValue("@Standard", dr["Standard"].ToString());
                    cmd2.Parameters.AddWithValue("@Subject", dr["Subject"].ToString());
                    SQLiteDataAdapter da2 = new SQLiteDataAdapter(cmd2);
                    da2.Fill(dt2);
                    if (dt2.Rows.Count == 0)
                    {
                        int BSlot = BalanceSlot - 1;
                        //cmd2.CommandText = "INSERT INTO group_odd_" + j + " (Teacher_Name, Standard, Subject) VALUES (@Teacher_Name, @Standard, @Subject)";
                        cmd2.CommandText = "Update group_odd_" + j + " SET Teacher_Name = @Teacher_Name, Standard = @Standard, Subject = @Subject";
                        cmd2.Parameters.AddWithValue("@Teacher_Name", dr["Teacher_Name"].ToString());
                        cmd2.Parameters.AddWithValue("@Standard", dr["Standard"].ToString());
                        cmd2.Parameters.AddWithValue("@Subject", dr["Subject"].ToString());
                        cmd2.CommandText = "Update GroupOdd set Balance_Slot = @Balance_Slot WHERE Teacher_Name = @Teacher_Name AND Standard = @Standard AND Subject = @Subject";
                        cmd2.Parameters.AddWithValue("@Balance_Slot", BSlot);
                        cmd2.Parameters.AddWithValue("@Teacher_Name", dr["Teacher_Name"].ToString());
                        cmd2.Parameters.AddWithValue("@Standard", dr["Standard"].ToString());
                        cmd2.Parameters.AddWithValue("@Subject", dr["Subject"].ToString());
                        cmd2.ExecuteNonQuery();
                    }
                    else
                    {
                        j++;
                    }
                }

而不是:

onCreate()
bool soundValue = Hawk.get("sound"); if (soundValue) { soundSwitch.setChecked(true); soundSwitch.setSelected(true); } else { soundSwitch.setChecked(false); soundSwitch.setSelected(false); }

中的

答案 1 :(得分:1)

试试这个

在onCreate()初始化Hawk

Hawk.init(context).build();

之后从Hawk获取声音值并将其设置为切换

boolean isSound = Hawk.get("some",false);
soundSwitch.setChecked(isSound);
soundSwitch.setSelected(isSound);

没有用if else

检查布尔值
soundSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            Log.i("is checked sound", isChecked + "");
            Hawk.put("sound", isChecked);

        }
    });

我使用过Hawk的2.0.1版本。

答案 2 :(得分:1)

您应该在setOnCheckedChangeListener调用时保存检查状态。并在onCreate调用时获取检查状态。