时间:2021-05-20
复制代码 代码如下:
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>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Kotlin基础教程之类、对象、接口Kotlin中类、接口相关概念与Java一样,包括类名、属性、方法、继承等,如下示例:interfaceA{funbar()
基础教程介绍了基本概念,特别是对象和类。进阶教程对基础教程的进一步拓展,说明Python的细节。希望在进阶教程之后,你对Python有一个更全面的认识。之前我们
应朋友们反馈的Android基础薄弱的问题,决定出一套Android基础教程,帮助大家复习,巩固Android基础,今天要讲的是Android中的Intent实
Kotlin基础教程之数组容器ArraysKotlin标准库提供了arrayOf()创建数组,**ArrayOf创建特定类型数组valarray=arrayOf
kotlin基础教程之类和继承类声明使用class关键字声明类,查看其声明格式::modifiers("class"|"interface")SimpleNam