android基础教程之夜间模式实现示例
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>
推荐阅读
-
android基础教程之夜间模式实现示例
-
Android开发之选项卡功能的实现方法示例
-
Android开发之TextView使用intent传递信息,实现注册界面功能示例
-
Android开发之DatePicker和TimePicker实现选择日期时间功能示例
-
Android开发之ViewFlipper自动播放图片功能实现方法示例
-
JavaScript设计模式之观察者模式(发布订阅模式)原理与实现方法示例
-
Android开发之ToggleButton实现开关效果示例
-
C++设计模式之简单工厂模式的实现示例
-
android基础教程之夜间模式实现示例
-
Android开发之TextView使用intent传递信息,实现注册界面功能示例