实战项目计算器

24702af67046e56f.png

简单计算器用到的控件

2ebc60714b445443.png

效果图:

793a60694c2e0ff5.png

代码演示:

activity_calculator.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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
android:orientation="vertical"
android:padding="5dp">

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/simple_calculator"
android:gravity="center"
android:textColor="@color/black"
android:textSize="20sp"/>

<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="0"
android:lines="3"
android:textColor="@color/black"
android:textSize="25sp"
android:gravity="right|bottom"/>

<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="5">

<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/Cancel"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_divide"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/divide"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_multiply"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/multiply"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/clear"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_seven"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/seven"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_eight"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/eight"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_nine"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/nine"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_plus"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/plus"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_four"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/four"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_five"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/five"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_six"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/six"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_minus"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/minus"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_one"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/one"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_two"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/two"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_three"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/three"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<ImageButton
android:id="@+id/ib_sqrt"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:src="@drawable/sqrt"
android:scaleType="centerInside"
android:contentDescription="@string/sqrt" />

<Button
android:id="@+id/btn_reciprocal"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/reciprocal"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_zero"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/zero"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_dot"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/dot"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>
<Button
android:id="@+id/btn_equal"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_columnWeight="1"
android:gravity="center"
android:text="@string/equal"
android:textSize="@dimen/button_font_size"
android:textColor="@color/black"
/>


</GridLayout>

</LinearLayout>

</ScrollView>


</LinearLayout>

CalculatorActivity.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
package com.luowen.chapter03;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {

private TextView tv_result;
//第一个操作数
private String firstNum= "";
//运算符
private String operator= "";
//第二个操作数
private String secondNum= "";
//当前的计算结果
private String result= "";
//显示的文本内容
private String showText= "";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
//从布局文件中获取名叫tv_result的文本视图
tv_result = findViewById(R.id.tv_result);
//下面给每个按钮控件都注册了点击监视器
findViewById(R.id.btn_cancel).setOnClickListener(this); //回退按钮
findViewById(R.id.btn_divide).setOnClickListener(this); //除法按钮
findViewById(R.id.btn_multiply).setOnClickListener(this);//乘法按钮
findViewById(R.id.btn_clear).setOnClickListener(this); //清除按钮
findViewById(R.id.btn_seven).setOnClickListener(this); //数字7按钮
findViewById(R.id.btn_eight).setOnClickListener(this);//数字8按钮
findViewById(R.id.btn_nine).setOnClickListener(this);//数字9按钮
findViewById(R.id.btn_plus).setOnClickListener(this);//加号按钮
findViewById(R.id.btn_four).setOnClickListener(this);//数字4按钮
findViewById(R.id.btn_five).setOnClickListener(this);//数字5按钮
findViewById(R.id.btn_six).setOnClickListener(this);//数字6按钮
findViewById(R.id.btn_minus).setOnClickListener(this);//减号按钮
findViewById(R.id.btn_one).setOnClickListener(this);//数字1按钮
findViewById(R.id.btn_two).setOnClickListener(this);//数字2按钮
findViewById(R.id.btn_three).setOnClickListener(this);//数字3按钮
findViewById(R.id.ib_sqrt).setOnClickListener(this);//根号按钮
findViewById(R.id.btn_reciprocal).setOnClickListener(this);//倒数按钮
findViewById(R.id.btn_zero).setOnClickListener(this);//数字0按钮
findViewById(R.id.btn_dot).setOnClickListener(this);//小数点按钮
findViewById(R.id.btn_equal).setOnClickListener(this);//等号按钮

}


@Override
public void onClick(View v) {
String inputText;
//如果是开根号
if(v.getId() == R.id.ib_sqrt){
inputText ="√";
}
else {//除了开根号之外的按钮
inputText=((TextView)v).getText().toString();}
switch (v.getId())
{
//点击了清楚按钮
case R.id.btn_clear:
clear();
break;
//点击了取消按钮
case R.id.btn_cancel:
cancel();
break;
//点击了加,减,乘,除 按钮
case R.id.btn_plus:
case R.id.btn_minus:
case R.id.btn_divide:
case R.id.btn_multiply:
operator = inputText;
refreshText(showText+operator);
break;
//点击了等号按钮
case R.id.btn_equal:
double calculate_result = calculateFour();
refreshOperate(String.valueOf(calculate_result));
refreshText(showText + "=" + result);
break;
//点击了开根号按钮
case R.id.ib_sqrt:
double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
refreshOperate(String.valueOf(sqrt_result));
refreshText(showText + "√=" + result);
break;
//求倒数按钮
case R.id.btn_reciprocal:
double reciprocal_result = 1.0/Double.parseDouble(firstNum);
refreshOperate(String.valueOf(reciprocal_result));
refreshText(showText + "/=" + result);
break;
//点击了其他按钮包括数字和小数点
default:
//上次运算结果已经出来了
if(result.length() > 0 && operator.equals("") ){
clear();
}
//无运算则继续拼接第一个操作数
if(operator.equals(""))
{
firstNum = firstNum + inputText;
}
//有运算符则继续拼接第二个操作数
else {
secondNum = secondNum +inputText;
}
//整数不需要加前面的0
if(showText.equals("0") && !inputText.equals(".") ){
refreshText(inputText);
}
else {
refreshText(showText+inputText);
}
break;
}

}

private double calculateFour() {
switch (operator){
case "+":
return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
case "-":
return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
case "×":
return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
default:
return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);

}
}
// 取消(退格)操作:删除最后一个字符,并同步更新 firstNum、operator、secondNum
private void cancel() {
if (showText.length() > 0) {
// 删除显示文本的最后一个字符
showText = showText.substring(0, showText.length() - 1);
tv_result.setText(showText);

// 同步更新操作数或运算符
if (operator.equals("")) {
// 正在输入第一个操作数
if (firstNum.length() > 0) {
firstNum = firstNum.substring(0, firstNum.length() - 1);
}
} else if (secondNum.length() > 0) {
// 正在输入第二个操作数
secondNum = secondNum.substring(0, secondNum.length() - 1);
} else {
// secondNum为空,说明要删除运算符
operator = "";
}
}
}

private void clear() {
refreshText("");
refreshOperate("");
}
//刷新运算结果
private void refreshOperate(String new_result){
result = new_result;
firstNum = result;
secondNum = "";
operator = "";
}

//刷新文本显示
private void refreshText(String text){
showText =text;
tv_result.setText(showText);

}

}