服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android开发学习实现简单计算器

Android开发学习实现简单计算器

2022-12-08 14:15阿柠xn Android

这篇文章主要为大家详细介绍了Android实现一个简单计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

这里是用线性布局实现的计算器,为防止以后再回顾知识代码找不到,特将代码贴在这里:

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">
 
 <EditText
 android:id="@+id/et_input"
 android:layout_width="match_parent"
 android:layout_height="100dp"
 android:paddingBottom="5dp"
 android:paddingRight="5dp"
 android:textSize="50sp"
 android:gravity="right"
 android:textColor="#00ff00"/>
 
 
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="30dp"
 android:orientation="horizontal"
 android:gravity="center_horizontal">
 
 <Button
  android:id="@+id/btn_clr"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="C"
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  android:textColor="#ff0000"/>
 
 <Button
  android:id="@+id/btn_del"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="Ba"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp" />
 <Button
  android:id="@+id/btn_add"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="+"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_sub"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="-"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 
 
 
 </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:id="@+id/btn_7"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="7"
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_8"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="8"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_9"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="9"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_mul"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="×"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 
 </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:id="@+id/btn_4"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="4"
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_5"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="5"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_6"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="6"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
 <Button
  android:id="@+id/btn_div"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="÷"
  android:textSize="30sp"
  android:layout_marginLeft="10dp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp" />
 
 </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="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_1"
   android:text="1"
   android:textSize="30sp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_2"
   android:text="2"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_3"
   android:text="3"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  </LinearLayout>
  <LinearLayout
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_marginTop="10dp">
  <Button
   android:layout_width="170dp"
   android:layout_height="80dp"
   android:id="@+id/btn_0"
   android:text="0"
   android:textSize="30sp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:id="@+id/btn_pt"
   android:text="."
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  </LinearLayout>
 
 </LinearLayout>
 
 <Button
  android:id="@+id/btn_eq"
  android:layout_width="80dp"
  android:layout_height="170dp"
  android:layout_marginLeft="10dp"
  android:text="="
  android:textSize="30sp"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  android:gravity="center"
  android:background="@color/colorAccent"/>
 
 </LinearLayout>
 
</LinearLayout>

Activity.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
package com.example.myapplication;
 
import androidx.appcompat.app.AppCompatActivity;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class Main2Activity extends AppCompatActivity implements View.OnClickListener{
 
 //创建Button对象 也就是activity_main.xml里所设置的ID
 Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9,btn_pt;
 Button btn_mul,btn_div,btn_add,btn_sub;
 Button btn_clr,btn_del,btn_eq;
 EditText et_input;
 boolean clr_flag; //判断et编辑文本框中是否清空
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main2);
 //实例化对象
 setContentView(R.layout.activity_main2);
 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_pt= (Button) findViewById(R.id.btn_pt);
 btn_add= (Button) findViewById(R.id.btn_add);
 btn_sub= (Button) findViewById(R.id.btn_sub);
 btn_mul= (Button) findViewById(R.id.btn_mul);
 btn_div= (Button) findViewById(R.id.btn_div);
 btn_clr= (Button) findViewById(R.id.btn_clr);
 btn_del= (Button) findViewById(R.id.btn_del);
 btn_eq= (Button) findViewById(R.id.btn_eq);
 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_pt.setOnClickListener(this);
 btn_add.setOnClickListener(this);
 btn_sub.setOnClickListener(this);
 btn_mul.setOnClickListener(this);
 btn_div.setOnClickListener(this);
 btn_clr.setOnClickListener(this);
 btn_del.setOnClickListener(this);
 btn_eq.setOnClickListener(this);
 }
 
 @Override
 public void onClick(View v) {
 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_pt:
  if(clr_flag){
   clr_flag=false;
   str="";
   et_input.setText("");
  }
  et_input.setText(str+((Button)v).getText());
  break;
  case R.id.btn_add:
  case R.id.btn_sub:
  case R.id.btn_mul:
  case R.id.btn_div:
  if(clr_flag){
   clr_flag=false;
   str="";
   et_input.setText("");
  }
  if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷")) {
   str=str.substring(0,str.indexOf(" "));
  }
  et_input.setText(str+" "+((Button)v).getText()+" ");
  break;
  case R.id.btn_clr:
  if(clr_flag)
   clr_flag=false;
  str="";
  et_input.setText("");
  break;
  case R.id.btn_del: //判断是否为空,然后在进行删除
  if(clr_flag){
   clr_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_eq: //单独运算最后结果
  getResult();//调用下面的方法
  break;
 }
 }
 
 private void getResult() {
 String exp=et_input.getText().toString();
 if(exp==null||exp.equals("")) return ;
 //因为没有运算符所以不用运算
 if(!exp.contains(" ")){
  return ;
 }
 if(clr_flag){
  clr_flag=false;
  return;
 }
 clr_flag=true;
 //截取运算符前面的字符串
 String s1=exp.substring(0,exp.indexOf(" "));
 //截取的运算符
 String op=exp.substring(exp.indexOf(" ")+1,exp.indexOf(" ")+2);
 //截取运算符后面的字符串
 String s2=exp.substring(exp.indexOf(" ")+3);
 double cnt=0;
 if(!s1.equals("")&&!s2.equals("")){
  double d1=Double.parseDouble(s1);
  double d2=Double.parseDouble(s2);
  if(op.equals("+")){
  cnt=d1+d2;
  }
  if(op.equals("-")){
  cnt=d1-d2;
  }
  if(op.equals("×")){
  cnt=d1*d2;
  }
  if(op.equals("÷")){
  if(d2==0) cnt=0;
  else cnt=d1/d2;
  }
  if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")) {
  int res = (int) cnt;
  et_input.setText(res+"");
  }else {
  et_input.setText(cnt+"");}
 }
 //如果s1是空 s2不是空 就执行下一步
 else if(!s1.equals("")&&s2.equals("")){
  double d1=Double.parseDouble(s1);
  if(op.equals("+")){
  cnt=d1;
  }
  if(op.equals("-")){
  cnt=d1;
  }
  if(op.equals("×")){
  cnt=0;
  }
  if(op.equals("÷")){
  cnt=0;
  }
  if(!s1.contains(".")) {
  int res = (int) cnt;
  et_input.setText(res+"");
  }else {
  et_input.setText(cnt+"");}
 }
 //如果s1是空 s2不是空 就执行下一步
 else if(s1.equals("")&&!s2.equals("")){
  double d2=Double.parseDouble(s2);
  if(op.equals("+")){
  cnt=d2;
  }
  if(op.equals("-")){
  cnt=0-d2;
  }
  if(op.equals("×")){
  cnt=0;
  }
  if(op.equals("÷")){
  cnt=0;
  }
  if(!s2.contains(".")) {
  int res = (int) cnt;
  et_input.setText(res+"");
  }else {
  et_input.setText(cnt+"");}
 }
 else {
  et_input.setText("");
 }
 }
}

结果图如下所示:

Android开发学习实现简单计算器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wxnshuai/article/details/105456391

延伸 · 阅读

精彩推荐
  • AndroidAndroid同时安装Release和Debug版本的方法

    Android同时安装Release和Debug版本的方法

    这篇文章主要介绍了Android同时安装Release和Debug版本的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    玲の9232022-09-08
  • AndroidAndroid使用xml文件资源定义菜单实现方法示例

    Android使用xml文件资源定义菜单实现方法示例

    这篇文章主要介绍了Android使用xml文件资源定义菜单实现方法,结合实例形式分析了Android资源文件管理及xml配置自定义菜单相关操作技巧,需要的朋友可以参考...

    水中鱼之19994632022-09-29
  • AndroidAndroid自定义view仿淘宝快递物流信息时间轴

    Android自定义view仿淘宝快递物流信息时间轴

    这篇文章主要为大家详细介绍了Android自定义view仿淘宝快递物流信息时间轴,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    u01397933010992022-02-22
  • AndroidAndroid开发之使用GridView展示图片的方法

    Android开发之使用GridView展示图片的方法

    这篇文章主要介绍了Android开发之使用GridView展示图片的方法,涉及Android使用GridView操作图片的布局与图片调用功能实现技巧,需要的朋友可以参考下...

    取号啦李刚10502021-05-04
  • AndroidAndroid加载图片内存溢出问题解决方法

    Android加载图片内存溢出问题解决方法

    这篇文章主要介绍了Android加载图片内存溢出问题解决方法,本文讲解使用BitmapFactory.Options解决内存溢出问题,需要的朋友可以参考下...

    Android开发网5192021-03-25
  • AndroidAndroid实现悬浮窗全系统版本

    Android实现悬浮窗全系统版本

    这篇文章主要为大家详细介绍了Android实现悬浮窗全系统版本,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    林一天11432022-11-08
  • AndroidAndroid实现拍照和录制视频功能

    Android实现拍照和录制视频功能

    这篇文章主要为大家详细介绍了Android实现拍照和录制视频功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    HuangGui9710224952022-02-25
  • AndroidAndroid显示GIF图片实例代码

    Android显示GIF图片实例代码

    这篇文章主要介绍了Android显示GIF图片实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    poclist4332022-02-16