2013-02-21 20 views
0

我想知道如何設置永久啓用的按鈕,以便每次退出應用程序時,它仍處於啓用狀態。在我的應用程序中有2個活動。 Activity1有2個按鈕。當您點擊第一個按鈕時,您將意圖進入包含按鈕的活動2。當您在活動2上單擊該按鈕時,您將返回活動1,然後activity1上的第二個按鈕將被設置爲啓用。如何設置永久啓用的按鈕?

我試過這個,但每次啓用按鈕時,都不會永久設置它。

首先我創建AppPreferences類

public class AppPreferences { 
     private static final String APP_SHARED_PREFS = "com.aydabtu.BroadcastSMS_preferences"; // Name of the file -.xml 
     private SharedPreferences appSharedPrefs; 
     private Editor prefsEditor; 

     public AppPreferences(Context context) 
     { 
      this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE); 
      this.prefsEditor = appSharedPrefs.edit(); 
     } 

     public boolean getOnOrOff() { 
      return appSharedPrefs.getBoolean("get_on_or_off", false); 

     } 

     public void setOnOrOFF(Boolean text) { 
      prefsEditor.putBoolean("get_on_or_off", text); 
      prefsEditor.commit(); 
     } 
} 

然後在我的按鈕,我給自己定的代碼

AppPreferences appPrefs = new AppPreferences(getApplicationContext()); 
    Button page2 = (Button) findViewById(R.id.button2); 

    Intent intent2=getIntent(); 
    String isEnabled2 = intent2.getStringExtra("isEnabled2"); 
    if(isEnabled2==null||isEnabled2.equals("disabled")){ 
      page2.setEnabled(false); 
    } 
    else{ 
      page2.setEnabled(appPrefs.getOnOrOff()); 
      appPrefs.setOnOrOFF(true); 
    } 

    page2.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 

      Intent myIntent = new Intent(view.getContext(), p3.class); 
      startActivityForResult(myIntent, 0); 

     } 
    }); 

回答