gpt4 book ai didi

java - 如何交换两个按钮的背景颜色?

转载 作者:行者123 更新时间:2023-12-01 16:51:43 25 4
gpt4 key购买 nike

我需要创建一个方法,以便当单击第一个按钮时,保存其 id 和背景颜色,当按下第二个按钮时,它们会更改它们之间的背景颜色。我怎样才能做到这一点?

我曾想过这样的事情:

public void changeColor(View v){
clicks = clicks++;
if (clicks == 1){
button1 = findViewById(v.getId());
color1 = button1.getBackground();
}
if (clicks == 2){
button2 = findViewById(v.getId());
color2 = button2.getBackground();
button1.setBackground(color2);
button2.setBackground(color1);
}
if (clicks == 2){
clicks = 0;
}
}

最佳答案

Incase of button you type boton so it can't work of use 
boton ensure this line begin of your code
button1 = boton1
button2 = boton3
and also import java.awt package in your code

public void changeColor(View v) {
clicks = clicks++;
if (clicks == 1){
button1 = findViewById(v.getId());
color1 = button1.getBackground();
}
else if (clicks == 2) {
button2 = findViewById(v.getId());
color2 = button2.getBackground();
button1.setBackground(color2);
button2.setBackground(color1);
clicks = 0;
}

关于java - 如何交换两个按钮的背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61671496/

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