//定义的类
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();
}
});