Android Study Notes 1 --- simple calculator

Source: Internet
Author: User

Lab questions:
Complete a simple computer function in the Android simulator.
Purpose:
Learn how to use common Android controls such as text boxes and buttons.
 
Step 1: Create a project

 


Select the SDK version. For example, select 2.3.3.

 

 
So far, the project has been created.

 
The Android project has created a main. xml interface by default.
Double-click to open the interface. Adjust the screen size of the interface.

 
The default layout of the Android interface is "streaming layout". For those who are familiar with windows development, it may be a bit difficult.
Therefore, first adjust the interface layout to "absolute layout (that is, use x, y to control the location )"

 
Select AbsoluteLayout Layout

Add the following controls on the page.

Set the ID of the button control in turn

 
Button ID, starting from btn0,
0 ----- btn0
1 ----- btn1
2 ----- btn2
.....
9 ----- btn9
0 --- btn10
(+) ---- Btn11
(-) ---- Btn12
(*) ---- Btn13
(/) ---- Btn14
 
The complete layout is as follows:

The appearance of a simple calculator is complete.
Add code now.

The Anroid interface consists of xml files, and the code files supporting the code are placed under src. For example, jsqActivity. java

Open the jsqActivity. java file and we can see the initial code.
Note: The main function cannot be found here.
The startup function of the program starts from Activity.
 
OnCreate () is executed during Activity construction. We can add our button Event code here.
Android events need to be manually mounted, which is much easier than VisualStudi.
The manager has two steps to handle the mounting event:
(1) capture the control

 
Button btn1 = (Button) this. findViewByID (id );
The findViewByID is inserted into the control id, which is why we need to specify the Control ID during the interface layout.
Associate the control with the object through the control ID, and then you can operate the control through the object.
Tip:
Common objects include buttons and TextView.

(2) bind button events

The code here is the same as the java standard code and is also written using anonymous Event code.
You can set a listener function for the Button object to process the event and call the corresponding method.
Sometimes you can write this for ease of writing.

The effects of the Code in the two red boxes are exactly the same.
 
If you are skilled, we recommend that you use the following method to bind events. You can reduce the number of codes and define fewer objects.

 
After binding events, you need to implement specific functions. For example, click "1" and add "1" to the text section.

 
Other buttons are as follows:
For example, button 2

 
The number button is done. 0 ~ 9 similar

 
Now this interface can be implemented from 0 ~ 9.
Now we can implement the "+" addition function.
Analysis:
The addition function actually requires two tasks in the code.
(1) Save the content of the txt text box
(2) Clear the txt text box
(3) An int Flag is saved in the Code. The current operation is addition.

Basic Idea: Use the addition, subtraction, multiplication, division, and equal key
Add, subtract, and multiply now

 
Division is similar and left for you to implement it yourself. Haha
The code for the equal sign button is as follows:

 
 
Now, a simple calculator is complete. Let's test it now.
Enter 123 + 456
The result should show 579

Then calculate 579-100
479

 
The functions of a simple calculator are basically implemented.
 
We will summarize the following:
 
Pass this experiment
(1) The binding of the "button" event and writing of the button event should be mastered.
(2) understand the Startup Process of Android applications
The program first creates a structure from OnCreate
(3) data type conversion
For example, convert String to float

Float to String
Note: The content of the text box cannot be directly sent to the String, which must be converted.
Because getText () does not return a String

 
(4) Pay attention to the [Scope] of variables]

The anonymous function in the event cannot access the err_txt object. It can only access the global txt variable. Note This.
 


From Zhang jianbo's column

Contact Us

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.