单击按钮并更改屏幕一半的颜色

时间:2022-11-20 13:58:20

So, i have 1 screen that is cut in 2 parts. First part contain 4 buttons(red, blue, green, purple) and the second is an empty space. What i want is, when i press 1 of the button, i want to color that corresponds to show up in the empty space.

所以,我有1个屏幕,分为2部分。第一部分包含4个按钮(红色,蓝色,绿色,紫色),第二部分是空白区域。我想要的是,当我按下按钮1时,我想要对应于显示在空白区域的颜色。

how do i create the connection between the buttons and the empty space

如何在按钮和空白区域之间创建连接

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="2"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5">
    <Button
        android:layout_width="200sp"
        android:layout_height="50sp"
        android:id="@+id/button1"
        android:text="Red"/>


    <Button
        android:layout_width="200sp"
        android:layout_height="50sp"
        android:id="@+id/button2"
        android:layout_toRightOf="@+id/button1"
        android:text="Blue"/>
    <Button
        android:layout_width="200sp"
        android:layout_height="50sp"
        android:id="@+id/button3"
        android:layout_below="@+id/button1"
        android:text="Green"/>
    <Button
        android:layout_width="200sp"
        android:layout_height="50sp"
        android:id="@+id/button4"
        android:layout_toRightOf="@+id/button3"
        android:layout_below="@+id/button2"
        android:text="Purple"/>
    </RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.5"
    android:id="@+id/bg2"
    android:background="@drawable/selector">


</RelativeLayout>
</LinearLayout>

3 个解决方案

#1


0  

In your onCreate add

在你的onCreate添加

Button button1 = (Button)findViewById(R.id.button1);
RelativeLayout rel =(RelativeLayout)findViewById(R.id.bg2);
        button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            rel.setBackgroundColor(Color.RED);           
           }
        });

#2


0  

You should create two fragments in this activity , and create a meme two relate this two fragment, so this two fragments can communicate and then you can send and receive data between the two fragments

你应该在这个活动中创建两个片段,并创建一个meme两个关联这两个片段,所以这两个片段可以通信,然后你可以在两个片段之间发送和接收数据

#3


0  

Have a look at this question:

看看这个问题:

set background color: Android

设置背景颜色:Android

You basically follow the same steps, except you invoke the corresponding setBackgroundColor(Color.parseColor("xyz")); to the button's color as the button's onClick() is invoked.

你基本上遵循相同的步骤,除了你调用相应的setBackgroundColor(Color.parseColor(“xyz”));当按钮的onClick()被调用时,按钮的颜色。

#1


0  

In your onCreate add

在你的onCreate添加

Button button1 = (Button)findViewById(R.id.button1);
RelativeLayout rel =(RelativeLayout)findViewById(R.id.bg2);
        button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            rel.setBackgroundColor(Color.RED);           
           }
        });

#2


0  

You should create two fragments in this activity , and create a meme two relate this two fragment, so this two fragments can communicate and then you can send and receive data between the two fragments

你应该在这个活动中创建两个片段,并创建一个meme两个关联这两个片段,所以这两个片段可以通信,然后你可以在两个片段之间发送和接收数据

#3


0  

Have a look at this question:

看看这个问题:

set background color: Android

设置背景颜色:Android

You basically follow the same steps, except you invoke the corresponding setBackgroundColor(Color.parseColor("xyz")); to the button's color as the button's onClick() is invoked.

你基本上遵循相同的步骤,除了你调用相应的setBackgroundColor(Color.parseColor(“xyz”));当按钮的onClick()被调用时,按钮的颜色。