夜间模式

//定义的类

package Night1;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;

public class Night {
// 定义变量
static WindowManager window;
private static SharedPreferences sp;
private final static String DAY = "day";
private final static String NIGHT = "night";
static Context con;
static View myView;
// 定义构造方法

public Night(WindowManager window, Context con) {
super();
this.window = window;
this.con = con;
}

// 创建黑夜的方法
public static 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 View(con);
myView.setBackgroundColor(0x80000000);
}
window.addView(myView, params);

//创建sp对象
SharedPreferences sp=con.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
Editor edit = sp.edit();
edit.putString("skin", NIGHT);
edit.commit();

}


public static void day() {
if (myView != null) {
window.removeView(myView);
SharedPreferences sp=con.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
Editor edit = sp.edit();
edit.putString("skin", DAY);
edit.commit();
}
}

}

 

 

///在主类里调用

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_fuction);
MyHelp myHelp = new MyHelp(FuctionActivity.this, "navs", null, 1);

ImageView night = (ImageView) findViewById(R.id.night);
text = (TextView) findViewById(R.id.day);
// 白天,黑夜的切换
sp = this.getSharedPreferences("skinchange", Context.MODE_PRIVATE);
String mode = sp.getString("skin", "");
nig = new Night(window, FuctionActivity.this);
if (mode != null || !mode.equals("")) {
if (mode.equals(NIGHT)) {
nig.night();
} else {
nig.day();
}
}
// 创建商品对象
SharedPreferences sha1 = getSharedPreferences("num", MODE_APPEND);
ed = sha1.edit();

night.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
SharedPreferences sha2 = getSharedPreferences("num",
MODE_APPEND);
int flage = sha2.getInt("aa", 0);
if (flage % 2 == 0) {
nig.night();
text.setText("白天模式");
text.setTextColor(Color.WHITE);
flage++;
} else {
nig.day();
text.setText("夜间模式");
text.setTextColor(Color.BLACK);
flage++;
}
ed.putInt("aa", flage);
ed.commit();
}

});

 

转载于:https://www.cnblogs.com/jsonfan/p/5299703.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值