Android 使用首选项保存开关状态,布尔值不保存

Android 使用首选项保存开关状态,布尔值不保存,android,Android,我正在尝试保存交换机的状态。我已经检查了stackoverflow的其他答案,但仍然没有找到答案 我的主要活动是切换按钮(tgbutton)和开关(simpleSwitch1)。切换按钮的首选项起作用,但开关不起作用。不确定我是否正确保存了切换按钮的首选项,但对于大多数其他论坛/答案,他们都是这样做的 请告知 public class MainActivity extends AppCompatActivity { Switch simpleSwitch1; boolean switchSta

我正在尝试保存交换机的状态。我已经检查了stackoverflow的其他答案,但仍然没有找到答案

我的主要活动是切换按钮(tgbutton)和开关(simpleSwitch1)。切换按钮的首选项起作用,但开关不起作用。不确定我是否正确保存了切换按钮的首选项,但对于大多数其他论坛/答案,他们都是这样做的

请告知

public class MainActivity extends AppCompatActivity {

Switch simpleSwitch1;
boolean switchState2 = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // initiate view's
    simpleSwitch1 = (Switch) findViewById(R.id.simpleSwitch1);

    SharedPreferences sharedPrefs = getSharedPreferences("com.example.xyle", MODE_PRIVATE);
    simpleSwitch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave",false));

    switchState2 = sharedPrefs.getBoolean("NameOfThingToSave", false);

    final ToggleButton tgbutton;
    tgbutton = (ToggleButton) findViewById(R.id.toggle);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    tgbutton.setChecked(sharedPreferences.getBoolean("toggleButton", false));  //default is false

    tgbutton.setOnClickListener(new ToggleButton.OnClickListener() {
        public void onClick(View v) {
            SharedPreferences sharedPreferences = PreferenceManager
                    .getDefaultSharedPreferences(getApplicationContext());
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("toggleButton", tgbutton.isChecked());
            editor.commit();
        }
    });

    simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked){
                SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
                editor.putBoolean("NameOfThingToSave", true);
                editor.commit();
                Toast.makeText(MainActivity.this, "This is on " + switchState2, Toast.LENGTH_SHORT).show();
            } else {
                SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
                editor.putBoolean("NameOfThingToSave", false);
                editor.commit();
                Toast.makeText(MainActivity.this, "This is off " + switchState2, Toast.LENGTH_SHORT).show();
            }
        }
    });

}

}

每次存储/检索不必要的值时,您都在声明新的
SharedReferences
。只需使用一个
SharedReferences
,并用不同的键跟踪不同的值。您应该将您的值存储在
onStop()
中,因为这样更有意义。请尝试以下代码:

public class MainActivity extends AppCompatActivity {

Switch simpleSwitch1;
boolean switchState2 = false;
SharedPreferences sharedPrefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // initiate view's
    simpleSwitch1 = (Switch) findViewById(R.id.simpleSwitch1);

    sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    if(sharedPrefs.contains("NameOfThingToSave")){
       simpleSwitch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave",false));
       switchState2 = sharedPrefs.getBoolean("NameOfThingToSave", false);
    }

    final ToggleButton tgbutton;
    tgbutton = (ToggleButton) findViewById(R.id.toggle);

    if(sharedPrefs.contains("toggleVutton")){
        tgbutton.setChecked(sharedPrefs.getBoolean("toggleButton", false));  //default is false
    }

    tgbutton.setOnClickListener(new ToggleButton.OnClickListener() {
        public void onClick(View v) {

        }
    });

    simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        }
    });

}

     @Override
     public void onStop() {
          // Store toggle state
          sharedPrefs.edit().putBoolean("toggleButton", tgbutton.isChecked()).commit();

         // Store switch state
         if (simpleSwitch1.isChecked){
                sharedPrefs.edit().putBoolean("NameOfThingToSave", true).commit();
                Toast.makeText(MainActivity.this, "This is on " + switchState2, Toast.LENGTH_SHORT).show();
            } else {
                sharedPrefs.edit().putBoolean("NameOfThingToSave", false).commit();
                Toast.makeText(MainActivity.this, "This is off " + switchState2, Toast.LENGTH_SHORT).show();
            }
       }
  }
希望这有帮助

  • 问题是,您正在使用
    不同的
    首选项(
    “com.example.xyle”
    “com.example.xyz”
    )来存储和获取
    布尔值

  • switchState2
    的值在代码中的任何地方都没有更改,这就是为什么在
    Toast
    消息上总是显示相同的结果

  • 试试这个:

    public class MainActivity extends AppCompatActivity {
    
    Switch simpleSwitch1;
    boolean switchState2 = false;
    
    SharedPreferences sharedPrefs;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // initiate view's
        simpleSwitch1 = (Switch) findViewById(R.id.simpleSwitch1);
    
        sharedPrefs = getSharedPreferences("com.example.xyz", MODE_PRIVATE);
        simpleSwitch1.setChecked(sharedPrefs.getBoolean("NameOfThingToSave",false));
    
        switchState2 = sharedPrefs.getBoolean("NameOfThingToSave", false);
    
        final ToggleButton tgbutton = (ToggleButton) findViewById(R.id.toggle);
        tgbutton.setChecked(sharedPrefs.getBoolean("toggleButton", false));  //default is false
    
        tgbutton.setOnClickListener(new ToggleButton.OnClickListener() {
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPrefs.edit();
                editor.putBoolean("toggleButton", tgbutton.isChecked());
                editor.commit();
            }
        });
    
        simpleSwitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    SharedPreferences.Editor editor = sharedPrefs.edit();
                    editor.putBoolean("NameOfThingToSave", true);
                    editor.commit();
    
                    switchState2 = true;
                    Toast.makeText(MainActivity.this, "This is on " + switchState2, Toast.LENGTH_SHORT).show();
                } else {
                    SharedPreferences.Editor editor = getSharedPreferences("com.example.xyz", MODE_PRIVATE).edit();
                    editor.putBoolean("NameOfThingToSave", false);
                    editor.commit();
    
                    switchState2 = false;
                    Toast.makeText(MainActivity.this, "This is off " + switchState2, Toast.LENGTH_SHORT).show();
                }
            }
        });
    
    }
    

    为什么不先检查onCheckedChanged是否被调用,然后检查布尔值是否被保存,最后检查该值是否被正确还原。嘿@Merlevede如果onCheckedChanged工作正常,如果我将状态从true更改为false,它将正常运行。在检查布尔值是否被保存时,我也在烤“switchState2”值,该值应该是开关状态的存储首选项,它保持为false,这表明它没有正确保存,或者switchState2没有正确初始化。您的烤面包是错误的,因为它总是显示原始的switchState2,而不是首选项。它应该是:
    Toast.makeText(MainActivity.this),这是:“+sharedPrefs.getBoolean”(“NameOfThingToSave”,false),Toast.LENGTH\u SHORT.show()