Computer Science
Grade 9
20 min
5. Handling User Input: Text Fields, Buttons, and Touch Events
Learn how to handle user input in mobile apps, including text fields, buttons, and touch events.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Identify the purpose of common user interface (UI) elements like text fields and buttons.
Define the concepts of an 'event', 'event listener', and 'event handler' in the context of user interaction.
Implement a button that triggers a specific function when tapped.
Retrieve and use text input from a text field in their application.
Write a simple event handler to respond to a basic touch event, like a screen tap.
Explain the flow of data from user action (input) to application response (output).
Ever wonder how your favorite game knows exactly where you tapped, or how Instagram knows what you typed in a comment? 🤔 Let's pull back the curtain!
This lesson explores how mobile apps listen and react to you. We'll learn...
2
Key Concepts & Vocabulary
TermDefinitionExample
User Interface (UI) ElementA visual component on the screen that a user can see and interact with. These are the building blocks of an app's screen.A 'Submit' button, a search bar (text field), a slider, or an image.
EventAn action that occurs in the app, often initiated by the user. The app can be programmed to respond to these actions.A user tapping a button, typing a character in a text field, or swiping their finger across the screen.
Event ListenerA piece of code that 'waits' for a specific event to happen on a specific UI element.Code that is specifically 'listening' for a 'tap' event on the 'loginButton'.
Event HandlerA function or block of code that is executed when the event listener detects the event it...
3
Core Syntax & Patterns
The Event Handling Pattern
1. Identify the UI Element. 2. Attach an Event Listener to it. 3. Define an Event Handler function to run.
This is the fundamental pattern for making apps interactive. You must connect an action (the event) on a screen object (the UI element) to a piece of code (the handler) that performs a task.
Retrieving Text Field Value
variable myText = myTextField.text
To use what the user typed, you must access the 'text' property of the text field UI element. This is typically done inside an event handler, like after a 'Submit' button is pressed.
Button Action Syntax
myButton.addEventListener('tap', functionToRun)
This syntax connects a UI element (myButton) to a function (functionToRun) that will execute when a specifi...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You want to build a simple calculator that adds two numbers. The UI has `numberField1`, `numberField2`, an `addButton`, and a `resultLabel`. Which of the following describes the most accurate and complete logic for the `addButton`'s event handler?
A.Get the text from `numberField1`, convert it to a number, and display it in `resultLabel`.
B.Get the text from both fields, add them together as strings (e.g., "5" + "3" becomes "53"), and display the result.
C.Get the text from `numberField1` and `numberField2`, convert both to numbers, add the numbers arithmetically, and update `resultLabel` with the sum.
D.When the button is tapped, check if `numberField1` has text. If it does, add 1 to it.
Challenging
Imagine you modify the 'Tap Counter' app. You add a button called `resetButton`. The goal is for the main screen tap to increment the counter, but tapping `resetButton` should set the counter back to 0. How does this new feature affect the overall event handling structure?
A.You must delete the original screen tap listener and replace it with one on the button.
B.You need one event handler function that can magically tell whether the screen or the button was tapped.
C.You must check inside the screen tap handler if the `resetButton` was also tapped at the same time.
D.You need to add a second, separate event listener on the `resetButton` with its own unique event handler function.
Challenging
A user complains that an app's 'Send' button feels unresponsive. They say they often have to tap it multiple times for it to work, but if they tap and hold for a second, it works every time. Based on the tutorial's concepts, which is the most likely programming error?
A.The event handler is reading text from the wrong text field.
B.The event listener was mistakenly set up to listen for a 'longPress' event instead of a 'tap' event.
C.The button's text color is the same as its background color, making it seem invisible.
D.The event handler function is empty and contains no code.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free