2015-07-28 63 views
0

我需要保存有關切換按鈕的信息,當用戶終止該應用或重新啓動我想要顯示的按鈕感謝如何保存切換按鈕的Android

public class MainActivity extends ActionBarActivity { 
    ToggleButton toggle; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     android.support.v7.app.ActionBar menu = getSupportActionBar(); 
     menu.setDisplayShowHomeEnabled(true); 
     menu.setLogo(R.mipmap.ic_launcher); 
     menu.setDisplayUseLogoEnabled(true); 

    } 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     toggle = (ToggleButton) findViewById(R.id.togglebutton); 
     toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
       if (isChecked) { 
        enableNotifications(); 
       } else { 
        makeDialog(); 
       } 
      } 
     }); 
    } 
+0

,可以使用共享偏好爲 – Pavya

+0

Android提供了一個[很多選項(http://developer.android.com/guide/topics/ data/data-storage.html)。共享首選項是這種簡單存儲的方式。 – Glorfindel

+0

你可以使用共享首選項來存儲按鈕的切換狀態並在需要時重新填充 –

回答

0

使用共享的先前狀態的狀態喜歡相同的。

togButton.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        int notification_status = Integer.parseInt(mPreferences.getString("status", "-1")); 
        SharedPreferences.Editor editor=mPreferences.edit(); 
        Log.i("notification_status="+notification_status,"888"); 
        if(notification_status==-1){ 
          editor.putString("status", "1"); 
          holder.notification_btn.setBackgroundResource(R.drawable.notification_on); 
          //do your stuff 
        }else if(notification_status==1){ 
         editor.putString("status", "0"); 
         holder.notification_btn.setBackgroundResource(R.drawable.notification_off); 
         //do your stuff 
        }else if(notification_status==0){ 
         editor.putString("status", "1"); 
         holder.notification_btn.setBackgroundResource(R.drawable.notification_on); 
         //do your stuff 
        } 
        editor.commit(); 
       } 
      }); 
0

使用共享偏好如下類

public class YourPreferenes { 

    private Context mContext; 
    private SharedPreferences mPrefs; 
    private static YourPreferenes mYourPreferenes; 
    private YourPreferenes(Context ctx){ 
     mContext = ctx; 
     mPrefs = mContext.getSharedPreferences("ATSPreferenes", Context.MODE_PRIVATE); 
    } 

    public static ATSPreferenes getInstance(Context ctx){ 
     if(mYourPreferenes == null){ 
      mYourPreferenes = new YourPreferenes(ctx); 
     } 
     return mYourPreferenes; 
    } 

    public void setButtonState(boolean btnstate){ 
     SharedPreferences.Editor editor = mPrefs.edit(); 
     editor.putBoolean("btnstate", btnstate); 
     editor.commit(); 
    } 

    public boolean getButtonState(){ 
     return mPrefs.getBoolean("btnstate", false); 
    } 
} 


And get your saved togalbutton stat even you kill your app.