<span style="font-size:18px;"><code class="hljs java has-numbering"><span class="hljs-keyword">import</span> android.support.v7.app.ActionBarActivity; <span class="hljs-keyword">import</span> android.content.Context; <span class="hljs-keyword">import</span> android.content.SharedPreferences; <span class="hljs-keyword">import</span> android.content.SharedPreferences.Editor; <span class="hljs-keyword">import</span> android.graphics.Color; <span class="hljs-keyword">import</span> android.graphics.PixelFormat; <span class="hljs-keyword">import</span> android.os.Bundle; <span class="hljs-keyword">import</span> android.view.Gravity; <span class="hljs-keyword">import</span> android.view.View; <span class="hljs-keyword">import</span> android.view.View.OnClickListener; <span class="hljs-keyword">import</span> android.view.WindowManager; <span class="hljs-keyword">import</span> android.view.WindowManager.LayoutParams; <span class="hljs-keyword">import</span> android.widget.Button; <span class="hljs-keyword">import</span> android.widget.TextView; <span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">ActionBarActivity</span> {</span> <span class="hljs-comment">// 获得窗体的管理者</span> <span class="hljs-keyword">private</span> WindowManager mWindowManager; <span class="hljs-keyword">private</span> Button dayandnight; <span class="hljs-keyword">private</span> SharedPreferences skinSp; <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> flag = <span class="hljs-number">0</span>; <span class="hljs-keyword">private</span> View myView; <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> String DAY = <span class="hljs-string">"day"</span>; <span class="hljs-keyword">private</span> <span class="hljs-keyword">final</span> <span class="hljs-keyword">static</span> String NIGHT = <span class="hljs-string">"night"</span>; <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span>(Bundle savedInstanceState) { <span class="hljs-keyword">super</span>.onCreate(savedInstanceState); <span class="hljs-comment">// 获得系统服务的窗体服务 在加载视图之前实现</span> mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);<span class="hljs-comment">// 根据上下文获得窗体的服务</span> setContentView(R.layout.activity_main); init(); } <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span>() { <span class="hljs-comment">// 获得轻量级存储类</span> skinSp = <span class="hljs-keyword">this</span>.getSharedPreferences(<span class="hljs-string">"skinchange"</span>, MODE_PRIVATE);<span class="hljs-comment">// 私有模式</span> dayandnight = (Button) findViewById(R.id.dayandnight); dayandnight.setOnClickListener(<span class="hljs-keyword">new</span> OnClickListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClick</span>(View v) { <span class="hljs-keyword">if</span> (flag % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>) {<span class="hljs-comment">// 这个是白天的模式</span> night(); dayandnight.setText(<span class="hljs-string">"白天模式"</span>); dayandnight.setTextColor(Color.WHITE); flag++; } <span class="hljs-keyword">else</span> { Day(); dayandnight.setText(<span class="hljs-string">"夜间模式"</span>); dayandnight.setTextColor(Color.BLACK); flag++; } } }); <span class="hljs-comment">// 获得是夜间模式还是白天的模式</span> String mode = skinSp.getString(<span class="hljs-string">"skin"</span>, <span class="hljs-string">""</span>); <span class="hljs-keyword">if</span> (mode != <span class="hljs-keyword">null</span> || !mode.equals(<span class="hljs-string">""</span>)) { <span class="hljs-keyword">if</span> (mode.equals(NIGHT)) { night(); } <span class="hljs-keyword">else</span> { Day(); } } } <span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">night</span>() { WindowManager.LayoutParams params = <span class="hljs-keyword">new</span> WindowManager.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT);<span class="hljs-comment">// 设置的五个参数</span> params.gravity = Gravity.BOTTOM; params.y = <span class="hljs-number">10</span>; <span class="hljs-keyword">if</span> (myView == <span class="hljs-keyword">null</span>) { myView = <span class="hljs-keyword">new</span> TextView(<span class="hljs-keyword">this</span>); myView.setBackgroundColor(<span class="hljs-number">0x80000000</span>); } mWindowManager.addView(myView, params); Editor edit = skinSp.edit(); edit.putString(<span class="hljs-string">"skin"</span>, NIGHT); edit.commit(); } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Day</span>() { <span class="hljs-keyword">if</span> (myView != <span class="hljs-keyword">null</span>) { mWindowManager.removeView(myView); Editor edit = skinSp.edit(); edit.putString(<span class="hljs-string">"skin"</span>, DAY); edit.commit(); } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">removeSkin</span>() { <span class="hljs-keyword">if</span> (myView != <span class="hljs-keyword">null</span>) { mWindowManager.removeView(myView); } } <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onDestroy</span>() { <span class="hljs-keyword">super</span>.onDestroy(); String mode = skinSp.getString(<span class="hljs-string">"skin"</span>, <span class="hljs-string">""</span>); <span class="hljs-keyword">if</span> (mode.equals(NIGHT)) { removeSkin(); } } }</code></span>
白天夜间模式切换(2)
最新推荐文章于 2022-08-24 12:15:58 发布