在单选按钮上使用组
ToggleGroup 用于管理 RadioButtons,以便每次只能选择每组中的一个。
创建一个简单的 ToggleGroup 如下:
ToggleGroup group = new ToggleGroup();
创建 Togglegroup 后,可以使用 setToggleGroup(ToggleGroup) 将其分配给 RadioButtons。使用 setSelected(Boolean) 预先选择其中一个 RadioButtons。
RadioButton radioButton1 = new RadioButton("stackoverlow is awesome! :)");
radioButton1.setToggleGroup(group);
radioButton1.setSelected(true);
RadioButton radioButton2 = new RadioButton("stackoverflow is ok :|");
radioButton2.setToggleGroup(group);
RadioButton radioButton3 = new RadioButton("stackoverflow is useless :(");
radioButton3.setToggleGroup(group);