gpt4 book ai didi

java - 开关机一次后按钮停止工作

转载 作者:行者123 更新时间:2023-11-30 00:53:30 25 4
gpt4 key购买 nike

应用程序有一个图像按钮,可以打开和关闭屏幕亮度,但在打开和关闭一次后,该按钮不再起作用。有谁知道代码有什么问题吗?

import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;

public class ScreenFlashlight extends AppCompatActivity {
private static String tag;
private static final String TAG = tag ;
private boolean brightnessOnOff;
private ImageButton screenFlashOnOffButton;

public ScreenFlashlight() throws Settings.SettingNotFoundException {
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_flashlight);
screenFlashOnOffButton = (ImageButton) findViewById(R.id.flashOnOffButton);
brightnessOnOff = false;


}
public void screenFlashButtonClicked(View view) {

try {
if (brightnessOnOff) {

turnBrightnessOff();
Log.d(TAG, "screenFlashButtonClicked: ");
} else {
brightnessOnOff = true;
turnBrightnessOn();
Log.d(TAG, "screenFlashButtonClicked: ");
}
} catch (Exception e) {
e.printStackTrace();
}

}



private void turnBrightnessOn() {
try {
screenFlashOnOffButton.setImageResource(R.drawable.screenonbutton);
int currentBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
//Screen refresh to add brightness

WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = currentBrightness/ 100.0f;
getWindow().setAttributes(lp);


} catch (Exception e) {
e.printStackTrace();
}
}

private void turnBrightnessOff() {
try {
screenFlashOnOffButton.setImageResource(R.drawable.screenoffbutton);
int currentBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
//Refresh screen

WindowManager.LayoutParams lpp = getWindow().getAttributes();
lpp.screenBrightness = currentBrightness;
getWindow().setAttributes(lpp);
} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

当亮度关闭时,您永远不会将 brightnessOnOff 设置为 false,因此在您第一次按下按钮后它将具有值 true。所以你的代码每次都会进入if block 。您需要做的是在关闭亮度时将 brightnessOnOff 设置为 false,并在再次打开亮度时将其设置为 true。基本上,您需要在 truefalse 之间交替 brightnessOnOff 才能正常工作

关于java - 开关机一次后按钮停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40564559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com