![](/img/trans.png)
[英]How to change background color of ImageView in Android using java code
[英]How to change the color of a button in android using JAVA?
我正在开发一个ANDROID项目,其中我有多个按钮。
要求:-
1.当我点击一个按钮时,我希望它变成绿色,当我再次点击同一个按钮时,我希望它变回原来的颜色。
2.当一个按钮被选中(即颜色变为绿色)时,我想在数组列表中添加一个数字,当我再次选择它时,我想从列表中删除添加的数字。
到目前为止,我已经提出了这个解决方案。 但它不起作用。
int flag=1;
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag+=1;
if(flag%2==0) {
SeatArrayList.remove(Integer.valueOf(1));
button1.setBackgroundColor(Color.GREEN);
}
else{
SeatArrayList.add(1);
button1.setBackgroundColor(bg_yellow);
}
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag+=1;
if(flag%2==0) {
SeatArrayList.remove(Integer.valueOf(3));
button3.setBackgroundColor(Color.GREEN);
}
else{
SeatArrayList.add(3);
button3.setBackgroundColor(bg_yellow);
}
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
flag+=1;
if(flag%2==0) {
SeatArrayList.remove(Integer.valueOf(4));
button4.setBackgroundColor(Color.GREEN);
}
else{
SeatArrayList.add(4);
button4.setBackgroundColor(bg_yellow);
}
}
});
您可以使用布尔标志轻松管理此问题,这里有一个相同的示例!
boolean isGreen = false;
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isGreen){
isGreen = false
SeatArrayList.removeAt(SeatArrayList.size-1)
button1.setBackgroundColor(Color.YELLOW);
}else{
isGreen = true
SeatArrayList.add(1);
button1.setBackgroundColor(Color.GREEN);
}
}
});
您应该为每个按钮创建一个标志。 我认为以下代码可以满足您在要求中提到的功能:
1.XML文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="@android:color/black" android:text="Button 1"> </Button> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="@android:color/black" android:text="Button 2"> </Button> <Button android:id="@+id/btn3" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="@android:color/black" android:text="Button 3"> </Button> <TextView android:id="@+id/txtNumbers" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="{ }" android:textSize="18sp"> </TextView> </LinearLayout>
2. MainActivity Java 文件:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn1, btn2, btn3;
private TextView txtNumbers;
private ArrayList<Integer> list;
private ArrayList<Boolean> isGreen;
private int initialColor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
txtNumbers = (TextView) findViewById(R.id.txtNumbers);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
list = new ArrayList<>();
isGreen = new ArrayList<>(Collections.nCopies(3, false));//Initialize values to false (In this case for only 3 buttons)
initialColor = Color.BLACK;//The same color as in the XML file
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn1:
requirement(btn1, 0, 0);
break;
case R.id.btn2:
requirement(btn2, 1, 1);
break;
case R.id.btn3:
requirement(btn3, 2, 2);
break;
}
}
private void requirement(Button btn, int index, int number) {
if (!isGreen.get(index)) {
btn.setBackgroundColor(Color.GREEN);
list.add(number);
printList();
isGreen.set(index, true);
} else {
btn.setBackgroundColor(initialColor);
list.remove((Integer) number);//Cast to Integer, because this will eliminate the number by its value, not index
printList();
isGreen.set(index, false);
}
}
private void printList() {
String numbers = "{ ";
for (int index = 0; index < list.size(); index++) {
numbers += list.get(index) + ", ";
}
numbers += " }";
txtNumbers.setText(numbers);
}
}
结果:
我希望它对你有用。 我继续关注,问候😁/。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.