(MAD) Understanding Events and Event Handling Lesson
Understanding Events and Event Handling
Apps are what we call event-driven applications. They don't perform a set of instructions in a predetermined order. They perform actions based on different events such as clicking a button, dragging your finger, or touching down on the screen. When you add a component to the screen in design view, the component will appear in the blocks editor so it can be programmed. The components are used to trigger an event. Once an event takes place, the app reacts by calling an event handler method that contains the instructions or the actions to do something in response to the event.
Your job as a programmer is to specify how the app should respond to events by creating event handler methods.
Event Handlers Methods
Event handlers are functions that are triggered by different events. In app eventer, event handler blocks are yellowish blocks that read, "when do". Each event handler block is like its own main program that contains the code that causes the action to occur. The following blocks are some of the events you'll code:
Probably the most common event is a button click event: what should the app do when the user clicks a button.
Button1.Click is an event that can happen to the app. The gold when Button1.Click block is called an event-handler. The blocks inside it are the response to the event. set means to change something, in this case, change the button's BackgroundColor to red. The blocks, as a whole, specify that when the user clicks the button it should turn red.
A button click is an example of a user-initiated event. Not all actions are triggered by the user. These events trigger an action but are not initiated by the user.
- Timer events, sort of like an alarm clock going off
- Sensor events, such as when GPS coordinates are beamed to the device
- Phone events, including incoming texts.
- Animation events, such as two objects colliding
- Web events, such as some requested data arriving from the web
- App (Screen) launch events
Naming Components
The recommended standard is to use a capitalized descriptive name, followed by the capitalized component name - with no spaces between words. Examples include: "ScoreLabel", "ScoreTextBox:, "TitleLabel" or "InstructionslLabel". (AppInventor doesn't allow spaces so if you space in between the words, App Inventor will automatically insert an underscore and name your label "Score_Label".) If you prefer, you can also purposely enter an underscore in a name: TitleName_Label. This will differentiate between a "Name_Label" and a "Name_TextBox" component and will make programming the components easier to identify.
[CC BY 4.0] UNLESS OTHERWISE NOTED | IMAGES: LICENSED AND USED ACCORDING TO TERMS OF SUBSCRIPTION