# AS calculator

Source: Internet
Author: User

This example introduces the application of function definition in the program and the code optimization process.

Ideas:

1. Use AS to create four text boxes. The first is used to enter numbers, the second is used to input operators, the third is used to enter numbers, and the last is used to output results.

2. Set a button (count_btn). Press the operator followed by the operator given in the second text box to calculate the content of the first and third texts and output the content in the last text box.

3. The first text box is named "in1"

The second text box is "sign"

The third text box is "in2"

The fourth text box is "out"

Executable procedure 1:

Var t_f: TextFormat = new TextFormat ();

T_f.size = 20;

// Set the font in text format to 20

// ========================== Set the first text box ======================= =

_ Root. createTextField ("in1", 1, 25, 50,150, 25 );

// Create a text box. Note that the Y coordinate of the text box is incremented.
In1.setNewTextFormat (t_f );

// Assign the set text format to the text box

In1.type =
"Input ";
// Set the text box type to input

In1.background = true;
// The text box has a background color.

In1.backgroundColor =
0 xffffff;
// The background color is white.

In1.border = true;
// The text box has a border

In1.borderColor = 0x0;
// The border color is black.

In1.maxChars =
7;
// The maximum number of characters in the text box is 7.

In1.restrict =
"0-9 ";
// The character set allowed in the text box is 0 to 9

// ================ Set the second text box (basic Same as above) ======================

_ Root. createTextField ("sign", 2, 25, 80,150, 25 );
Sign. setNewTextFormat (t_f );

Sign. type = "input ";

Sign. background = true;

Sign. backgroundColor = 0 xffffff;

Sign. border = true;

Sign. borderColor = 0x0;

Sign. maxChars = 1;

Sign. restrict =
"+ */\\-";
// ================ Set the third text box (basic Same as above) ====================

_ Root. createTextField ("in2", 3, 25,110,150, 25 );
In2.setNewTextFormat (t_f );

In2.type = "input ";

In2.background = true;

In2.backgroundColor = 0 xffffff;

In2.border = true;

In2.borderColor = 0x0;

In2.restrict = "0-9 ";

In2.maxChars =
7;
// ====================== Set the fourth output text box ======================

_ Root. createTextField ("out", 4, 25,165,150, 25 );
Out. setNewTextFormat (t_f );

Out. type = "dynamic ";

Out. background = true;

Out. backgroundColor = 0 xffffff;

Out. border = true;

Out. borderColor =
0x0;
// ============================ Calculate after the button is pressed

Count_btn.onRelease = function (){

If (sign. text = "+ "){

Out. text =
Int (in1.text) + int (in2.text );

}

If (sign. text = "-"){

Out. text =
Int (in1.text)-int (in2.text );

}

If (sign. text = "*"){

Out. text =
Int (in1.text) * int (in2.text );

}

If (sign. text = "/"){

Out. text =
Int (in1.text)/int (in2.text );

}

};

In this program, we can see that the setting of the text box attribute is repeated multiple times. To streamline the code, we should consider using functions to solve the code reuse problem. Please refer to the second program

Executable procedure 2:

Var t_f: TextFormat = new TextFormat ();

T_f.size = 20;
Function F (txt ){

Txt. setNewTextFormat (t_f );

Txt. background = true;

Txt. backgroundColor = 0 xffffff;

Txt. border = true;

Txt. borderColor = 0x0;

Txt. type = "input ";

If (txt = sign ){

Txt. maxChars = 1;

Txt. restrict = "+ */\\-";

}

If (txt = out ){

Txt. type = "dynamic ";

}

If (txt = in1 | txt = in2 ){

Txt. maxChars = 7;

Txt. restrict = "0-9 ";

}

}
_ Root. createTextField ("in1", 1, 25, 50,150,
25 );

F (in1 );

_ Root. createTextField ("sign", 2, 25, 80,150, 25 );

F (sign );

_ Root. Createtext field ("in2", 3, 25,110,150,
25 );
F (in2 );
_ Root. createTextField ("out ",
4, 25,165,150, 25 );

F (out );

Count_btn.onRelease = function (){

If (sign. text = "+ "){

Out. text =
Int (in1.text) + int (in2.text );

}

If (sign. text = "-"){

Out. text =
Int (in1.text)-int (in2.text );

}

If (sign. text = "*"){

Out. text =
Int (in1.text) * int (in2.text );

}

If (sign. text = "/"){

Out. text =
Int (in1.text)/int (in2.text );

}

};
// In this program, we call a function to set Attributes. The places with the same attributes are written at the beginning of the function. if statements are used to determine different places before assigning

Finally, we will optimize the code: Use the with statement, set attributes in batches, and change the if in the button to switch to make the structure clearer and more readable:

Executable Program 3:

Var t_f: TextFormat = new TextFormat ();

T_f.size = 20;
Function F (txt ){

With
(Txt ){

SetNewTextFormat (t_f );

Background = true;

BackgroundColor =
0 xffffff;

Border = true;

BorderColor = 0x0;

}

If (txt = sign)
{
With
(Txt)
{
Type
= "Input ";

MaxChars =
1;

Restrict =
"+ */\\-";

}
}

If (txt = out)
{
Txt. type
= "Dynamic ";
}

If (txt = in1 | txt = in2 ){

With
(Txt ){

Type =
"Input ";

MaxChars =
7;

Restrict =
"0-9 ";

}

}

}

_ Root. createTextField ("in1", 1, 25, 50,150, 25 );

F (in1 );

_ Root. createTextField ("sign", 2, 25, 80,150, 25 );

F (sign );

_ Root. createTextField ("in2", 3, 25,110,150, 25 );

F (in2 );

_ Root. createTextField ("out", 4, 25,165,150, 25 );

F (out );

Count_btn.onRelease = function ()
{
Switch (sign. text)
{

Case "+ ":

Out. text =
Int (in1.text) + int (in2.text );

Break;

Case "-":

Out. text =
Int (in1.text)-int (in2.text );

Break;

Case "*":

Out. text =
Int (in1.text) * int (in2.text );

Break;

Case "/":

Out. text =
Int (in1.text)/int (in2.text );

Break;

}

};
// Now, the code is optimized from traditional to simplified. I hope you can understand it in future practices.

FLASH charging 1: TextField
Common attributes and event processing functions
Common attributes:

1. createTextField ("instance name", depth, x coordinate, y coordinate, width,
Height)

Creates a text box.

Example: createTextField ("txt", 1, 10, 20,100,100 );

2. autoSize: controls automatic size adjustment and alignment of text fields.

The acceptable values of autoSize are "none" (default), "left", "right", and
"Center ". When you set the autoSize attribute, true is a synonym for "left", false
Is a synonym for "none.

Note: When txt. autoSize = true, it indicates that the size is automatically matched.

At this time, createTextField ("txt", 1, 10, 20, 0, 0) width and height can be thought of as 0

3. background: specifies whether the text field is filled with background information;

BackgroundColor: specifies the background color of a text field.

Note: These two statements must be used in combination:

Example: txt. background = true;

Txt. backgroundColor = 0xff0000;

4. border: Specifies whether a text field has a border.

BorderColor: the border color of the text field.

Note: These two statements must be used in combination:

Example: txt. border = true;

Txt. borderColor = 0x0;

5. selectable: a Boolean value indicating whether the text field is optional.

For example, txt. selectable = false indicates that the txt text field is optional.

6. textColor: indicates the text color in the text field.

Example: txt. textColor = 0xFF0000;

7. type: specifies the text field type.

There are two types of values:

"Dynamic": specifies the dynamic text fields that cannot be edited by users;

"Input": specifies the input text field.

For example, txt. type = "dynamic" indicates that the txt type is dynamic text;

Txt. type = "input" indicates that the txt type is input text.

8. wordWrap: a Boolean value that indicates whether text fields wrap automatically.

For example, txt. wordWrap = true indicates automatic line feed.

9. multiline: indicates whether a text field is a multiline text field.

For example, txt. multiline = true indicates that the txt text box contains multiple rows.

10. hscroll: Indicates the current horizontal scroll position.

Note: When txt is a single line without line breaks

Write in the button: txt. hscroll + = 5.

Scroll: specifies the current vertical scroll position (relative to hscroll ).

Note: When the txt file contains multiple lines and contains line breaks

Write in the button: txt. scroll + = 5.

11. maxChars: indicates the maximum number of characters that a text field can contain.

For example, txt. maxChars = 7 indicates that txt can contain up to 7 characters.

12. password: specifies whether the text field is a password text field.

For example, txt. password = true indicates that fields in txt appear as passwords.

13. restrict: restrict character sets that can be entered into text fields.

For example:

<1> only uppercase characters, spaces, and numbers can be entered in text fields:

Txt. restrict = "A-Z 0-9 ";
<2> all characters except lowercase letters:

Txt. restrict = "^ a-z ";
<3> You can use a backslash to enter ^ or-
. The recognized backslash sequence is \-, \ ^, or
\\. The backslash must be a literal character in the string. Therefore
Two backslashes must be used for. The following code only contains-and ^:

Txt. restrict = "\-\ ^ ";
<4> it can be used anywhere in the string.
^ To switch between the included and excluded characters. Contains uppercase letters Q
Other uppercase letters:

Txt. restrict = "A-Z ^ Q ";

Common event processing functions:

1. TextField. onChanged = function (){}

Call a function when the content of a text field is changed.

For example, the text content is output as long as the segments in the text box change.

_ Root. createTextField ("txt", 99,10, 10,300, 20 );

Txt. border = true;

Txt. type = "input ";

Txt. onChanged = function (){

Trace (txt. text );

};

2. TextField. onKillFocus = function ()
{}

Call the function when the text field loses the keyboard focus.

For example, when the text box loses focus, the output text content

_ Root. createTextField ("txt", 99,10, 10,300, 20 );

Txt. border = true;

Txt. type = "input ";

Txt. onKillFocus = function (){

Trace (txt. text );

};

3. TextField. onSetFocus = function ()
{}

The function is called when a text field receives the keyboard focus.

For example, when the text box receives the focus, the text content is cleared.

_ Root. createTextField ("txt", 99,10, 10,300, 20 );

Txt. border = true;

Txt. type = "input ";

Txt. onSetFocus = function (){

Txt. text = "";

};

Important methods:

SetNewTextFormat: set the default new text format for text fields.

For example:

Var t_f: TextFormat = new TextFormat ();

T_f.bold = true;

T_f.font = "Arial ";

This. createTextField ("txt", 1, 40, 10, 70, 20 );

Txt. border = true;

Txt. setNewTextFormat (t_f );

Txt. text = "Hello World! ";

FLASH Charging 2: TextFormat common attributes

1. var t_f: TextFormat = new TextFormat ();

Apply for a TextFormat object and instantiate it.

2. TextFormat. align: specifies the alignment of a paragraph.

"Left" -- left alignment.

"Center" -- center.

"Right" -- right alignment.

"Justify" -- the two ends are aligned.

Example: t_f.align = "center"

3. TextFormat. bold: indicates whether the text is in bold.

Example: t_f.bold = true

4. TextFormat. color: indicates the color of the text.

Example: t_f.color = 0xff0000

5. TextFormat. font: text font name, represented in string format.
Example:
T_f.font = ""

6. TextFormat. italic: indicates whether the text in this text format is italic.

Example: t_f.font = true

7. TextFormat. underline: indicates whether the text in this text format is underlined.

Example: t_f.underline = true

8. TextFormat. leftMargin: left margin of a paragraph, in lbs.

Example: t_f.leftMargin = 5

9. TextFormat. rightMargin: right margin of a paragraph, in lbs.
Example:
T_f.rightMargin = 5

10. TextFormat. size: the pound value of the text in this text format.

Example: t_f.size = 20

11. TextFormat. indent: indicates the indent integer from the left to the first character in the paragraph.

Example: t_f.indent = 5

12. TextFormat. url: indicates
URL.

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.