自定义主题下Quiz App按钮背景色重置问题求助
Hey there! I see you're having trouble resetting your quiz buttons back to their original theme-specific drawables after showing the correct answer state. Let's break down how to fix this properly.
The Core Issue
Right now your resetColor() method is hardcoding R.drawable.button_custom, but since you've got multiple custom themes (each using button_custom1 to button_custom4 via the ?button theme attribute), this fixed ID won't match the actual drawable used by the current theme. Your previous attempts to parse the background drawable's string representation were unreliable because Drawable.toString() doesn't return a consistent resource ID string across Android versions.
The Correct Solution: Fetch from the Theme
Since you set the button background using the ?button theme attribute, the best way to get the original drawable is to directly resolve that attribute from the current theme. Here's how to update your resetColor() method:
import androidx.core.content.ContextCompat; import android.content.res.TypedValue; public void resetColor() { // Resolve the "button" attribute from the current theme to get its resource ID TypedValue typedValue = new TypedValue(); getTheme().resolveAttribute(R.attr.button, typedValue, true); // Get the actual drawable using the resolved resource ID Drawable originalButtonDrawable = ContextCompat.getDrawable(this, typedValue.resourceId); // Reset all buttons to the original theme drawable buttonA2.setBackground(originalButtonDrawable); buttonB2.setBackground(originalButtonDrawable); buttonC2.setBackground(originalButtonDrawable); buttonD2.setBackground(originalButtonDrawable); }
Key Explanations
resolveAttribute(): This method looks up the value of the?buttonattribute in your currently applied theme, giving you the resource ID of the drawable (likeR.drawable.button_custom1for Theme.Custom1).ContextCompat.getDrawable(): Using this instead ofgetResources().getDrawable()ensures compatibility across older Android versions (API 21+).- Reusability: You can even extract this logic into a helper method if you need to use it elsewhere:
Then call it inprivate Drawable getOriginalButtonDrawable() { TypedValue typedValue = new TypedValue(); getTheme().resolveAttribute(R.attr.button, typedValue, true); return ContextCompat.getDrawable(this, typedValue.resourceId); }resetColor():Drawable originalDrawable = getOriginalButtonDrawable(); buttonA2.setBackground(originalDrawable); // ... repeat for other buttons
Quick Check
Make sure you have the button attribute defined in your res/values/attrs.xml file (you probably already do since your theme uses it, but just in case):
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="button" format="reference" /> <!-- Other custom attributes if you have them --> </resources>
This approach will automatically adapt to whatever theme the user has selected, so it works for all your button_custom1 to button_custom4 drawables without hardcoding any IDs.
内容的提问来源于stack exchange,提问作者Heaf

