开发者

togglebutton remember state

In AIR, I'm create a nativewindow component that will generate a set of togglebutton, as each time the code below is run whether I open a nativewindow, the togglebutton开发者_StackOverflow will be reset to the default state. How do I make the state persistent?

for(var i:int=0;i<10;++)
toggbtn.label = "Power "+1;
stage.addElement(toggbtn);
}


This is surprisingly (and happily) simple. Create a variable, like this:

var toggbtnVar:String;

Then, drop the following line of code into your button click event:

togglebtnVar = togglebtn.state;

In the above code, you are simply setting the string variable you created to the state of the togglebtn. The .state will be either "on" or "off".

Thus, to retrieve the last saved state of the button, just reverse the previous line of code.

togglebtn.state = togglebtnVar;

I will warn you, the above lines of code are UNTESTED, so you may have to tweak it a little. But you get the general idea. I'm fairly sure by inference that .state accepts a string, however, if it does not, you can set up the variable as a boolean, and set the state inside an if, else if statement.

Now, the only thing you have to do is ensure you save that variable. There are a number of ways to do that, so I'll leave that part of the research to you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜