我正在努力让Global Variable
为我的整个app
制作一个黑暗模式。但它似乎无法正常工作。
我已按照指南制作了Global Variable
。
以下是我到目前为止的情况:
GolobalVariables appState;
appState = ((GolobalVariables) this.getApplication());
这是我的on touch event
:
@Override
public void onLongPress(MotionEvent e) {
mScrollView.post(new Runnable() {
@Override
public void run() {
darkMode();
appState.setDarkModeOn(!appState.isDarkModeOn());
}
});
}
以下是darkMode method
:
private void darkMode() {
Log.d("DarkMode", "`" + appState.isDarkModeOn());
if(appState.isDarkModeOn()) {
maarivRelativeLayout.setBackgroundColor(Color.parseColor("#333333"));
for (int i = 0; i < maarivLinearLayout.getChildCount(); i++) {
((TextView) maarivLinearLayout.getChildAt(i)).setTextColor(Color.parseColor("#FFFFFF"));
}
} else {
maarivRelativeLayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
for (int i = 0; i < maarivLinearLayout.getChildCount(); i++) {
((TextView) maarivLinearLayout.getChildAt(i)).setTextColor(Color.parseColor("#333333"));
}
}
}
我更改boolean
的状态,以便在切换活动时变量保持不变。只有当用户长按屏幕时,才会切换。
发生了2个问题:
关闭并重新打开相同的活动时,Log语句在打开时会显示false,在长按时会再次显示false。真实而真实。
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Davening"/>
<activity android:name=".Benching"/>
<activity android:name=".Zmanim"/>
<activity android:name=".Extras"></activity>
<activity android:name=".Shacharis"></activity>
<activity android:name=".Mincha"></activity>
<activity android:name=".Maariv"></activity>
<activity android:name=".Compass"></activity>
<activity android:name=".TestingZone"></activity>
<!--Camera-->
<activity android:name=".CameraView"
android:label="@string/app_name"
android:screenOrientation="portrait">
<!-- configure this activity to use landscape orientation -->
</activity>
<!--<application android:theme="@style/Theme.AppCompat.Light.NoActionBar" />-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.camera.autofocus" /><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
应用索引。有关详细信息,请参阅https://g.co/AppIndexing/AndroidStudio。 - &GT;
这是我的Golbal变量类:
package com.example.edonfreiner.siddur;
import android.app.Application;
public class GolobalVariables extends Application{
public boolean darkModeOn = false;
public boolean isDarkModeOn() {
return darkModeOn;
}
public void setDarkModeOn(boolean darkModeOn) {
this.darkModeOn = darkModeOn;
}
}