Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The new spark ToggleSwitch component defines a typical tablet control for defining a binary switch such as an on/off switch for an application. Clicking anywhere inside of the control toggles the state. We can also slide our thumb along the track to change the state, and we can even set the duration for how long that lasts. The default values for the unselected and selected labels are off if they're unselected or on if they are selected. We can also easily change the labels shown by defining a custom skin.
Let's explore how to use the brand-new ToggleSwitch component to build truly compelling tablet-based applications. So the first thing I want you to do is just import the Flash Builder project. This is our 1-5.fxp file. So go ahead and just import that and then click Finish, and I'm just going to run this really quickly to show you what we have. Okay, here is our toggle switch component. We don't have any toggle switches added in in the starting files here, so for those of you who don't have the student files, you can just create a brand-new mobile project and add in the toggle switch components like we'll do in just a second, wherever you like, and you'll get the same gist.
So here I'm going to say mobile application. I'm going to build a mobile configuration for Apple iOS. We'll test it on our desktop, and we'll choose an iPad, and I'll run this. And what we're going to do is we're going to add some ToggleSwitch components, so we can switch whether or not our application is running on port 80 and whether or not it's using WiFi. It'll give the users the ability to slide on and off these two functionalities. So, very cool. And first thing I want you to do is just scroll down to where it says WiFi and add in a new ToggleSwitch component here.
So I'm just going to say s:ToggleSwitch, just like so, and I'm going to just give it an id of let's call it switch1. And then I'll specify my slideDuration. We'll try 100 milliseconds. You can play with it and see what works best for your application of course. And then I'll add a change event, and I'll just simply say change is equal to-- and let's create a new function here called toggleswitch_1, and we'll just call it change.
And then here, I'll make sure that of course I'm passing that the event object, just like so, and then I will close off my ToggleSwitch component. So now let's add in a script block right at the top of our application, and I'll just say fx:Script, just like so. I'll say protected function. I'll make sure that matches the same function, so I'll just say toggleswitch_1change, just like so. And I'll say event:Event, and then I'll specify void, because this method won't actually be returning anything, and there is my new ToggleSwitch change event.
Now let's also add a ToggleSwitch MXML directly under our port 80 here as well, and we'll just make this easy. We'll copy this same thing here, and I'll copy that over here, and I'll paste that directly under port 80 here, like so. And instead of calling it switch1, I'll call switch2, and I'll call the same event handler. So now go up here to my ToggleSwitch event handler and you'll see down below there is a label control with the id of LBL.
So let's just populate this lbl and I'll say .text will populate the text property that label control. And I'll say is equal to the event object. We'll just say target.id, so this will of course be whichever toggle switch component, whether it was 1 or 2, that I happen to click on. And I'll say plus, you can do a concatenation there, and I'll say selected is equal to, close that, and then I'll say plus event.target.selected, just like so. And now that should populate that with the toggle switch that I actually clicked on.
So let's explore that. I'll save this. You should see that it compiles without any problems. We'll go back to my run configuration. Make sure you have a Run Configuration for an iPad, click on Run, and you'll see here are my new toggle switch components. Very cool. Click here and you can see that it's either selected true or false, and again, you could see that also the port 80 is selected true or false. Now, we can actually skin our toggle switch component so we can make these look even better, and we can have a lot of creative control over how these switch components look.
So let's explore how to do that. And what I want you to do is just create a new package here called skins. So I'm just going to say New > Package, and we'll call this skins, and I'll click Finish. And then what I'm going to do is I'm going to build a new file called MyToggleSwitchSkin. So I'm going to simply say New, and we'll just say File here to make it easy. We'll see new file, and we'll simply call this MyToggleSwitchSkin.as, for ActionScript of course, and we will simply build a new ActionScript class for skinny.
So I'm just going to simply say package skins, like so, and then I'm going to say import spark.skins.mobile.ToggleSwitchSkin. So this is importing the existing skin that we're already using, so we don't have to re-create the wheel there. So we're going to import that, and then we're going to define our skin class. So I'll say public class MyToggleSwitchSkin extends ToggleSwitchSkin, just like so, define my class there, and then of course, I'll define my constructor.
So I'll say public function MyToggleSwitchSkin, like so, and then I'll call the constructor of the toggle switch skin class, so I'll just say super to call that. And then I'll say selectedLabel, and I'll specify is equal to Open. Again, that's all defined in the original class. And then I'll just say unselectedLabel is equal to Closed. Then I'll put in that string literal there, Don't forget your semicolons. And just an example of how to do a very simple skin.
So go ahead and save that, go back to your ToggleSwitch.mxml, and now go to your second toggle switch and add a property here called skins class. So I'm going to say skinClass is equal to, and I should be able to just say skins, and you'll see the code introspection automatically happens. So I'm going to say MyToggleSwitchSkin, like so, and then I'll say the slide duration.
I'll put all of that in, and then I'll go ahead and hit Save. And now let's again run this on our simulated iPad, and then I'll click Run. And notice you should now see that I've modified that skin, instead of saying on or off, I'm saying Open and Closed by creating that custom ActionScript skin. So, very, very cool, and again, yet another component that really allows us to build great tablet-based applications.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99411 Viewers
56 Video lessons · 112649 Viewers
71 Video lessons · 81479 Viewers
131 Video lessons · 39114 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.