Development of simple calculator for android development example study notes

Development tools: android studio

Goal: Develop a simple calculator that can add, subtract, multiply and divide

1. Open Android studio and create a new project (basic operation, directly above)




After hitting finish, it's time to start our code show

2. Add the components used in our simple calculator to the app->src->main->res->layout->activity_main layout file, including two TextViews (used to display input data and calculation results respectively) and 15 Buttons (0-9 ten basic digital buttons, +-*/ four basic operation buttons, an answer button for controlling the output result. Of course, you can also add "clear screen", "back" and other function buttons , just add a few more Buttons)


The properties of each Button and TextView should include its id, size, and position. Button also needs to set Android: text, such as:


Of course, you can also control the positional relationship of each control through the design view design, but I suggest that when you first start learning, you should choose to type each property by hand. First, it is convenient to familiarize yourself with the properties and property values ​​of each control. Second, it will be later. The call can also clearly know which attribute of the control corresponds to, and the design is convenient but not easy to grasp, which often affects the whole body. (You may also have noticed that there is a property in Button called onClick, which we will talk about later, don’t be confused)


This is what I designed with design, please ignore its aesthetic effect

3. Install a listener for each Button so that it can respond to click events

This is the magic of the onClick attribute just mentioned. If there is no such attribute, then we have to write a separate response function for each Button. After adding the onClick attribute to each Button, we only need to write an onClick function, and use switch to judge the source of the response and distinguish them.


The onClick function accepts a parameter View, first use view.getID() to get the source of the clicked Button, and then add the text of the button to the string displayed in the TextView, and display it through setText().

If there is a clear screen button, then its response event is the TextView object setText("").

For the button that controls the output of the result, we need to use a third-party library Arityto calculate the expression. ArityThe library files can be downloaded from the download address provided by Andou.com . Copy it to the directory , if the directory does not exist, create one by yourself; in the project browsing area on the left side of Android Studio, select the view; right-click on it and select ; in the pop-up selection box, click .arity-2.1.2.jarCalulator\app\libsProjectapp->libs->arity-2.1.2,jarAdd as Library...OK

Then, write the response event of the answer button


Here, we use the eval function of the Symbols object in the third-party library.

So far, a simple calculator has been completed. run test


Reference: The blog "Teach you how to get started with Android"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325217926&siteId=291194637