gpt4 book ai didi

android - 在 SharedPreference 中存储值并在 android 中从中获取

转载 作者:行者123 更新时间:2023-11-29 01:43:04 25 4
gpt4 key购买 nike

我是 android 的新手,我一直在使用这段代码来保存我的复选框的值当应用程序关闭时工作正常(在 Settings.class 中)

        public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;

}

但我想在另一个 Activity (Progress.class)中使用相同的保存值

这是我的 Activity

Settings.class

          checkBox_one = (CheckBox) findViewById(R.id.checkBox1);

boolean isChecked = getBooleanFromPreferences("isChecked");
Log.i("start",""+isChecked);
checkBox_one.setChecked(isChecked);
//checkBox_one.setChecked(true);//Enable By Default
checkBox_one.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {
Log.i("boolean",""+isChecked);
Settings.this.putBooleanInPreferences(isChecked,"isChecked");
}
});

checkBox_two = (CheckBox) findViewById(R.id.checkBox2);

boolean isCheckedTwo = getBooleanFromPreferences("isCheckedTwo");
checkBox_two.setChecked(isCheckedTwo );
//checkBox_two.setChecked(true);//Enable By Default
checkBox_two.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {

Settings.this.putBooleanInPreferences(isChecked,"isCheckedTwo");
}
});

checkBox_three = (CheckBox) findViewById(R.id.checkBox3);

boolean isCheckedThree = getBooleanFromPreferences("isCheckedThree");
checkBox_three.setChecked(isCheckedThree );
checkBox_three.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {

Settings.this.putBooleanInPreferences(isChecked,"isCheckedThree");
}
});

checkBox_four = (CheckBox) findViewById(R.id.checkBox4);

boolean isCheckedFour = getBooleanFromPreferences("isCheckedFour");
checkBox_four.setChecked(isCheckedFour );
//checkBox_four.setChecked(true);//Enable By Default
checkBox_four.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {

Settings.this.putBooleanInPreferences(isChecked,"isCheckedFour");
}
});

checkBox_five = (CheckBox) findViewById(R.id.checkBox5);

boolean isCheckedFive = getBooleanFromPreferences("isCheckedFive");
checkBox_five.setChecked(isCheckedFive );
checkBox_five.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked) {

Settings.this.putBooleanInPreferences(isChecked,"isCheckedFive");
}
});

}

public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean(key, isChecked);
editor.commit();
}
public boolean getBooleanFromPreferences(String key){
SharedPreferences sharedPreferences = this.getPreferences(Activity.MODE_PRIVATE);
Boolean isChecked = sharedPreferences.getBoolean(key, false);
return isChecked;

}
//-------------------------//

这里是 Progress.Class

        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
progressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
checkBox2 = (CheckBox) findViewById(R.id.checkBox2);

checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateProgressBars();
}
});

checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
updateProgressBars();
}
});
}

public void updateProgressBars() {

progressBar1.setVisibility(View.GONE);
progressBar2.setVisibility(View.GONE);

if (checkBox1.isChecked() && checkBox2.isChecked()) {
progressBar2.setVisibility(View.VISIBLE);
} else if (checkBox1.isChecked()) {
progressBar1.setVisibility(View.VISIBLE);
}

最佳答案

我最近在我的应用程序中实现了相同的要求。我遵循了以下过程。

由于您已经创建了共享首选项,现在在目标 Activity 中使用如下。

SharedPreferences setprefsd=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

现在获取 bool 值,如下所示:

setprefsd.getBoolean(Key,Value)   //key is the one which you have used while declaring

让我知道进展如何

编辑-------------------------------------------- ----------------------------------

像这样更改键,因为键应该是一个 String 值,并使 isChecked 静态以从另一个 Activity 访问

将值(value)放在共享偏好中

  editor.getBoolean("key", isChecked);

获取值

setprefsd.getBoolean("Key", isChecked)

编辑2-------------------------------------------- ----------如下所示更改您的共享首选项插入。

public void putBooleanInPreferences(boolean isChecked,String key){
SharedPreferences sharedPreferences =PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("key", isChecked);
editor.commit();


}

关于android - 在 SharedPreference 中存储值并在 android 中从中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23180143/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com