英文:
Android Studio, Change the color of the background by pressing a switch button
问题
final Switch BackgroundColorButton = (Switch) findViewById(R.id.BackgroundColorButton);
BackgroundColorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (BackgroundColorButton.isChecked()) {
BackgroundColorButton.setText("白色模式");
// 在这里,我需要将应用程序的背景颜色更改为黑色或深灰色
} else {
BackgroundColorButton.setText("暗色模式");
// 在这里,我需要将应用程序的背景颜色更改为白色
}
}
});
请注意,上述代码中的注释已经被翻译为中文。
英文:
just started working in Android Studio, for most of the things I can find just fine everything with "Google it" but apparently for this thing I don't. I have a basic switch button that I want to change the app color background from white to black and back unchecking the button. The interrogation is really simple and works just fine changing the text of the switch from "dark mode" to "White mode".
The interrogation is done in MainActivity.java.
final Switch BackgroundColorButton = (Switch) findViewById(R.id.BackgroundColorButton);
BackgroundColorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(BackgroundColorButton.isChecked())
{
BackgroundColorButton.setText("White Mode");
//here I need to change the app background color to Black or DarkGray
}
else
{
BackgroundColorButton.setText("Dark Mode");
//here I need to change the app background color to White
}
}
});
答案1
得分: 1
在印度视频中找到了一个简短的解决方案,只需在activity_main.xml文件中添加以下代码行: //仅限以下部分
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rView" // 仅限以下部分
tools:context=".MainActivity">
在MainActivity.java文件中,以下部分:
screenView.setBackgroundColor(Color.BLACK);//仅限以下部分
screenView.setBackgroundColor(Color.WHITE);//仅限以下部分
英文:
Ok, did find on an Indian video.
the short solution, add activity main XML the line of code with //JUST THIS
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rView" // JUST THIS
tools:context=".MainActivity">
and in the mainactivity.java
final Switch BackgroundColorButton = (Switch) findViewById(R.id.BackgroundColorButton);
BackgroundColorButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(BackgroundColorButton.isChecked())
{
BackgroundColorButton.setText("White Mode");
screenView.setBackgroundColor(Color.BLACK);//JUST THIS
}
else
{
BackgroundColorButton.setText("Dark Mode");
screenView.setBackgroundColor(Color.WHITE);//JUST THIS
}
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论