Android Studio实现按钮按下后保持颜色至下一个按钮触发
Hey there! I see you're trying to make your xylophone buttons stay highlighted (green) after being pressed, until another button is tapped. Your current drawable only changes color during the press action, which is why it doesn't stick. Let's fix this step by step:
Step 1: Create a Stateful Drawable Selector
First, replace your existing my_button.xml with a selector that handles normal and selected states. Create a new file button_state_selector.xml in your drawable directory:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Selected state (stays green when tapped) --> <item android:drawable="@color/green" android:state_selected="true"/> <!-- Default state (red when not selected) --> <item android:drawable="@color/red"/> </selector>
Make sure you define the colors red and green in your res/values/colors.xml file (if you haven't already):
<color name="red">#FF0000</color> <color name="green">#00FF00</color>
Step 2: Update Your Button XML
In your activity_main.xml, update each button's android:background to use the new selector. You can also remove the old my_button reference:
<Button android:id="@+id/button_do" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginTop="10dp" android:layout_marginBottom="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:elevation="0dp" android:background="@drawable/button_state_selector" <!-- Updated here --> android:textColor="@android:color/white" android:onClick="playSound" <!-- Renamed for consistency --> android:text="DO" />
Repeat this update for all your buttons (button_re, button_mi, etc.). I renamed the click handler to playSound for better consistency—we'll use this single method for all buttons.
Step 3: Handle State Logic in MainActivity
Now, we need to track the currently selected button and update states when a new button is tapped. Here's how to implement this in your MainActivity.java:
import android.media.MediaPlayer; import android.os.Bundle; import android.view.View; import android.widget.Button; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { private Button currentSelectedButton; // Tracks the last pressed button @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void playSound(View view) { // Reset the previously selected button (if any) if (currentSelectedButton != null) { currentSelectedButton.setSelected(false); } // Set the current button as selected Button pressedButton = (Button) view; pressedButton.setSelected(true); currentSelectedButton = pressedButton; // Play the corresponding sound (replace with your sound logic) int soundResource = 0; switch (pressedButton.getId()) { case R.id.button_do: soundResource = R.raw.do_sound; // Replace with your actual sound file break; case R.id.button_re: soundResource = R.raw.re_sound; break; case R.id.button_mi: soundResource = R.raw.mi_sound; break; // Add cases for other buttons if needed } // Play the sound if (soundResource != 0) { MediaPlayer mediaPlayer = MediaPlayer.create(this, soundResource); mediaPlayer.start(); mediaPlayer.setOnCompletionListener(MediaPlayer::release); // Release resource after playback } } }
Key Notes:
- We use
setSelected(true)to trigger the green state in our drawable selector. - When a new button is pressed, we first deselect the previous button (
setSelected(false)), then select the new one. - The sound playback logic is included here—make sure to replace the
R.raw.*references with your actual sound files in theres/rawdirectory.
Testing It Out
Run your app now: when you tap a button, it should turn green and stay that way until you tap another button (which will turn green, and the previous one reverts to red). Perfect for your xylophone app!
内容的提问来源于stack exchange,提问作者Artur de Costres

