运行时创建Button:如何设置点击监听器实现其他按钮背景变色
Got it, let's tweak your code to get the behavior you want—when a button is clicked, all other buttons change their background color. Here's the adjusted implementation with clear explanations:
Step 1: Save all created Buttons in a collection
First, we need a way to reference every Button we create later in the click listener. An ArrayList<Button> works perfectly for this, since it lets us easily iterate through all buttons when a click happens.
Full Modified Code
// Declare this outside your loop to keep track of all buttons ArrayList<Button> buttonList = new ArrayList<>(); for(int j = 0; j < arrayName.length; j++) { // Create Button instance final Button btn = new Button(this); // Set your button properties (layout, text, ID) btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); btn.setText(arrayName[j]); btn.setId(j); // Add the button to our collection before setting the click listener buttonList.add(btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Loop through every button in our list for(Button button : buttonList) { // Check if this is NOT the button that was clicked if(button.getId() != v.getId()) { // Change the background color of non-clicked buttons // Replace Color.GRAY with your desired color button.setBackgroundColor(Color.GRAY); } else { // Optional: Reset the clicked button to its original background // Use your default button color here, e.g., Color.WHITE button.setBackgroundColor(Color.WHITE); } } Toast.makeText(ProductPage.this, btn.getText().toString(), Toast.LENGTH_SHORT).show(); } }); // Add the button to your layout buttonLayout.addView(btn); }
Key Details to Note
- Why the ArrayList? Without storing references to all buttons, your click listener can't access the other buttons created in the loop. The collection lets us iterate through every button later to update their backgrounds.
- ID Check: We use
button.getId() != v.getId()to skip the clicked button itself—so only the other buttons get their background changed. - Flexibility: Adjust the color values (like
Color.GRAYorColor.WHITE) to match your app's design system. You could even define these as class-level variables for easier global updates.
Optional Optimization
If you want to reset all buttons to their default background before changing the non-clicked ones (to avoid leftover color changes from previous clicks), adjust the click listener like this:
@Override public void onClick(View v) { // First reset all buttons to default color for(Button button : buttonList) { button.setBackgroundColor(Color.WHITE); // Default color } // Now change the background of all buttons except the clicked one for(Button button : buttonList) { if(button.getId() != v.getId()) { button.setBackgroundColor(Color.GRAY); } } Toast.makeText(ProductPage.this, btn.getText().toString(), Toast.LENGTH_SHORT).show(); }
内容的提问来源于stack exchange,提问作者Too Low
相关产品推荐
相关产品推荐

