在Android中单击按钮时更改布局的背景颜色

时间:2016-08-29 05:51:08

标签: android

我是Android的新手。如果我问一个愚蠢的问题,请饶恕我。

我的应用程序在线性布局中只包含一个按钮。要求是,我必须在按钮点击时更改我的应用程序的线性布局的背景颜色。默认情况下,它是 WHITE ,当我按下按钮时它应该变成一些随机颜色,当我再次按下按钮时,它应该再次变为默认颜色(白色)。

button.setBackgroundColor(Color.BLUE)(在 OnClick()方法上),将背景颜色更改为蓝色,但如何恢复默认颜色?

8 个答案:

答案 0 :(得分:1)

drawble 文件夹

中创建xml文件

<强> change_colcor.xml

<?xml version="1.0" encoding="utf-8"?> 


<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_focused="true"
    android:state_pressed="true"
    android:color="#222222"  />
 <item
    android:state_focused="false"
    android:state_pressed="true"
    android:color="#4aa5d4"  />
</selector>

然后将XML文件设置为按钮背景

<Button
android:id="@+id/mybutton"
android:background="@drawable/change_colcor" />

答案 1 :(得分:1)

我希望这段代码可以帮到你!单击按钮时取布尔变量。

public class MainActivity extends Activity {
  boolean iscolor = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.btn);
    final LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);

    btn.setOnClickListener(new OnClickListener() {

     @Override
        public void onClick(View view) {

         if(iscolor)
         {
             layout.setBackgroundColor(Color.BLUE);
             iscolor = false;
         }
         else
         {
             layout.setBackgroundColor(Color.WHITE);
             iscolor = true;
         }

        }
    });
}

答案 2 :(得分:0)

创建名为button_pressed.xml的形状,如下所示....

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <solid android:color="@color/blue" />

    <stroke
        android:width="4dp"
        android:color="@color/blue" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

</shape>

假设您有idR.id.btnR.id.btn1的拖车按钮,如下所示......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="12dp"
        android:background="@drawable/button_pressed"
        android:onClick="onClick"
        android:text="Press Me 1" />        


</LinearLayout>

按如下方式编写onClick()方法...这将允许您保留更改的颜色,直到按下另一个按钮。

Button button;

public void onClick(View v) {

    Drawable dr = getResources().getDrawable(R.drawable.button_pressed);
    dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP);

    switch (v.getId()) {
    case R.id.btn:

        if (button == null) {
            button = (Button) findViewById(v.getId());
        } else {
            button.setBackgroundResource(R.drawable.button_pressed);
            button = (Button) findViewById(v.getId());
        }
        button.setBackgroundDrawable(dr);

        break;        


    default:
        break;
    }
}

Reference from here

答案 3 :(得分:0)

这是一个完全基于Java的方法来解决您的问题。它如下;

package com.example.param.background_changer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.EditText;
import android.graphics.Color;
import android.content.res.Resources;
import android.util.TypedValue;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



//Head
        //Layout type
        final RelativeLayout myLayout = new RelativeLayout(this);

            //Original Background color
            myLayout.setBackgroundColor(Color.BLUE);


//Body

            //Button
            final Button Changer_Button = new Button(this);

                //Button Properties
                Changer_Button.setText("Click Here");
                Changer_Button.setBackgroundColor(Color.GREEN);

                    //Position Properties
                        RelativeLayout.LayoutParams ChangerButton_Position_Details =
                                new RelativeLayout.LayoutParams(
                                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                                        RelativeLayout.LayoutParams.WRAP_CONTENT
                                );
                        ChangerButton_Position_Details.addRule(RelativeLayout.CENTER_HORIZONTAL);
                        ChangerButton_Position_Details.addRule(RelativeLayout.CENTER_VERTICAL);

        myLayout.addView(Changer_Button,ChangerButton_Position_Details);

            //On click listener
            Changer_Button.setOnClickListener(
                    new Button.OnClickListener() {

                        public void onClick(View v) {

                            Changer_Button.setText("Button Clicked");
                            myLayout.setBackgroundColor(Color.RED);
                        }
                    }
            );

        setContentView(myLayout);
    }
}

答案 4 :(得分:0)

尝试使用此代码:

android.graphics.drawable.GradientDrawable bg = new android.graphics.drawable.GradientDrawable();

bg.setColor(new android.content.res.ColorStateList(new int[][] {{-android.R.attr.state_pressed}, {android.R.attr.state_pressed}},new int[] {Color.parseColor("#901020"), Color.parseColor("#40109020")}));
bg.setShape(1);
bg.setCornerRadius(10);
bg.setStroke(2, new android.content.res.ColorStateList(new int[][] {{-android.R.attr.state_pressed}, {android.R.attr.state_pressed}}, new int[] {Color.parseColor("#505050"), Color.parseColor("#502010")}));

button1.setBackground(bg);

答案 5 :(得分:0)

activity_main.xml

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:id="@+id/ll"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:id="@+id/b1"></Button>
</LinearLayout>

MainActivity.java

package com.example.adouble;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
    Button b1;
    LinearLayout layout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1=findViewById(R.id.b1);
        layout=findViewById(R.id.ll);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            layout.setBackgroundColor(Color.RED);
                }
            }
        });
    }
}

答案 6 :(得分:0)

此代码将在单击按钮时在背景上生成随机颜色。默认情况下为白色,当按下按钮时,它将更改为某种随机颜色;再次按下按钮时,它将再次更改为默认颜色(白色)。

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

答案 7 :(得分:0)

将以下代码添加到布局中

android:addStatesFromChildren="true"