计算器——android应用开发

这阵子乱七八糟的东西做的太多,整理一下

没做的太复杂,数值计算的时候偷懒直接就用a+b计算的

主要工作用在界面和点击按钮时引发的操作(对TextView显示文本的处理和将输入的数值赋给寄存两个计算数值的变量a、b)

在点击数字按钮的时候需要将数字输入到TextView显示出来,而且不能单纯的只显示这一个数字,需要跟之前的输入文本连接起来一起显示。而且,在点击AC按钮、等于按钮和操作符按钮后,再次输入数字需要将之前TextView显示的文本信息清掉,重新显示文本,此时显示的是另一个计算数值了。这里用了一个clear变量标志是否清屏。

运算符需要考虑两种情况:第一种,比较简单,类似于3.4×5,屏幕上已经输入了一个数3.4,接下来点击×按钮时,需要先把已经输入的3.4赋给变量a,并clear=true(下次输入数字的时候需要清屏);第二种,则类似于3.4×5÷7,点击÷按钮后,先是需要计算出3.4×5的值赋给变量a,然后clear=true。这里用了一个set变量标记是否需要运算。

小数点三种情况:一种是屏幕上已经输入了小数点,例如6.7,这种不用采取任何操作直接返回;再就是,需要清屏,例如已经输入了5×,这个时候点小数点按钮屏幕上直接显示0.就可以了。其他情况,直接在已经输入的数字后面加个“.”就可以了

再有就是正负号按钮,举个例子,TextView显示的是6.13点按钮后需要显示-6.13,再点按钮时显示6.13,这个对字符串的处理一下就行。

下面是实现计算器的界面的代码:

MainActivity.java
  1 package com.example.calculator;
  2 
  3 import android.os.Bundle;
  4 import android.app.Activity;
  5 import android.util.Log;
  6 import android.view.Menu;
  7 import android.view.View;
  8 import android.view.View.OnClickListener;
  9 import android.widget.Button;
 10 import android.widget.TextView;
 11 
 12 public class MainActivity extends Activity {
 13     public String TAG = "MainActivity";
 14     private TextView txt = null;
 15     private double a = 0;
 16     private double b = 0;
 17     private double result = 0;
 18 
 19     // 运算标志位:标记是否需要运算,第一次按运算符的时候不计算,等待第二个数输入
 20     private boolean set = false;
 21 
 22     // 清屏标志位:标记是否需要清屏
 23     private boolean clear = false;
 24 
 25     // 运算符标记:+ 1;- 2;× 3;÷ 4
 26     private int flag = 0;
 27     Calculator calculator = new Calculator();
 28 
 29     @Override
 30     public void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_main);
 33         txt = (TextView) findViewById(R.id.textView1);
 34         Button button_ac = (Button) findViewById(R.id.button_ac);
 35         // 全部归位
 36         button_ac.setOnClickListener(new OnClickListener() {
 37             public void onClick(View v) {
 38                 set = false;
 39                 clear = false;
 40                 flag = 0;
 41                 a = 0;
 42                 b = 0;
 43                 result = 0;
 44                 txt.setText("0");
 45             }
 46         });
 47 
 48         // 数字按钮
 49         Button button0 = (Button) findViewById(R.id.button0);
 50         button0.setOnClickListener(new OnClickListener() {
 51             public void onClick(View v) {
 52                 numberButton(0);
 53             }
 54         });
 55         Button button1 = (Button) findViewById(R.id.button1);
 56         button1.setOnClickListener(new OnClickListener() {
 57             public void onClick(View v) {
 58                 numberButton(1);
 59             }
 60         });
 61         Button button2 = (Button) findViewById(R.id.button2);
 62         button2.setOnClickListener(new OnClickListener() {
 63             public void onClick(View v) {
 64                 numberButton(2);
 65             }
 66         });
 67         Button button3 = (Button) findViewById(R.id.button3);
 68         button3.setOnClickListener(new OnClickListener() {
 69             public void onClick(View v) {
 70                 numberButton(3);
 71             }
 72         });
 73         Button button4 = (Button) findViewById(R.id.button4);
 74         button4.setOnClickListener(new OnClickListener() {
 75             public void onClick(View v) {
 76                 numberButton(4);
 77             }
 78         });
 79         Button button5 = (Button) findViewById(R.id.button5);
 80         button5.setOnClickListener(new OnClickListener() {
 81             public void onClick(View v) {
 82                 numberButton(5);
 83             }
 84         });
 85         Button button6 = (Button) findViewById(R.id.button6);
 86         button6.setOnClickListener(new OnClickListener() {
 87             public void onClick(View v) {
 88                 numberButton(6);
 89             }
 90         });
 91         Button button7 = (Button) findViewById(R.id.button7);
 92         button7.setOnClickListener(new OnClickListener() {
 93             public void onClick(View v) {
 94                 numberButton(7);
 95             }
 96         });
 97         Button button8 = (Button) findViewById(R.id.button8);
 98         button8.setOnClickListener(new OnClickListener() {
 99             public void onClick(View v) {
100                 numberButton(8);
101             }
102         });
103         Button button9 = (Button) findViewById(R.id.button9);
104         button9.setOnClickListener(new OnClickListener() {
105             public void onClick(View v) {
106                 numberButton(9);
107             }
108         });
109 
110         // 等于:计算结果,重置运算标志位和运算符标记,置清屏标志位
111         Button button10 = (Button) findViewById(R.id.button10);
112         button10.setOnClickListener(new OnClickListener() {
113             public void onClick(View v) {
114                 String input = (String) txt.getText();
115                 if (set) {
116                     b = Double.parseDouble(input);
117                     result = calculator.compute(a, b, flag);
118                     txt.setText(Double.toString(result));
119                     a = 0;
120                     b = 0;
121                     set = false;
122                     clear = true;
123                     flag = 0;
124                     return;
125                 }
126             }
127         });
128         // 小数点
129         Button button11 = (Button) findViewById(R.id.button11);
130         button11.setOnClickListener(new OnClickListener() {
131             public void onClick(View v) {
132                 String input = (String) txt.getText();
133                 if (input.indexOf('.') != -1)
134                     return;
135                 if (clear || input.equals("0")) {
136                     txt.setText("0.");
137                     clear = false;
138                     return;
139                 }
140                 txt.setText(input + ".");
141             }
142         });
143         // 运算符
144         // 加运算
145         Button button12 = (Button) findViewById(R.id.button12);
146         button12.setOnClickListener(new OnClickListener() {
147             public void onClick(View v) {
148                 operationButton(1);
149             }
150         });
151         // 减运算
152         Button button13 = (Button) findViewById(R.id.button13);
153         button13.setOnClickListener(new OnClickListener() {
154             public void onClick(View v) {
155                 String input = (String) txt.getText();
156                 operationButton(2);
157             }
158         });
159         // 乘运算
160         Button button14 = (Button) findViewById(R.id.button14);
161         button14.setOnClickListener(new OnClickListener() {
162             public void onClick(View v) {
163                 operationButton(3);
164             }
165         });
166         // 除运算
167         Button button15 = (Button) findViewById(R.id.button15);
168         button15.setOnClickListener(new OnClickListener() {
169             public void onClick(View v) {
170                 operationButton(4);
171             }
172         });
173         // 正负
174         Button button16 = (Button) findViewById(R.id.button16);
175         button16.setOnClickListener(new OnClickListener() {
176             public void onClick(View v) {
177                 String input = (String) txt.getText();
178                 if (input.equals("0"))
179                     return;
180                 if (input.startsWith("-")) {
181                     txt.setText(input.substring(1));
182                     return;
183                 }
184                 txt.setText("-" + input);
185             }
186         });
187     }
188 
189     // 点击数字键的响应动作:清屏标志位为TRUE时,清屏,并重置清屏标志位
190     public void numberButton(int i) {
191         String input = (String) txt.getText();
192         if (clear || input.equals("0")) {
193             txt.setText(Integer.toString(i));
194             clear = false;
195             return;
196         }
197         txt.setText(input + i);
198     }
199 
200     // 点击运算符键的响应动作:运算标志位为TRUE时,计算结果,并将结果赋给a
201     public void operationButton(int i) {
202         String input = (String) txt.getText();
203         if (set) {
204             b = Double.parseDouble(input);
205             result = calculator.compute(a, b, flag);
206             txt.setText(Double.toString(result));
207             a = result;
208             b = 0;
209             clear = true;
210             flag = i;
211             return;
212         }
213         a = Double.parseDouble(input);
214         set = true;
215         clear = true;
216         flag = i;
217     }
218 
219     @Override
220     public boolean onCreateOptionsMenu(Menu menu) {
221         getMenuInflater().inflate(R.menu.activity_main, menu);
222         return true;
223     }
224 }

xml布局文件

activity_main.xml
  1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:background="#555555" >
  6 
  7     <TextView
  8         android:id="@+id/textView1"
  9         android:layout_width="wrap_content"
 10         android:layout_height="wrap_content"
 11         android:layout_alignParentLeft="true"
 12         android:layout_alignParentRight="true"
 13         android:background="#FFFF99"
 14         android:text="0"
 15         android:textAppearance="?android:attr/textAppearanceLarge" />
 16 
 17     <LinearLayout
 18         android:id="@+id/linearLayout1"
 19         android:layout_width="fill_parent"
 20         android:layout_height="wrap_content"
 21         android:layout_alignParentLeft="true"
 22         android:layout_alignParentRight="true"
 23         android:layout_below="@+id/textView1"
 24         android:layout_marginTop="26dp" >
 25 
 26         <Button
 27             android:id="@+id/button_ac"
 28             android:layout_width="80dp"
 29             android:layout_height="match_parent"
 30             android:text="AC" />
 31 
 32         <Button
 33             android:id="@+id/button16"
 34             android:layout_width="80dp"
 35             android:layout_height="match_parent"
 36             android:text="+/-" />
 37 
 38         <Button
 39             android:id="@+id/button15"
 40             android:layout_width="80dp"
 41             android:layout_height="match_parent"
 42             android:text="÷" />
 43 
 44         <Button
 45             android:id="@+id/button14"
 46             android:layout_width="80dp"
 47             android:layout_height="match_parent"
 48             android:text="×" />
 49     </LinearLayout>
 50 
 51     <LinearLayout
 52         android:id="@+id/linearLayout2"
 53         android:layout_width="fill_parent"
 54         android:layout_height="wrap_content"
 55         android:layout_alignParentLeft="true"
 56         android:layout_below="@+id/linearLayout1" >
 57 
 58         <Button
 59             android:id="@+id/button7"
 60             android:layout_width="80dp"
 61             android:layout_height="match_parent"
 62             android:text="7" />
 63 
 64         <Button
 65             android:id="@+id/button8"
 66             android:layout_width="80dp"
 67             android:layout_height="match_parent"
 68             android:text="8" />
 69 
 70         <Button
 71             android:id="@+id/button9"
 72             android:layout_width="80dp"
 73             android:layout_height="match_parent"
 74             android:text="9" />
 75 
 76         <Button
 77             android:id="@+id/button13"
 78             android:layout_width="80dp"
 79             android:layout_height="fill_parent"
 80             android:text="-" />
 81     </LinearLayout>
 82 
 83     <LinearLayout
 84         android:id="@+id/linearLayout3"
 85         android:layout_width="fill_parent"
 86         android:layout_height="wrap_content"
 87         android:layout_alignParentLeft="true"
 88         android:layout_below="@+id/linearLayout2" >
 89 
 90         <Button
 91             android:id="@+id/button4"
 92             android:layout_width="80dp"
 93             android:layout_height="match_parent"
 94             android:text="4" />
 95 
 96         <Button
 97             android:id="@+id/button5"
 98             android:layout_width="80dp"
 99             android:layout_height="match_parent"
100             android:text="5" />
101 
102         <Button
103             android:id="@+id/button6"
104             android:layout_width="80dp"
105             android:layout_height="match_parent"
106             android:text="6" />
107 
108         <Button
109             android:id="@+id/button12"
110             android:layout_width="80dp"
111             android:layout_height="match_parent"
112             android:text="+" />
113     </LinearLayout>
114 
115     <LinearLayout
116         android:id="@+id/linearLayout4"
117         android:layout_width="fill_parent"
118         android:layout_height="wrap_content"
119         android:layout_alignParentLeft="true"
120         android:layout_below="@+id/linearLayout3" >
121 
122         <Button
123             android:id="@+id/button1"
124             android:layout_width="80dp"
125             android:layout_height="match_parent"
126             android:text="1" />
127 
128         <Button
129             android:id="@+id/button2"
130             android:layout_width="80dp"
131             android:layout_height="match_parent"
132             android:text="2" />
133 
134         <Button
135             android:id="@+id/button3"
136             android:layout_width="80dp"
137             android:layout_height="match_parent"
138             android:text="3" />
139 
140         <Button
141             android:id="@+id/button11"
142             android:layout_width="80dp"
143             android:layout_height="match_parent"
144             android:text="." />
145     </LinearLayout>
146 
147     <LinearLayout
148         android:layout_width="fill_parent"
149         android:layout_height="wrap_content"
150         android:layout_alignParentLeft="true"
151         android:layout_alignParentRight="true"
152         android:layout_below="@+id/linearLayout4" >
153 
154         <Button
155             android:id="@+id/button0"
156             android:layout_width="80dp"
157             android:layout_height="match_parent"
158             android:text="0" />
159 
160         <Button
161             android:id="@+id/button10"
162             android:layout_width="80dp"
163             android:layout_height="match_parent"
164             android:text="=" />
165     </LinearLayout>
166 
167 </RelativeLayout>
  1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:background="#555555" >
  6 
  7     <TextView
  8         android:id="@+id/textView1"
  9         android:layout_width="wrap_content"
 10         android:layout_height="wrap_content"
 11         android:layout_alignParentLeft="true"
 12         android:layout_alignParentRight="true"
 13         android:background="#FFFF99"
 14         android:text="0"
 15         android:textAppearance="?android:attr/textAppearanceLarge" />
 16 
 17     <LinearLayout
 18         android:id="@+id/linearLayout1"
 19         android:layout_width="fill_parent"
 20         android:layout_height="wrap_content"
 21         android:layout_alignParentLeft="true"
 22         android:layout_alignParentRight="true"
 23         android:layout_below="@+id/textView1"
 24         android:layout_marginTop="26dp" >
 25 
 26         <Button
 27             android:id="@+id/button_ac"
 28             android:layout_width="80dp"
 29             android:layout_height="match_parent"
 30             android:text="AC" />
 31 
 32         <Button
 33             android:id="@+id/button16"
 34             android:layout_width="80dp"
 35             android:layout_height="match_parent"
 36             android:text="+/-" />
 37 
 38         <Button
 39             android:id="@+id/button15"
 40             android:layout_width="80dp"
 41             android:layout_height="match_parent"
 42             android:text="÷" />
 43 
 44         <Button
 45             android:id="@+id/button14"
 46             android:layout_width="80dp"
 47             android:layout_height="match_parent"
 48             android:text="×" />
 49     </LinearLayout>
 50 
 51     <LinearLayout
 52         android:id="@+id/linearLayout2"
 53         android:layout_width="fill_parent"
 54         android:layout_height="wrap_content"
 55         android:layout_alignParentLeft="true"
 56         android:layout_below="@+id/linearLayout1" >
 57 
 58         <Button
 59             android:id="@+id/button7"
 60             android:layout_width="80dp"
 61             android:layout_height="match_parent"
 62             android:text="7" />
 63 
 64         <Button
 65             android:id="@+id/button8"
 66             android:layout_width="80dp"
 67             android:layout_height="match_parent"
 68             android:text="8" />
 69 
 70         <Button
 71             android:id="@+id/button9"
 72             android:layout_width="80dp"
 73             android:layout_height="match_parent"
 74             android:text="9" />
 75 
 76         <Button
 77             android:id="@+id/button13"
 78             android:layout_width="80dp"
 79             android:layout_height="fill_parent"
 80             android:text="-" />
 81 
 82     </LinearLayout>
 83 
 84     <LinearLayout
 85         android:id="@+id/linearLayout3"
 86         android:layout_width="fill_parent"
 87         android:layout_height="wrap_content"
 88         android:layout_alignParentLeft="true"
 89         android:layout_below="@+id/linearLayout2" >
 90 
 91         <Button
 92             android:id="@+id/button4"
 93             android:layout_width="80dp"
 94             android:layout_height="match_parent"
 95             android:text="4" />
 96 
 97         <Button
 98             android:id="@+id/button5"
 99             android:layout_width="80dp"
100             android:layout_height="match_parent"
101             android:text="5" />
102 
103         <Button
104             android:id="@+id/button6"
105             android:layout_width="80dp"
106             android:layout_height="match_parent"
107             android:text="6" />
108 
109         <Button
110             android:id="@+id/button12"
111             android:layout_width="80dp"
112             android:layout_height="match_parent"
113             android:text="+" />
114 
115     </LinearLayout>
116 
117     <LinearLayout
118         android:id="@+id/linearLayout4"
119         android:layout_width="fill_parent"
120         android:layout_height="wrap_content"
121         android:layout_alignParentLeft="true"
122         android:layout_below="@+id/linearLayout3" >
123 
124         <Button
125             android:id="@+id/button1"
126             android:layout_width="80dp"
127             android:layout_height="match_parent"
128             android:text="1" />
129 
130         <Button
131             android:id="@+id/button2"
132             android:layout_width="80dp"
133             android:layout_height="match_parent"
134             android:text="2" />
135 
136         <Button
137             android:id="@+id/button3"
138             android:layout_width="80dp"
139             android:layout_height="match_parent"
140             android:text="3" />
141 
142         <Button
143             android:id="@+id/button11"
144             android:layout_width="80dp"
145             android:layout_height="match_parent"
146             android:text="." />
147 
148     </LinearLayout>
149 
150     <LinearLayout
151         android:layout_width="fill_parent"
152         android:layout_height="wrap_content"
153         android:layout_alignParentLeft="true"
154         android:layout_alignParentRight="true"
155         android:layout_below="@+id/linearLayout4" >
156 
157         <Button
158             android:id="@+id/button0"
159             android:layout_width="80dp"
160             android:layout_height="match_parent"
161             android:text="0" />
162 
163         <Button
164             android:id="@+id/button10"
165             android:layout_width="80dp"
166             android:layout_height="match_parent"
167             android:text="=" />
168     </LinearLayout>
169 
170 </RelativeLayout>

计算两个数的值:

Calculator.java
 1 package com.example.calculator;
 2 
 3 public class Calculator {
 4     private double x = 0;
 5     private double y = 0;
 6     private double result = 0;
 7 
 8     Calculator(double a, double b) {
 9         x = a;
10         y = b;
11     }
12 
13     Calculator() {
14 
15     }
16 
17     public double compute(double a, double b, int flag) {
18         switch (flag) {
19         case 1:
20             x = a;
21             y = b;
22             result = x + y;
23             break;
24         case 2:
25             x = a;
26             y = b;
27             result = x - y;
28             break;
29         case 3:
30             x = a;
31             y = b;
32             result = x * y;
33             break;
34         case 4:
35             x = a;
36             y = b;
37             result = x / y;
38             break;
39         default:
40             result = 0;
41         }
42         return result;
43     }
44 }
posted @ 2012-11-07 16:21  Bay  阅读(800)  评论(0编辑  收藏  举报