Radio Buttons

Overview

Radio buttons let a person pick exactly one choice from a short list, like a pizza size or a game's difficulty. Picking one choice clears the one that was picked before. The name comes from old car radios, where pushing in one station button popped the others out. In this tutorial, you will put radio buttons in a grouping on a page, read which one is picked, and learn how people move through a group with the arrow keys.

Goals

You have the following goals for this tutorial:

By the end of this lesson, you can

  • Put radio buttons in a grouping so that only one can be picked at a time
  • Find out which radio button is picked with GetSelectedButton
  • Explain how the keyboard works in a group: Tab to reach it, arrow keys to change the choice
  • Decide when to use radio buttons and when to use checkboxes

Warm up

Think of a form you have filled out, on paper or online. Find a question where you could pick only one answer and a question where you could pick several. How did the form show you the difference? What would go wrong if the two kinds were mixed up?

Vocabulary

You will be learning about the following vocabulary words:

Vocabulary
TermDefinition
Radio buttonA toggle button that is one choice in a group. Only one radio button in a group can be picked at a time.
GroupingA control on a page that holds other controls. When it holds radio buttons, it makes sure only one of them is picked, and it handles the arrow keys for them.
ButtonGroupThe class a Grouping comes from. It is the part that keeps only one button picked.
ToggleButtonThe Quorum class that every on/off button comes from. RadioButton and Checkbox are both kinds of ToggleButton.
Arrow key navigationMoving between the choices in a group with the arrow keys instead of the Tab key. The group gets one Tab stop, and the arrows change the choice.

New Code to Learn

You will be using the following new pieces of code:

New Code to Learn
Quorum CodeCode ExampleExplanation
AddGrouping(text name)Grouping sizes = page:AddGrouping("Pizza size")Adds a grouping to a page. The name is what a screen reader says when the group gets the focus.
AddRadioButton(text name)sizes:AddRadioButton("Small")Makes a radio button, puts it in the grouping and shows it.
GetSelectedButton()ToggleButton choice = sizes:GetSelectedButton()Returns the button that is picked, or undefined if none is picked yet.
SetToggleState(boolean state)medium:SetToggleState(true)Picks this radio button from code. The grouping clears the others.
GetRadioButton(text name)RadioButton small = sizes:GetRadioButton("Small")Finds a radio button by its name.

CSTA Standards

This tutorial covers the following CSTA standards:

For teachers · 2026 CSTA K–12 Computer Science Standards

HS-PRO-TR-19

High School · Programming

Evaluate a computing technology's alignment with design specifications and responsible design values, including its correctness, effectiveness, and user experience.

Also in 7 other lessons

MS-PRO-TR-19

Middle School · Programming

Use systematic strategies to test, refine, and document changes to a computing technology to meet the intended purpose.

Also in 6 other lessons

S1-SWD-UX-04

Specialty I · Software Development

Refine a user interface design using accessibility and responsive design tools.

Also in 13 other lessons

MS-ALG-PS-03

Middle School · Algorithms & Design

Verify the accuracy of an algorithm for given inputs.

Also in 2 other lessons

HS-ALG-PS-03

High School · Algorithms & Design

Evaluate algorithms for efficiency, correctness, and clarity, using metrics or test cases.

Also in 4 other lessons

Every standard, and the lessons that cover it

Explore

What are radio buttons?

In the Checkboxes tutorial, you met the toggle button: a button that remembers whether it is on or off. In Quorum, these all come from the ToggleButton class. Checkbox is one kind of toggle button. RadioButton is another.

The difference is in how they turn off. A checkbox turns off when you activate it again. A radio button does not. Once it is picked, it stays picked until a different radio button in the same group is picked. This makes radio buttons right for questions with exactly one answer.

That only works if the radio buttons know about each other. On a page, they do this through a grouping, which you met in the Groupings tutorial. A grouping that holds radio buttons turns all the others off when one is turned on.

Making radio buttons on a page

A page's AddGrouping action makes a grouping. Then the grouping's AddRadioButton action makes a radio button, puts it in the group and shows it, all in one step. The page puts the radio buttons side by side, in one row, each with an equal share of the width.

The grouping's name, "Pizza size", is what a screen reader says when the group gets the focus. A person who can see the screen needs the question too, so the program also adds a label above the grouping.

use Libraries.Interface.Forms.Form
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.Grouping
use Libraries.Interface.Controls.RadioButton
use Libraries.Interface.Controls.Button

Form form
Page page = form:GetMainPage()
page:SetTitle("Order a Pizza")

page:AddLabel("Pick a size:")
Grouping sizes = page:AddGrouping("Pizza size")
sizes:SetDescription("Pick one size. Use the arrow keys to change it.")
sizes:AddRadioButton("Small")
RadioButton medium = sizes:AddRadioButton("Medium")
sizes:AddRadioButton("Large")
medium:SetToggleState(true)

Button order = page:AddButton("Choose size")
order:SetDescription("Shows the size you picked.")
page:AddLabel("Result", "You haven't chosen yet.")

SizeBehavior behavior
order:SetBehavior(behavior)

form:Display()

The line medium:SetToggleState(true) picks "Medium" before the form appears. It's good practice to start with a sensible choice already picked, so that the question always has an answer.

Finding out which one is picked

A grouping's GetSelectedButton action returns the toggle button that is picked. If nothing is picked yet, it returns undefined, so check for that. Make a class in a file named SizeBehavior.quorum. It is a FormBehavior, so it can find the grouping and the label on the page by their names:

use Libraries.Interface.Forms.FormBehavior
use Libraries.Interface.Forms.Page
use Libraries.Interface.Forms.Grouping
use Libraries.Interface.Events.BehaviorEvent
use Libraries.Interface.Controls.ToggleButton
use Libraries.Game.Graphics.Label

class SizeBehavior is FormBehavior
    action Run(BehaviorEvent event)
        Page page = GetPage()
        Grouping sizes = page:GetGrouping("Pizza size")
        Label result = page:GetLabel("Result")
        if sizes = undefined or result = undefined
            return now
        end

        ToggleButton choice = sizes:GetSelectedButton()
        if choice = undefined
            result:SetText("Please pick a size first.")
        else
            result:SetText("You picked " + choice:GetName() + ".")
        end
    end
end

GetSelectedButton returns a ToggleButton, the parent class of RadioButton, which is why the behavior uses ToggleButton and not RadioButton.

Blocks

Using the keyboard: one Tab stop, then arrow keys

A group of radio buttons is one stop in the Tab order, not one stop per button. The group gets the focus, not the radio buttons inside it:

  • Press Tab or Shift+Tab to move the focus to the group. If nothing was picked yet, the first radio button is picked for you.
  • Press Down Arrow or Right Arrow to pick the next choice. Press Up Arrow or Left Arrow to pick the previous one. At the end of the list, the arrows wrap around to the other end.
  • Press Tab again to leave the group.

You don't write any code for this. The grouping does it. This is the same way radio buttons work in web pages and desktop programs, so people who use a keyboard or a screen reader already know it. It also keeps the Tab order short: a group with ten choices is still one Tab stop.

A screen reader says the group's name when the group gets the focus, and the radio button that is picked. As the person presses the arrow keys, it says each new choice. The exact words depend on the screen reader.

On a phone or tablet

  • With touch: tap a choice to pick it. The choice that was picked before is unpicked for you.
  • With VoiceOver (iPhone and iPad) or TalkBack (Android) on: swipe right to move to the next choice and left to move to the previous one. Each radio button is its own stop, so the screen reader reads each choice and whether it is picked. Double-tap anywhere on the screen to pick the choice it is on.

Reacting when the choice changes

Like a checkbox, a radio button can have a behavior. Its Run action is called each time that radio button is picked, whether by a click, an arrow key or code. You can give the same behavior to every radio button in a group and use the event to find out which one was picked:

use Libraries.Interface.Behaviors.Behavior
use Libraries.Interface.Events.BehaviorEvent

class PickedBehavior is Behavior
    action Run(BehaviorEvent event)
        output event:GetItem():GetName() + " is now picked."
    end
end

To use it, add these lines to Main.quorum after the radio buttons are made, and before medium:SetToggleState(true):

PickedBehavior picked
RadioButton small = sizes:GetRadioButton("Small")
small:SetBehavior(picked)
medium:SetBehavior(picked)
RadioButton large = sizes:GetRadioButton("Large")
large:SetBehavior(picked)

When a different radio button is picked, the old one is turned off, and the grouping calls the old one's behavior's Dispose action. You can use Dispose to undo whatever Run did.

Radio buttons or checkboxes?

Use this rule:

  • Exactly one answer? Use radio buttons. Example: pizza size.
  • Any number of answers, including none? Use checkboxes. Example: pizza toppings.
  • A single yes-or-no? Use one checkbox, not two radio buttons labeled "Yes" and "No."

Engage

In this activity, you write a short quiz with three multiple-choice questions. The goal is to use radio buttons for choices where only one answer can be picked, and to think about how a quiz should start. Each question is a label and a grouping of radio buttons, and a "Check answers" button shows in a label how many answers are right. Give each grouping a name that is the question, such as "What is 6 times 7?", and give each radio button a clear name. Then decide whether each question should start with a choice already picked. For a quiz, is that fair? Explain your decision in a comment in your code.

Before you test, write a short test plan: for each question, which choices you will pick and what the label should say. Include all right, all wrong, and a mix. Run your plan using only the keyboard: Tab to each question and the arrow keys to choose an answer. Then check your quiz against this list:

  • Does Tab stop once on each question, and do the arrow keys change the answer?
  • Does every row of your test plan give the label you wrote down?
  • Would each question's name make sense to someone who had never seen the quiz?

Fix anything that doesn't match, and write down each change you made and why, in a comment at the top of your code.

Wrap up

Radio buttons are toggle buttons that live in a group. The group makes sure only one is picked, takes the focus for all of them, and lets people change the choice with the arrow keys. Think about the warm up again: why do you think the keyboard works differently for a group of radio buttons than for a list of checkboxes?