欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android基础教程之夜间模式实现示例

程序员文章站 2023-11-08 14:57:40
复制代码 代码如下:package org.david.dayandnightdemo.cor; import android.os.bundle;import andr...

复制代码 代码如下:

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>