Android Studio,通过按下开关按钮更改背景颜色。

huangapple go评论86阅读模式
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:id=&quot;@+id/rView&quot; // JUST THIS
    tools:context=&quot;.MainActivity&quot;&gt;

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(&quot;White Mode&quot;);
                        screenView.setBackgroundColor(Color.BLACK);//JUST THIS

                    }
                    else
                    {
                        BackgroundColorButton.setText(&quot;Dark Mode&quot;);
                        screenView.setBackgroundColor(Color.WHITE);//JUST THIS
                    }
                }
            });

huangapple
  • 本文由 发表于 2020年7月22日 23:10:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63037512.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定