android studio中带有颜色更改的按钮链接

android studio中带有颜色更改的按钮链接,android,button,colors,background,onclick,Android,Button,Colors,Background,Onclick,我正在开发我的应用程序,它即将完成,我想实现一个功能,用户点击一个按钮,背景就会改变颜色。我该怎么办?我希望有两个按钮彼此相邻,当单击时可以更改背景颜色。按钮一会将其更改为颜色或,按钮二会将其更改为颜色二。我试着把它编码进去,但它没有按照我想要的方式显示,所以我在这里问,因为我想从头开始按钮 有人能帮我解决这个问题吗 您可以按如下方式更改活动本身的背景颜色: 这是活动的XML文件:- <RelativeLayout xmlns:android="http://schemas.android

我正在开发我的应用程序,它即将完成,我想实现一个功能,用户点击一个按钮,背景就会改变颜色。我该怎么办?我希望有两个按钮彼此相邻,当单击时可以更改背景颜色。按钮一会将其更改为颜色或,按钮二会将其更改为颜色二。我试着把它编码进去,但它没有按照我想要的方式显示,所以我在这里问,因为我想从头开始按钮


有人能帮我解决这个问题吗

您可以按如下方式更改活动本身的背景颜色:

这是活动的XML文件:-

<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"
    tools:context=".MainActivity"
    android:id="@+id/backgroundId">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RED"
        android:id="@+id/redButton"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="70dp"
        android:onClick="onRedButtonClick"/>
        </RelativeLayout>

希望这有帮助。

发布您的尝试,以便我们可以帮助您找出您做错了什么。否则,这篇文章对SOCheck来说太宽泛了,请将代码放入按钮单击侦听器
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onRedButtonClick(View view) {
        RelativeLayout background = (RelativeLayout) findViewBy(R.id.backgroundId);
        background.setBackgroundColor(Color.RED);
    }