android 夜間模式
阿新 • • 發佈:2019-02-18
SharedPreferrenceHelper
public class SharedPreferrenceHelper { private static final String THEME = "theme"; public static void settheme(Context context, String theme){ SharedPreferences sp = context.getSharedPreferences("demo",Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); editor.putString(THEME,theme); editor.apply(); } public static String gettheme(Context context){ SharedPreferences sp = context.getSharedPreferences("demo",Context.MODE_PRIVATE); return sp.getString(THEME,"1"); } }
utils
public class utils { public static int getAppTheme(Context context){ String value = SharedPreferrenceHelper.gettheme(context); switch (Integer.valueOf(value)){ case 1: return R.style.AppTheme; case 2: return R.style.AppThemeDark; default: return R.style.AppTheme; } } public static void switchAppTheme(Context context){ String value = SharedPreferrenceHelper.gettheme(context); switch (Integer.valueOf(value)){ case 1: SharedPreferrenceHelper.settheme(context,"2"); break; case 2: SharedPreferrenceHelper.settheme(context,"1"); break; default: SharedPreferrenceHelper.settheme(context,"1"); break; } } }
BaseActivity
public abstract class BaseActivity extends FragmentActivity { TextView mTextView; private int theme = 0; private static final String TAG = "MainActivity"; @Override protected void onResume() { Log.d(TAG,"onResume"); super.onResume(); if(theme==utils.getAppTheme(this)){ }else{ reload(); } } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG,"onDestroy"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("theme",theme); } @Override protected void onCreate(Bundle savedInstanceState) { if(savedInstanceState==null){ theme=utils.getAppTheme(this); }else{ theme=savedInstanceState.getInt("theme"); } setTheme(theme); super.onCreate(savedInstanceState); Log.d(TAG,"onCreate"); //setContentView(R.layout.activity_main); setView(); } abstract void setView(); public void reload() { Intent intent = getIntent(); overridePendingTransition(0, 0);//不設定進入退出動畫 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart"); } @Override protected void onPause() { super.onPause(); Log.d(TAG,"onPause"); } }style
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppThemeDark" parent="Theme.AppCompat"></style> </resources>
MainActivity
public class MainActivity extends BaseActivity { private Button btn1; @Override void setView() { setContentView(R.layout.activity_main); btn1= (Button) findViewById(R.id.btn); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { utils.switchAppTheme(MainActivity.this); reload(); } }); } }