安卓端简易计算器APP实现。



目标

制作简易计算器(Android)。

思路

第一步:页面布局
第二步:事件监听以及实现运算

成品

简易计算器
简易计算器

代码

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.example.calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{
Button btn_0;//0按钮
Button btn_1;//1按钮
Button btn_2;//2按钮
Button btn_3;//3按钮
Button btn_4;//4按钮
Button btn_5;//5按钮
Button btn_6;//6按钮
Button btn_7;//7按钮
Button btn_8;//8按钮
Button btn_9;//9按钮
Button btn_point;//小数点按钮
Button btn_clear;//清除按钮
Button btn_del;//删除按钮
Button btn_plus;//加按钮
Button btn_minus;//减按钮
Button btn_multiply;//乘按钮
Button btn_divide;//除按钮
Button btn_equal;//等号按钮
EditText et_input;//显示输入内容的显示屏
boolean clear_flag;//清空标识

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_0 = (Button) findViewById(R.id.btn_0);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_point = (Button) findViewById(R.id.btn_point);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_del = (Button) findViewById(R.id.btn_del);
btn_plus = (Button) findViewById(R.id.btn_plus);
btn_minus = (Button) findViewById(R.id.btn_minus);
btn_multiply = (Button) findViewById(R.id.btn_multiply);
btn_divide = (Button) findViewById(R.id.btn_divide);
btn_equal = (Button) findViewById(R.id.btn_equal);
//以上是实例化按钮
et_input=(EditText)findViewById(R.id.et_input);//实例化显示屏

btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_point.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equal.setOnClickListener(this);
//以上设置按钮的点击事件




}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str =et_input.getText().toString();
switch (v.getId()) {
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_point:
if(clear_flag){
clear_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str+((Button)v).getText());
break;


case R.id.btn_plus:
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divide:
if(clear_flag){
clear_flag=false;
str="";
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");
break;
case R.id.btn_clear:
clear_flag=false;
str="";
et_input.setText("");
break;
case R.id.btn_del:
if(clear_flag){
clear_flag=false;
str="";
et_input.setText("");
}else if(str!=null&&!str.equals("")){
et_input.setText(str.substring(0, str.length()-1));
}
break;
case R.id.btn_equal:
getResult();
break;

}
}
/**
* 运算结果
*/
private void getResult(){
String exp=et_input.getText().toString();
if(exp==null||exp.equals("")){
return;
}
if(!exp.contains(" ")){
return;
}

if(clear_flag){
clear_flag=false;
return;
}
clear_flag=true;
double result=0;
String s1=exp.substring(0, exp.indexOf(" "));//运算符前面的字符串
String op=exp.substring(exp.indexOf(" ")+1, exp.indexOf(" ")+2);//运算符
String s2=exp.substring(exp.indexOf(" ")+3);//运算符后面的字符串

if(!s1.equals("")&&!s2.equals("")){
double d1=Double.parseDouble(s1);
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=d1+d2;
}else if(op.equals("-")){
result=d1-d2;
}else if(op.equals("×")){
result=d1*d2;
}else if(op.equals("÷")){
if(d2==0){
result=0;
}else{
result=d1/d2;
}
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){
int r=(int)result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}else if(!s1.equals("")&&s2.equals("")){
et_input.setText(exp);
}else if(s1.equals("")&&!s2.equals("")){
double d2=Double.parseDouble(s2);
if(op.equals("+")){
result=0+d2;
}else if(op.equals("-")){
result=0-d2;
}else if(op.equals("×")){
result=0;
}else if(op.equals("÷")){
result=0;
}
if(!s2.contains(".")){
int r=(int)result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}else{
et_input.setText("");
}

}
}

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.calculator.MainActivity" >


<EditText
android:layout_width="fill_parent"
android:layout_height="60dip"
android:id="@+id/et_input"
android:editable="false"
android:gravity="right|bottom"
android:background="@drawable/white_bg"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="C"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_clear"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="DEL"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_del"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="÷"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_divide"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="×"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_multiply"
/>
</LinearLayout>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="7"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_7"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="8"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_8"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="9"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_9"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="-"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_minus"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="4"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_4"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="5"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_5"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="6"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_6"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="+"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_plus"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"

>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"

>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="1"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_1"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="2"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:id="@+id/btn_2"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="3"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:id="@+id/btn_3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
>
<Button
android:layout_width="130dp"
android:layout_height="60dp"
android:text="0"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_0"
/>
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="."
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_selector"
android:textSize="20sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:id="@+id/btn_point"
/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="60dp"
android:layout_height="130dp"
android:text="="
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/orange_selector"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_equal"
/>
</LinearLayout>
</LinearLayout>

color.xml

res/values下新建一个xml.

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="red">#FF0000</color>
<color name="black">#000000</color>
<color name="gray">#808080</color>
<color name="orange">#FFA500</color>
<color name="ashen">#CD853F</color>
</resources>

white_bg.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"/>
<solid
android:color="@color/white"
/>
<!--
<gradient
android:startColor="@color/white"
android:endColor="@color/red"


/>
<stroke
android:width="1dp"
android:color="@color/black"
/>
-->
</shape>

white_selector.xml

1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/gray_bg" android:state_pressed="true"/>
<item android:drawable="@drawable/white_bg"/>
</selector>

出现的问题及解决方法

  1. 新建工程时,出现报错:
    1
    2
    3
    \res\values\styles.xml:7: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.
    \res\values-v11\styles.xml:7: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light'.
    \res\values-v14\styles.xml:8: error: Error retrieving parent for item: No resource found that matches the given name 'Theme.AppCompat.Light.DarkActionBar'.

解决方法:
顺着改为:

1
2
3
style name="AppBaseTheme" parent="android:Theme.Light"
style name="AppBaseTheme" parent="android:Theme.Holo.Light"
style name="AppBaseTheme"parent="android:Theme.Holo.Light.DarkActionBar"

然后menu会报错,改为:

1
android:showAsAction="never"

  1. 导入工程时报错The import android cannot be resolved

解决:在新建Android项目的时候,eclipes新更新的ADT,在创建的Android版本最低版本低于4.0的时候,会新建一个v7的项目,把里面的包导入就可以。如果不想有警告的话,直接在创建项目的时候把最低版本设置为4.0以上就可以。并且不会出现第一个问题。

  1. 定义按钮时错误提示:Button cannot be resolved to a type

解决:没导包,快捷键ctrl+shift+o搞定。

笔记

去掉标题栏

在res/values/string.xml中和AndroidManifest.xml去掉相应代码即可。

颜色需新建xml

存放在res/values/colors中,RGB颜色表示。

drawble下文件结构

颜色和选择器
颜色和选择器

注意

Main_Activity.java中,首先命名控件,给一个id,其次,按钮要设置点击事件,监听到该控件动作。
activity_main.xml中,线性结构可以嵌套使用。

思考:线性结构代码显臃肿,换成TableLayout也许更佳。并且计算器需要进一步优化,目前存在小的问题,例如,加减乘除符号可以在没有数字输入时顺序打印在屏幕上。总之,基本实现了计算器的功能。

APP下载

https://github.com/hubojing/Calculator