本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下
先来个效果图:
功能: 满足加减乘除四则运算规则,有回退、清除功能。
下面的代码只是完成基本功能,若添加背景图先看看下面的方法:Android Studio App设置背景图片
1、本地准备好图片,复制它,粘贴进mipmap(drawable)文件夹。
2、在activity_main.xml里添加下面代码,注意添加位置。
3、完成,效果图:
content_main.xml文件(页面布局,content_main.xml代码包含在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<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/next" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context=".MainActivity" tools:showIn="@layout/activity_main"> <!--<TextView--> <!--android:layout_width="wrap_content"--> <!--android:layout_height="wrap_content"--> <!--android:text="Hello World!"--> <!--app:layout_constraintBottom_toBottomOf="parent"--> <!--app:layout_constraintLeft_toLeftOf="parent"--> <!--app:layout_constraintRight_toRightOf="parent"--> <!--app:layout_constraintTop_toTopOf="parent" />--> <LinearLayout android:layout_width="fill_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/input" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:editable="false" android:hint="@string/shuru" /> <EditText android:id="@+id/output" android:layout_width="match_parent" android:layout_height="70dp" android:layout_gravity="center" android:editable="true" android:gravity="right" android:hint="@string/shuchu" /> <RelativeLayout android:layout_width="350dp" android:layout_height="wrap_content" android:layout_gravity="center"> <Button android:id="@+id/clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:text="@string/clear" android:textSize="40sp" /> <Button android:id="@+id/back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/clear" android:text="@string/back" android:textSize="40sp" /> <Button android:id="@+id/divide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/back" android:text="@string/divide" android:textSize="40sp" /> <Button android:id="@+id/multiply" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/divide" android:text="@string/multiply" android:textSize="40sp" /> <Button android:id="@+id/seven" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/clear" android:text="@string/seven" android:textSize="40sp" /> <Button android:id="@+id/eight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/seven" android:layout_below="@id/divide" android:text="@string/eight" android:textSize="40sp" /> <Button android:id="@+id/nine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/multiply" android:layout_toRightOf="@id/eight" android:text="@string/nine" android:textSize="40sp" /> <Button android:id="@+id/subtract" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/multiply" android:layout_toRightOf="@id/nine" android:text="@string/subtract" android:textSize="40sp" /> <Button android:id="@+id/four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/seven" android:text="@string/four" android:textSize="40sp" /> <Button android:id="@+id/five" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/eight" android:layout_toRightOf="@id/four" android:text="@string/five" android:textSize="40sp" /> <Button android:id="@+id/six" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/nine" android:layout_toRightOf="@id/five" android:text="@string/six" android:textSize="40sp" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/subtract" android:layout_toRightOf="@id/six" android:text="@string/add" android:textSize="40sp" /> <Button android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/four" android:text="@string/one" android:textSize="40sp" /> <Button android:id="@+id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/five" android:layout_toRightOf="@id/one" android:text="@string/two" android:textSize="40sp" /> <Button android:id="@+id/three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/six" android:layout_toRightOf="@id/two" android:text="@string/three" android:textSize="40sp" /> <Button android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="146dp" android:layout_alignParentRight="true" android:layout_below="@id/add" android:layout_toRightOf="@id/three" android:text="@string/result" android:textSize="40sp" /> <Button android:id="@+id/zero" android:layout_width="175dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/one" android:text="@string/zero" android:textSize="40sp" /> <Button android:id="@+id/dot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/three" android:layout_toRightOf="@id/zero" android:text="@string/dot" android:textSize="40sp" /> </RelativeLayout> </LinearLayout> </android.support.constraint.ConstraintLayout>
strings.xml(content_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<resources> <string name="app_name">Calculator</string> <string name="action_settings">Settings</string> <string name="title_activity_page2">page2</string> <string name="next">下一页</string> <string name="zero">0</string> <string name="one">1</string> <string name="two">2</string> <string name="three">3</string> <string name="four">4</string> <string name="five">5</string> <string name="six">6</string> <string name="seven">7</string> <string name="eight">8</string> <string name="nine">9</string> <string name="add">+</string> <string name="subtract">-</string> <string name="multiply">*</string> <string name="divide">/</string> <string name="clear">CE</string> <string name="back"><-</string> <string name="result">=</string> <string name="shuru">请输入:</string> <string name="shuchu">结果:</string> <string name="dot">.</string> <string name="resultText">计算式</string> </resources>
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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352package com.example.dell.calculator; import android.content.Intent; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.app.Activity; import android.content.Context; import android.view.View.OnClickListener; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Button; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.DecimalFormat; import java.text.NumberFormat; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity { private EditText output = null; private EditText input = null; private Button btn0 = null; private Button btn1 = null; private Button btn2 = null; private Button btn3 = null; private Button btn4 = null; private Button btn5 = null; private Button btn6 = null; private Button btn7 = null; private Button btn8 = null; private Button btn9 = null; private Button btnadd = null; private Button btnsubtract = null; private Button btnmultiply = null; private Button btndivide = null; private Button btnclear = null; private Button btnback = null; private Button btndot = null; private Button btnresult = null; private String text = "";//保存输入的数字和符号 private Double result = 0.0;//输出结果 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); output = (EditText) findViewById(R.id.output); input = (EditText) findViewById(R.id.input); btn0 = (Button) findViewById(R.id.zero); btn1 = (Button) findViewById(R.id.one); btn2 = (Button) findViewById(R.id.two); btn3 = (Button) findViewById(R.id.three); btn4 = (Button) findViewById(R.id.four); btn5 = (Button) findViewById(R.id.five); btn6 = (Button) findViewById(R.id.six); btn7 = (Button) findViewById(R.id.seven); btn8 = (Button) findViewById(R.id.eight); btn9 = (Button) findViewById(R.id.nine); btnadd = (Button) findViewById(R.id.add); btnsubtract = (Button) findViewById(R.id.subtract); btnmultiply = (Button) findViewById(R.id.multiply); btndivide = (Button) findViewById(R.id.divide); btnclear = (Button) findViewById(R.id.clear); btnback = (Button) findViewById(R.id.back); btndot = (Button) findViewById(R.id.dot); btnresult = (Button) findViewById(R.id.result); //设置按钮侦听事件 btn0.setOnClickListener(listener); btn1.setOnClickListener(listener); btn2.setOnClickListener(listener); btn3.setOnClickListener(listener); btn4.setOnClickListener(listener); btn5.setOnClickListener(listener); btn6.setOnClickListener(listener); btn7.setOnClickListener(listener); btn8.setOnClickListener(listener); btn9.setOnClickListener(listener); btnadd.setOnClickListener(listener); btnsubtract.setOnClickListener(listener); btnmultiply.setOnClickListener(listener); btndivide.setOnClickListener(listener); btnclear.setOnClickListener(listener); btnback.setOnClickListener(listener); btndot.setOnClickListener(listener); btnresult.setOnClickListener(listener); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } //public void onClickNext(View view) { // Intent intent = new Intent(this,page2.class); // startActivity(intent); // } private OnClickListener listener = new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { //输入数字 case R.id.zero: num(0); break; case R.id.one: num(1); break; case R.id.two: num(2); break; case R.id.three: num(3); break; case R.id.four: num(4); break; case R.id.five: num(5); break; case R.id.six: num(6); break; case R.id.seven: num(7); break; case R.id.eight: num(8); break; case R.id.nine: num(9); break; case R.id.dot: dot(); break; //执行运算 case R.id.add: add(); break; case R.id.subtract: sub(); break; case R.id.multiply: multiply(); break; case R.id.divide: divide(); break; case R.id.clear: clear(); break; case R.id.back: back(); break; //计算结果 case R.id.result: result(); break; default: break; } input.setText(text); output.setText(String.valueOf(result)); } }; private void num(int i) { // TODO Auto-generated method stub text = text + String.valueOf(i); } private void dot() { // TODO Auto-generated method stub text = text + "."; } private void clear() { // TODO Auto-generated method stub text = ""; result = null; input.setText(""); output.setText(""); } private void back() { // TODO Auto-generated method stub String str = text.substring(0, text.length()-1); text = str; } private void add() { // TODO Auto-generated method stub text += "+"; } private void sub() { // TODO Auto-generated method stub text += "-"; } private void multiply() { // TODO Auto-generated method stub text += "*"; } private void divide() { // TODO Auto-generated method stub text += "/"; } //计算输出结果 private void result() { // TODO Auto-generated method stub result = testOperation(text); } public Double testOperation(String s){ //分割字符然后放进数组 String s1 =s.replace("+","-"); String[] str = s1.split("-"); double total1=0; //先遍历数组,把里面的乘除结果算出来 for(String str1:str){ if(str1.contains("*")||str1.contains("/")){ double total = 0; for(int i =0;i<str1.length();){ int count =1; a:for(int j =i+1;j<str1.length();j++){ char c =str1.charAt(j); if(c=='*'||c=='/'){ break a; }else{ count++; } } //将数字截取出来 String s2 =str1.substring(i,i+count); double d = Double.parseDouble(s2); if(i==0){ total = d; }else{ char c1 = str1.charAt(i-1); if(c1=='*'){ total*=d; }else if(c1=='/'){ //如果除数为0,直接返回null; if(d == 0) return null; total/=d; } } i+=count+1; } s= s.replace(str1, total+""); } } //进行加减运算 for(int i =0;i<s.length();i++){ int count =1; a:for(int j=i+1;j<s.length();j++){ char c = s.charAt(j); if(c=='+'||c=='-'){ break a; }else{ count++; } } String s3= s.substring(i,i+count); double d2 = Double.parseDouble(s3); if(i==0){ total1 = d2; }else{ char c = s.charAt(i-1); if(c=='+'){ total1+=d2; }else if(c=='-'){ total1-=d2; } } i+=count; } return total1; } }
更多计算器功能实现,请点击专题: 计算器功能汇总 进行学习
关于Android计算器功能的实现,查看专题:Android计算器 进行学习。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持靠谱客。
最后
以上就是干净小蝴蝶最近收集整理的关于android studio实现简单的计算器功能的全部内容,更多相关android内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复