2013-03-23 100 views
0

*我一直在最近幾天堅持使用這個問題。我試圖使用SharedPreferences來允許我的用戶通過ToggleButton保存所選的遊戲內音頻選項,但是每當我運行我的應用程序,並點擊我的「完成」按鈕返回到main_activity屏幕,然後再次點擊設置,它從來沒有saving.I已經做了一些搜索google搜索,我的書和這個網站,但通過許多方法我'已經嘗試過的結果是一樣的。 我是新開發的android開發,更不用說應用程序開發了,就像我在過去幾周學到的一樣,這對我來說是一個主要障礙,我很抱歉如果這個問題已經在本網站上被覆蓋,但我在一個真實的我在這裏做錯了這裏的損失。 這是我的設置活動中的代碼。SharedPreferences ToggleButton沒有保存

package com.fullfrontalgames.numberfighter; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.preference.Preference; 
import android.preference.PreferenceManager; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CompoundButton; 
import android.widget.ToggleButton;  

public class Settings extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.settings); 

     Button Notifications = (Button) findViewById(R.id.Notifications); 
     Button Done = (Button) findViewById(R.id.done); 
     Button AccountSettings = (Button) findViewById(R.id.AccountSettings); 
     final ToggleButton AT = (ToggleButton) findViewById(R.id.AudioToggle); 
     AT.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       { 
        if ((AT.isChecked())) 
        { 
         SharedPreferences appPrefs = 
         getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences", 
             MODE_PRIVATE); 
         SharedPreferences.Editor editor = appPrefs.edit(); 
         editor.putBoolean("atpref", AT.isChecked()); //value to store 
         editor.commit(); 
        } 
       } 
      } 
     }); 
     SharedPreferences appPrefs = 
       getSharedPreferences("com.fullfrontalgames.numberfighter.Settings_preferences", 
       MODE_PRIVATE); 
     boolean atpref = appPrefs.getBoolean("atpref", true); //default is true 
     AT.setChecked(atpref); 
     Done.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent Intent = new Intent(Settings.this,activity_main.class); 
       Intent.setFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(Intent); 



      } 
     }); 
     Notifications.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent("com.fullfrontalgames.numberfighter.Notifications")); 
      } 
     }); 
     AccountSettings.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       startActivity(new Intent("com.fullfrontalgames.numberfighter.AccountSettings")); 
      } 
     }); 

    }  

    public void onToggleClicked(View view) { 
     // Is the toggle on? 
     boolean on = ((ToggleButton) view).isChecked(); 

     if (on) { 
      view.setSoundEffectsEnabled(true); 
     } else { 
      view.setSoundEffectsEnabled(false); 

     } 
    } 

} 
+0

我想你可以更改默認值從真到假。或者你可以用@Sam的答案去。 – 2013-03-23 22:41:08

回答

2

如果你試圖保存切換按鈕的狀態時,它是false,只需刪除你的OnClickListener if語句:

if ((AT.isChecked())) 

+1

非常感謝您爲我工作!永遠不會想到這一點,我從你的答案中學到了很多! – Cranosaur 2013-03-23 23:17:32