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.

Related Article

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.