白天夜间模式切换(2)

主类中的实现代码
这个切换模式不需要主题的选择切换

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
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 ActionBarActivity {
    // 获得窗体的管理者
    private WindowManager mWindowManager;
    private Button dayandnight;
    private SharedPreferences skinSp;
    private int flag = 0;
    private View myView;
    private final static String DAY = "day";
    private final static String NIGHT = "night";

    @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", MODE_PRIVATE);// 私有模式
        dayandnight = (Button) findViewById(R.id.dayandnight);
        dayandnight.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if (flag % 2 == 0) {// 这个是白天的模式
                    night();
                    dayandnight.setText("白天模式");
                    dayandnight.setTextColor(Color.WHITE);
                    flag++;
                } else {
                    Day();
                    dayandnight.setText("夜间模式");
                    dayandnight.setTextColor(Color.BLACK);
                    flag++;
                }
            }
        });
        // 获得是夜间模式还是白天的模式
        String mode = skinSp.getString("skin", "");
        if (mode != null || !mode.equals("")) {
            if (mode.equals(NIGHT)) {
                night();
            } else {
                Day();
            }
        }
    }

    private void night() {
        WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                WindowManager.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();
        }
    }
}

你可能感兴趣的:(白天夜间模式切换(2))