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

时间:2014-04-20 08:42:01

标签: android sharedpreferences

我是Android新手,我一直在使用此代码来保存我的Checkboxes的价值 当应用程序关闭时工作正常(在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;       

        }

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

这是我的活动

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);
         }

1 个答案:

答案 0 :(得分:1)

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

由于您已经创建了共享首选项,因此在目标活动中使用如下所示。

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

现在得到布尔值,如下所示:

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

让我知道它是怎么回事

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

像这样更改密钥,因为密钥应为String值,并使isChecked静态以便从其他活动访问

将值放入共享首选项

  editor.getBoolean("key", isChecked);

要检索值

setprefsd.getBoolean(“Key”,isChecked)

EDIT2 ---------------------------------------------- -------- 更改您的共享偏好设置插入如下。

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


    }