Search…
Edit
An Edit is a field used to show and alter texts. Also known as UITextField on Swift and input on HTML. There also is some variations, like Outlined Edit, Calculator Edit, Password Edit, etc.

Examples

In most applications is necessary to remove the background from the object and change the color of the element so that it is better viewed.
1
package com.totalcross;
2
import totalcross.sys.Settings;
3
import totalcross.ui.Edit;
4
import totalcross.ui.MainWindow;
5
import totalcross.ui.dialog.MessageBox;
6
import totalcross.ui.gfx.Color;
7
import totalcross.util.UnitsConverter;
8
9
public class HelloWorld extends MainWindow {
10
private Edit simpleEdit;
11
private int GAP = UnitsConverter.toPixels(DP + 15);
12
13
public HelloWorld(){
14
setUIStyle(Settings.MATERIAL_UI);
15
}
16
17
@Override
18
public void initUI(){
19
try{
20
simpleEdit = new Edit();
21
simpleEdit.caption = "Simple Edit";
22
simpleEdit.transparentBackground = true;
23
simpleEdit.captionColor = Color.RED;
24
simpleEdit.setForeColor(Color.RED);
25
26
add(simpleEdit, LEFT + GAP, AFTER + GAP);
27
} catch (Exception e) {
28
MessageBox.showException(e, true);
29
}
30
}
31
}
Copied!

Numeric edit

In some situations, entry should be limited to numbers.
1
package com.totalcross;
2
import totalcross.sys.Settings;
3
import totalcross.ui.Edit;
4
import totalcross.ui.MainWindow;
5
import totalcross.ui.dialog.MessageBox;
6
import totalcross.util.UnitsConverter;
7
8
public class HelloWorld extends MainWindow {
9
10
private Edit numericEdit;
11
private Edit calculatorEdit;
12
private int GAP = UnitsConverter.toPixels(DP + 15);
13
14
public HelloWorld(){
15
setUIStyle(Settings.MATERIAL_UI);
16
}
17
@Override
18
public void initUI(){
19
try{
20
numericEdit = new Edit();
21
numericEdit.caption = "NumericBox Edit";
22
numericEdit.setMode(Edit.CURRENCY);
23
numericEdit.setKeyboard(Edit.KBD_NUMERIC);
24
add(numericEdit, LEFT + GAP, AFTER + GAP);
25
26
calculatorEdit = new Edit();
27
calculatorEdit.caption = "Calculator Edit";
28
calculatorEdit.setMode(Edit.CURRENCY, true);
29
add(calculatorEdit, LEFT + GAP, AFTER + GAP);
30
} catch (Exception ee) {
31
MessageBox.showException(ee, true);
32
}
33
}
34
}
Copied!

Password Edit

If the entry is a password, it should not be possible to see it.
1
package com.totalcross;
2
import totalcross.sys.Settings;
3
import totalcross.ui.Edit;
4
import totalcross.ui.MainWindow;
5
import totalcross.ui.dialog.MessageBox;
6
import totalcross.util.UnitsConverter;
7
8
public class HelloWorld extends MainWindow {
9
private Edit passwordShowEdit;
10
private Edit passwordHidenEdit;
11
private int GAP = UnitsConverter.toPixels(DP + 15);
12
13
public HelloWorld(){
14
setUIStyle(Settings.MATERIAL_UI);
15
}
16
@Override
17
public void initUI(){
18
try{
19
passwordShowEdit = new Edit();
20
passwordShowEdit.caption = "Password Edit (last character is shown)";
21
passwordShowEdit.setMode(Edit.PASSWORD);
22
add(passwordShowEdit, LEFT + GAP, AFTER + GAP);
23
24
passwordHidenEdit = new Edit();
25
passwordHidenEdit.caption = "Password Edit (all characters are hidden)";
26
passwordHidenEdit.setMode(Edit.PASSWORD_ALL);
27
add(passwordHidenEdit, LEFT + GAP, AFTER + GAP);
28
}catch (Exception ee) {
29
MessageBox.showException(ee, true);
30
}
31
}
32
}
Copied!

Edit shapes

To receive different input formats, which are not predefined in TotalCross.
1
package com.totalcross;
2
import totalcross.sys.Settings;
3
import totalcross.ui.Edit;
4
import totalcross.ui.MainWindow;
5
import totalcross.ui.dialog.MessageBox;
6
import totalcross.util.UnitsConverter;
7
8
public class HelloWorld extends MainWindow {
9
private Edit calendarEdit;
10
private Edit timerEdit;
11
private Edit maskedEdit;
12
private int GAP = UnitsConverter.toPixels(DP + 15);
13
14
public HelloWorld(){
15
setUIStyle(Settings.MATERIAL_UI);
16
}
17
18
@Override
19
public void initUI(){
20
try{
21
calendarEdit = new Edit("99/99/99");
22
calendarEdit.caption = "Calendar Edit";
23
calendarEdit.setMode(Edit.DATE, true);
24
add(calendarEdit, LEFT + GAP, AFTER + GAP);
25
26
timerEdit = new Edit("99" + Settings.timeSeparator + "99" + Settings.timeSeparator + "99");
27
timerEdit.caption = "TimeBox Edit (24-hour format)";
28
timerEdit.setValidChars("0123456789AMP");
29
timerEdit.setMode(Edit.NORMAL, true);
30
timerEdit.setKeyboard(Edit.KBD_TIME);
31
add(timerEdit, LEFT + GAP, AFTER + GAP);
32
33
maskedEdit = new Edit("999.999.999-99");
34
maskedEdit.caption = "Masked Edit (999.999.999-99)";
35
maskedEdit.setMode(Edit.NORMAL, true);
36
maskedEdit.setValidChars(Edit.numbersSet);
37
add(maskedEdit, LEFT + GAP, AFTER + GAP);
38
}catch (Exception ee){
39
MessageBox.showException(ee, true);
40
}
41
}
42
}
Copied!

Behind the Class

Attributes

Type
Name
Description
String
clearValueStr
stores the value that will be used by the clear() method to switch texts
Byte
captalise
used to modify the the letter case from the text within the Edit:
  • ALL_NORMAL: normal case
  • ALL_LOWER: all lowercase
  • ALL_UPPER: all uppercase
String
caption
The Edit's placeholder text
boolean
forceBackgroundColor
forces the edit tu use the background color on it, must be used with UIColors.sameColors setted to true

Methods

Type
Name
Description
Constructor
Edit( )
Creates a box for user input
Constructor
Edit(String mask)
Creates a box for user input, but with a mask
void
setMode(byte mode)
Used to change the mode value to one of the accepted ones by the mode constants, without masking. To enable masking, you should use the setMode(byte mode, boolean maskedEdit) method, and pass the mode you wish and true on the second parameter
void
setEditable(boolean on)
will enable or disable the Edit. Can be used as a way to make sure the user don't modify something that was already saved on server and can't be modified without proper authorization
String
getText( )
Returns the text within the Edit, without care for the mask. It's important to note that in the case where there is mask, the characters from the mask will come with the user input, to get only the user input you should be using the getTextWithoutMask() method to recover the text
void
setMaxLength()
Is used to limit the characters number the user can digit on the text field
void
setPrediction(boolean prediction)
Is used to set if there should or not exist prediction on this Edit, it is, however set to true by default.

References