gpt4 book ai didi

java - 如何检查我的代码中按钮的背景(在我设计的各种可绘制 xml 之间)以便我可以修改它?

转载 作者:行者123 更新时间:2023-12-02 05:45:17 24 4
gpt4 key购买 nike

button1.setOnClickListener(new View.OnClickListener() {

        @Override
public void onClick(View v) {
// TODO Auto-generated method stub


if (//button1 background is blue){

changeBackgroundColorRed();

}
else if (//button1 background is red){

changeBackgroundColorBlue();

}
}

});

changeBackgroundColor 方法正在工作,我只需要知道它们当前必须能够调用该方法的背景。

最佳答案

您可以根据您设置的颜色来设置按钮的标签,并让标签检查当前是蓝色还是红色。

在 onCreate 或类似内容中:

实例化按钮时,您需要首先将标签设置为初始颜色:

button.setTag("blue");

Onclick 内部

if (button.getTag().equals("blue")){ //background is currently blue
changeBackgroundColorRed();
button.setTag("red"); //set the Tag to red cause you change the background to red
}
else if (button.getTag().equals("red")){ //background is currently red
changeBackgroundColorBlue();
button.setTag("blue"); //set the Tag to blue cause you change the background to blue
}

关于java - 如何检查我的代码中按钮的背景(在我设计的各种可绘制 xml 之间)以便我可以修改它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24131040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com