2017-08-08 33 views
0

我使用Utills類的主題改變點擊按鈕,有8個圖像背景的按鈕。這裏是我Utills點擊按鈕點擊改變整個應用程序的主題動態並存儲它

public class Utills { 

public static boolean isPurchsed =true; 
private static int sTheme; 
public final static int THEME_DEFAULT = 0; 
public final static int THEME_ONE = 1; 
public final static int THEME_TWO = 2; 
public final static int THEME_THREE = 3; 
public final static int THEME_FOUR = 4; 
public final static int THEME_FIVE = 5; 
public final static int THEME_SIX = 6; 
public final static int THEME_SEVEN = 7; 
/** 
* Set the theme of the Activity, and restart it by creating a new Activity of the same type. 
*/ 
public static void changeToTheme(Activity activity, int theme) 
{ 
    sTheme = theme; 
    activity.finish(); 
    activity.startActivity(new Intent(activity, activity.getClass())); 
} 
/** Set the theme of the activity, according to the configuration. */ 
public static void onActivityCreateSetTheme(Activity activity) 
{ 
    switch (sTheme) 
    { 
     default: 
     case THEME_DEFAULT: 
      activity.setTheme(R.style.MyTheme); 
      break; 
     case THEME_ONE: 
      activity.setTheme(R.style.MySecondTheme); 
      break; 
     case THEME_TWO: 
      activity.setTheme(R.style.MyThirdTheme); 
      break; 
     case THEME_THREE: 
      activity.setTheme(R.style.MyFourthTheme); 
      break; 
     case THEME_FOUR: 
      activity.setTheme(R.style.MyFifthTheme); 
      break; 
     case THEME_FIVE: 
      activity.setTheme(R.style.MySixthTheme); 
      break; 
     case THEME_SIX: 
      activity.setTheme(R.style.MySeventhTheme); 
      break; 
     case THEME_SEVEN: 
      activity.setTheme(R.style.MyLastTheme); 
      break; 
    } 
} 

這裏是活動類

public class ChangeBackgroundActivity extends AppCompatActivity { 


Button bck1, bck2, bck3, bck4, bck5, bck6,bck7,bck8; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    Utills.onActivityCreateSetTheme(this); 
    setContentView(R.layout.activity_change_background); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_changebackground); 
    setSupportActionBar(toolbar); 


    bck1 = (Button)findViewById(R.id.btn_bck1); 
    bck1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this, Utills.THEME_DEFAULT); 
     } 
    }); 

    bck2 = (Button)findViewById(R.id.btn_bck2); 
    bck2.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this, Utills.THEME_ONE); 
     } 
    }); 


    bck3 = (Button)findViewById(R.id.btn_bck3); 
    bck3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this, Utills.THEME_TWO); 
     } 
    }); 

    bck4 = (Button)findViewById(R.id.btn_bck4); 
    bck4.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this, Utills.THEME_THREE); 
     } 
    }); 
    bck5 = (Button)findViewById(R.id.btn_bck5); 
    bck5.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this, Utills.THEME_FOUR); 
     } 
    }); 
    bck6 = (Button)findViewById(R.id.btn_bck6); 
    bck6.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this, Utills.THEME_FIVE); 
     } 
    }); 
    bck7 = (Button)findViewById(R.id.btn_bck7); 
    bck7.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this, Utills.THEME_SIX); 
     } 
    }); 
    bck8 = (Button)findViewById(R.id.btn_bck8); 
    bck8.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Utills.changeToTheme(ChangeBackgroundActivity.this,Utills.THEME_SEVEN); 
     } 
    }); 

} 

當我關閉應用程序並重新啓動再設置整個應用程序的默認主題。 如何從Utills類中存儲應用程序的主題,所以無論何時關閉我的應用程序並重新啓動它,所選主題都將設置爲整個應用程序。

回答

1

你需要堅持你的主題。例如,您可以使用SharedPreferences。比retreive你的主題,並將其設置爲setContentView之前的活動。

所以。您需要添加保存到changeToTheme(Activity activity, int theme)和retreiving到onActivityCreateSetTheme(Activity activity)