Search…
Radio

Overview

A Radio button is a button that can be either checked or unchecked. A user can tap the button to check or uncheck it. It can also be checked from the template using the checked property Use an element with a RadioGroupController attribute to group a set of radio button. When radio buttons are inside a RadioGroupController, just one radio in the group can be checked at any time. If a radio button is not placed in a group, they will all have the ability to be checked at the same time
To see the complete sample, go to github

Source Code

RadioSample.java
1
import totalcross.sys.Settings;
2
import totalcross.ui.Bar;
3
import totalcross.ui.Check;
4
import totalcross.ui.Label;
5
import totalcross.ui.MainWindow;
6
import totalcross.ui.Radio;
7
import totalcross.ui.RadioGroupController;
8
import totalcross.ui.ScrollContainer;
9
import totalcross.ui.event.ControlEvent;
10
import totalcross.ui.event.PressListener;
11
import totalcross.ui.font.Font;
12
import totalcross.ui.gfx.Color;
13
14
public class RadioButton extends MainWindow{
15
16
public RadioButton(){
17
super("", BORDER_NONE);
18
setUIStyle(Settings.Material);
19
Settings.uiAdjustmentsBasedOnFontHeight = true;
20
setBackForeColors(Color.WHITE, Color.BLACK);
21
22
Bar h1 = new Bar(" RADIO");
23
h1.canSelectTitle = false;
24
h1.setFont(Font.getFont("Lato Bold", false, h1.getFont().size+3));
25
h1.setBackForeColors(0XF8F8F8,Color.BLACK);
26
add(h1, LEFT,TOP,FILL,PREFERRED-50);
27
28
}
29
30
public void initUI(){
31
ScrollContainer sc = new ScrollContainer(false, true);
32
add(sc,LEFT,AFTER,FILL,FILL);
33
34
RadioGroupController radioGroup = new RadioGroupController();
35
36
Radio pyton = new Radio("Pyton", radioGroup);
37
pyton.setChecked(true);
38
sc.add(pyton, LEFT+100, AFTER+100 ,PREFERRED+100,PREFERRED+25);
39
40
Radio java = new Radio("Java", radioGroup);
41
sc.add(java, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
42
43
Radio ruby = new Radio("Ruby", radioGroup);
44
sc.add(ruby, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
45
46
Radio php = new Radio("PHP", radioGroup);
47
sc.add(php, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
48
49
Label autolock = new Label("Auto-Lock");
50
autolock.setFont(Font.getFont("Lato Regular", false, autolock.getFont().size+3));
51
sc.add(autolock, LEFT+100, AFTER+100, FILL, PREFERRED+25);
52
53
final Radio enable = new Radio("Enable: ''Never'' ");
54
enable.setChecked(true);
55
enable.setFont(Font.getFont("Lato Regular", false, Font.NORMAL_SIZE-2));
56
sc.add(enable, LEFT+123, AFTER+100 ,PREFERRED+100,PREFERRED+25);
57
58
RadioGroupController radioGroup2 = new RadioGroupController();
59
60
Radio rd1 = new Radio("1 Hour", radioGroup2);
61
rd1.setChecked(true);
62
sc.add(rd1, LEFT+100, AFTER+100 ,PREFERRED+100,PREFERRED+25);
63
64
Radio rd2 = new Radio("2 Hours", radioGroup2);
65
sc.add(rd2, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
66
67
Radio rd3 = new Radio("3 Hours", radioGroup2);
68
sc.add(rd3, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
69
70
final Radio never = new Radio("Never", radioGroup2);
71
sc.add(never, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
72
73
enable.addPressListener(new PressListener(){
74
public void controlPressed(ControlEvent e){
75
boolean b = enable.isChecked();
76
never.setEnabled(b);
77
}
78
});
79
80
Label properties = new Label("Properties");
81
properties.setFont(Font.getFont("Lato Regular", false, properties.getFont().size+3));
82
sc.add(properties, LEFT+100, AFTER+100, FILL, PREFERRED+25);
83
84
Radio simple = new Radio("Simple Radio");
85
simple.setChecked(true);
86
sc.add(simple, LEFT+100, AFTER+100 ,PREFERRED+100,PREFERRED+25);
87
88
Radio bcolor = new Radio("Background Color");
89
bcolor.setBackColor(Color.YELLOW);
90
bcolor.textColor = Color.BLUE;
91
bcolor.checkColor = uiMaterial ? Color.BLACK : Color.YELLOW;
92
sc.add(bcolor, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
93
94
Radio fcolor = new Radio("Foreground Color");
95
fcolor.setForeColor(Color.darker(Color.GREEN));
96
fcolor.checkColor = Color.GREEN;
97
sc.add(fcolor, LEFT+100, AFTER+50 ,PREFERRED+100,PREFERRED+25);
98
}
99
}
Copied!

Attributes

Type
Name
Description
int
checkColor
Set to the color of the check, if you want to make it different of the foreground color
boolean
leftJustify
Set to true to left justify this control if the width is above the preferred one
int
checkColor
Sets the text color of the check

Métodos

Tipo
Nome
Descrição
Constructor
Radio(String Text)
Creates a radio control displaying the given text
Constructor
Radio(String text, RadioGroupController radioGroup)
Creates a radio control with the given text attached to the given RadioGroupController
RadioGroupController
getRadioGroup( )
Returns the RadioGroupController that this radio belongs to, or null if none
String
getText( )
Gets the text displayed in the radio
boolean
isChecked( )
Returns the checked state of the control
void
setChecked(boolean checked)
Sets the checked state of the control
void
setChecked(boolean checked, boolean sendPress)
Sets the checked state of the control, and send the press event if desired
void
setText( )
Sets the text

References

Last modified 2yr ago