2014-04-20 65 views
2

我是新來的Android和我一直用這個代碼來保存我的複選框 的值當應用程序關閉工作正常(在Settings.class)商店價值,並從它的Android

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

以下面的方式在您的get和set方法中實例化您的sharepreferences對象。 –

回答

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( 「鑰匙」,器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();  


    } 
+0

你好。你知道一個教程顯示可用的內存和一個TextView的內存? –

+0

不....不知道.. – Siva

+0

你好siva我已經失去了啓用/關閉飛行模式的代碼,但是我在聊天室給你發了那段代碼,你可以給我那個鱈魚嗎? –