You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义主题下Quiz App按钮背景色重置问题求助

Fixing Button Background Reset with Custom Themes in Your 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

  1. resolveAttribute(): This method looks up the value of the ?button attribute in your currently applied theme, giving you the resource ID of the drawable (like R.drawable.button_custom1 for Theme.Custom1).
  2. ContextCompat.getDrawable(): Using this instead of getResources().getDrawable() ensures compatibility across older Android versions (API 21+).
  3. Reusability: You can even extract this logic into a helper method if you need to use it elsewhere:
    private Drawable getOriginalButtonDrawable() {
        TypedValue typedValue = new TypedValue();
        getTheme().resolveAttribute(R.attr.button, typedValue, true);
        return ContextCompat.getDrawable(this, typedValue.resourceId);
    }
    
    Then call it in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 12:17:36