Add the Sharedpreference flag to remember the last style you chose, so you don't have to reset it the next time you start.
Package Com.zms.nightstyle;import Android.app.activity;import Android.content.context;import Android.content.sharedpreferences;import Android.content.sharedpreferences.editor;import Android.os.Bundle; Import Android.view.view;import Android.widget.button;import Android.widget.toast;public class Main extends Activity { Private Boolean isnight = false; Private Button Btnset; Private Button Btnget; Private Sharedpreferences sharedpreferences; @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Sharedpreferences = getsharedpreferences ("Usestyle", context.mode_world_readable); Isnight = Sharedpreferences.getboolean ("Isnight", false); if (isnight) {this.settheme (r.style.mythemenight); } else {this.settheme (r.style.mythemedefault); } setcontentview (R.layout.main); Btnset = (Button) Findviewbyid (R.id.btnset); Btnget = (Button) Findviewbyid (r.id.btnGet); Btnset.setonclicklistener (New Onclicklistenerimp ()); Btnget.setonclicklistener (New Onclicklistenerimp ()); } class Onclicklistenerimp implements View.onclicklistener {@Override public void OnClick (View v) { if (v = = btnset) {Editor editor = Sharedpreferences.edit (); if (isnight) {setTheme (R.style.mythemedefault); Isnight = false; } else {setTheme (r.style.mythemenight); Isnight = true; } editor.putboolean ("Isnight", isnight); Editor.commit (); Setcontentview (R.layout.main); Btnset = (Button) Findviewbyid (R.id.btnset); Btnget = (Button) Findviewbyid (r.id.btnget); Btnset.setonclicklistener (New Onclicklistenerimp ()); Btnget.setonclicklistener (New Onclicklistenerimp ()); } else if (v = = BtngeT) {Toast.maketext (Main.this, "isnight:" + isnight, Toast.length_short). Show (); } } }}
Two styles of themes:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <!--default Theme-- <style name= " Mythemedefault "parent=" @android: Style/theme "> <item name=" Btncolor "> #00ff00 </item> < Item Name= "Mainbackground" > #ffffff </item> <item name= "Maintextcolor" > #000000 </item> <item name= "TextString" > Default theme </item> </style> <!--night Theme-- <style name = "Mythemenight" parent= "@android: Style/theme" > <item name= "Btncolor" > #0000ff </item> <item name= "Mainbackground" > #000000 </item> <item name= "Maintextcolor" > #ffffff </item > <item name= "TextString" > Night theme </item> </style></resources>
Layout file:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:background="? MainBackgrou nd "android:orientation=" vertical "> <textview android:layout_width=" fill_parent "Android:layout_ height= "Wrap_content" android:text= "TextString"/> <imageview android:id= "@+id/ivbook" Android Oid:layout_width= "62dip" android:layout_height= "42dip" android:layout_gravity= "center" android:layout _margintop= "0dip" android:gravity= "center" android:src= "? Btncolor"/> <button android:id= "@+ Id/btnset "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_ma rgintop= "50DP" android:text= "Change Theme"/> <button android:id= "@+id/btnget" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_margintop= "50DP" android:text= "Get Flag"/></linearlayout>
Reprint Please specify source: Zhou Mushi's CSDN blogHttp://blog.csdn.net/zhoumushui
my github: Zhou Mushi's GitHubHttps://github.com/zhoumushui
Android switch app style, night mode