Apache Royale中j:Button无enabled属性,如何用AS3代码控制启用/禁用?
Got it, let's walk through how to toggle your j:Button's enabled state using AS3. Since you’ve already added the j:Disabled bead in your MXML, the key is to access that bead in your ActionScript code and adjust its property.
Step-by-Step Implementation
Reference your button instance
If your button has the IDbp_next(like in your MXML snippet), you can grab it directly in your AS3 code (assuming it’s in the same scope):var nextButton:Button = bp_next as Button;Retrieve the Disabled bead
Use thegetBeadByType()method to fetch theDisabledbead attached to the button:var disabledBead:Disabled = nextButton.getBeadByType(Disabled) as Disabled;Toggle the enabled state
Now you can control whether the button is enabled or disabled by setting thedisabledproperty of the bead:- To enable the button:
if (disabledBead) { disabledBead.disabled = false; } - To disable the button:
if (disabledBead) { disabledBead.disabled = true; }
- To enable the button:
Full Example Code
Here’s a complete snippet you can use to wrap this logic into a reusable function:
// Don't forget to import the required classes import org.apache.flex.html.Button; import org.apache.flex.html.beads.Disabled; private function setButtonEnabled(button:Button, isEnabled:Boolean):void { var disabledBead:Disabled = button.getBeadByType(Disabled) as Disabled; if (disabledBead) { disabledBead.disabled = !isEnabled; } } // Usage: Enable the "Next" button setButtonEnabled(bp_next, true); // Usage: Disable the "Next" button setButtonEnabled(bp_next, false);
A quick reminder: Make sure you’ve imported the correct Button and Disabled classes to avoid compilation errors. Since you already added the j:Disabled bead in MXML, the bead will exist on the button instance once it’s initialized, so the getBeadByType() call should return a valid reference.
内容的提问来源于stack exchange,提问作者Fred

