简体   繁体   中英

How can I change one of my three buttons colors on click to blue but make it so that only one can be blue at a time?

I want to make it so that when I switch between buttons one button will be blue while the other two will remain gray. This means that if I click a new button that button should then become blue and the previously clicked buttons should go back to their original color.

Xml

<androidx.constraintlayout.widget.ConstraintLayout 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="horizontal"
  android:layout_gravity="center"
>

  <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="32dp"
      android:layout_marginLeft="32dp"
      android:layout_marginTop="172dp"
      android:text="Button"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

  <Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="136dp"
      android:layout_marginLeft="136dp"
      android:layout_marginTop="172dp"
      android:text="Button"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

  <Button
      android:id="@+id/button3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="236dp"
      android:layout_marginLeft="236dp"
      android:layout_marginTop="172dp"
      android:text="Button"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java

I can change all of the buttons colors to blue but I'm struggling to make it so that only one at a time is blue while the other two are their original rgb color.
 package com.example.scrolltes1; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = findViewById(R.id.button1); Button button2 = findViewById(R.id.button2); Button button3 = findViewById(R.id.button3); button1.setOnClickListener(this); button2.setOnClickListener(this); button3.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: v.setBackgroundColor(Color.BLUE); break; case R.id.button2: v.setBackgroundColor(Color.BLUE); break; case R.id.button3: v.setBackgroundColor(Color.BLUE); break; } } }
public void onClick(View v) {
    button1.setBackgroundColor(v.getId() == R.id.button1 ? Color.BLUE : Color.GREY);
    button2.setBackgroundColor(v.getId() == R.id.button2 ? Color.BLUE : Color.GREY);
    button3.setBackgroundColor(v.getId() == R.id.button3 ? Color.BLUE : Color.GREY);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM