android基礎(chǔ)教程之夜間模式實現(xiàn)示例
package org.david.dayandnightdemo.cor;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private WindowManager mWindowManager;
private View myView;
private Button btn_dayAndnight;
private SharedPreferences skinSp;
private final static String DAY = "day";
private final static String NIGHT = "night";
private int flage = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
setContentView(R.layout.activity_main);
init();
}
private void init() {
skinSp = this.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
btn_dayAndnight = (Button) findViewById(R.id.btn_dayAndnight);
btn_dayAndnight.setOnClickListener(this);
String mode = skinSp.getString("skin", "");
if(mode!=null||!mode.equals("")){
if(mode.equals(NIGHT)){
night();
}else{
day();
}
}
}
@Override
public void onClick(View v) {
if(flage%2==0){
night();
btn_dayAndnight.setText("白天模式");
btn_dayAndnight.setTextColor(Color.WHITE);
flage++;
}else{
day();
btn_dayAndnight.setText("夜間模式");
btn_dayAndnight.setTextColor(Color.BLACK);
flage++;
}
}
public void night() {
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity=Gravity.BOTTOM;
params.y=10;
if(myView==null){
myView=new TextView(this);
myView.setBackgroundColor(0x80000000);
}
mWindowManager.addView(myView, params);
Editor edit = skinSp.edit();
edit.putString("skin", NIGHT);
edit.commit();
}
public void day(){
if(myView!=null){
mWindowManager.removeView(myView);
Editor edit = skinSp.edit();
edit.putString("skin", DAY);
edit.commit();
}
}
public void removeSkin(){
if(myView!=null){
mWindowManager.removeView(myView);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
String mode = skinSp.getString("skin", "");
if(mode.equals(NIGHT)){
removeSkin();
}
}
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/btn_dayAndnight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn_changeskin" />
</RelativeLayout>
相關(guān)文章
自定義toast外形,多次點擊不會總是彈出toast的實現(xiàn)方法
下面小編就為大家?guī)硪黄远xtoast外形,多次點擊不會總是彈出toast的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android App調(diào)用MediaRecorder實現(xiàn)錄音功能的實例
這篇文章主要介紹了Android App調(diào)用MediaRecorder實現(xiàn)錄音功能的實例,MediaRecorder非常強(qiáng)大,不僅能夠用來錄制音頻還可以錄制視頻,需要的朋友可以參考下2016-04-04Android自定義view仿QQ的Tab按鈕動畫效果(示例代碼)
這篇文章主要介紹了Android自定義view仿QQ的Tab按鈕動畫效果(示例代碼),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考價值,需要的朋友可以參考下2021-01-01Android 桌面Widget開發(fā)要點解析(時間日期Widget)
總的來說,widget主要功能就是顯示一些信息。我們今天編寫一個很簡單的作為widget,顯示時間、日期、星期幾等信息。需要顯示時間信息,那就需要實時更新,一秒或者一分鐘更新一次2013-07-07關(guān)于Fragment?already?added問題的解決方案
這篇文章主要介紹了關(guān)于Fragment?already?added問題的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10一文詳解Android無需權(quán)限調(diào)用系統(tǒng)相機(jī)拍照
這篇文章主要為大家介紹了Android無需權(quán)限調(diào)用系統(tǒng)相機(jī)拍照詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03