iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >android基础教程之夜间模式实现示例
  • 239
分享到

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

程之示例教程Android 2022-06-06 10:06:54 239人浏览 泡泡鱼
摘要

代码如下:package org.david.dayandnightdemo.cor; import Android.os.Bundle;import android.app

代码如下:
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 单例模式 Singleton 简单实例设计模式解析android开发设计模式之——单例模式详解Android单例模式的几种方法总结Android源码学习之观察者模式应用及优点介绍Android开发中的mvc设计模式浅析Android设计模式系列之组合模式Android设计模式系列之工厂方法模式Android设计模式之策略模式详解Android开发中的单例模式应用详解


--结束END--

本文标题: android基础教程之夜间模式实现示例

本文链接: https://www.lsjlt.com/news/27143.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作