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

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

服务器之家 - 编程语言 - Android - Android Studio实现简易计算器

Android Studio实现简易计算器

2022-03-11 15:27RikkaTheWorld Android

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

如果是制作简易计算器的话是基本没有难点的,供大家参考,具体内容如下

步骤是先写好界面布局,将按钮的布局、字号颜色啥的做好,再就是设置监听器。

使用了NoTitleBar的主题

代码如下:

activity_main里关于界面的代码:

?
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
<?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="fill_parent"
  android:layout_height="90dp"
  android:background="@drawable/white_bg"
  android:editable="false"
  android:gravity="right|bottom"
  android:paddingBottom="20dp"
  android:paddingRight="20dp"
  android:textSize="50sp" />
 
 <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/bt_clr"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:text="C"
  android:gravity="right|bottom"
  android:textSize="30sp"
  android:background="@drawable/white_selector"
  android:paddingRight="15sp"
  android:paddingBottom="15sp"
  />
  <Button
   android:id="@+id/bt_del"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="DEL"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
  />
  <Button
   android:id="@+id/bt_div"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="÷"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_mul"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="×"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   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/bt_7"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="7"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_8"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="8"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_9"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="9"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_sub"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="-"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   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/bt_4"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="4"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_5"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="5"
   android:gravity="right|bottom"
   android:textSize="30sp"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_6"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="6"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   android:paddingRight="15sp"
   android:paddingBottom="15sp"
   />
  <Button
   android:id="@+id/bt_add"
   android:layout_width="80dp"
   android:layout_height="80dp"
   android:text="+"
   android:textSize="30sp"
   android:gravity="right|bottom"
   android:layout_marginLeft="10dp"
   android:background="@drawable/white_selector"
   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/bt_1"
      android:text="1"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
     <Button
      android:layout_width="80dp"
      android:layout_height="80dp"
      android:id="@+id/bt_2"
      android:text="2"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:layout_marginLeft="10dp"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
     <Button
      android:layout_width="80dp"
      android:layout_height="80dp"
      android:id="@+id/bt_3"
      android:text="3"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:layout_marginLeft="10dp"
      android:background="@drawable/white_selector"
      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/bt_0"
      android:text="0"
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
     <Button
      android:layout_width="80dp"
      android:layout_height="80dp"
      android:id="@+id/bt_pt"
      android:text="."
      android:textSize="30sp"
      android:gravity="right|bottom"
      android:layout_marginLeft="10dp"
      android:background="@drawable/white_selector"
      android:paddingRight="15sp"
      android:paddingBottom="15sp"
      />
    </LinearLayout>
 
   </LinearLayout>
 
   <Button
    android:id="@+id/bt_eq"
    android:layout_width="80dp"
    android:layout_height="170dp"
    android:layout_marginLeft="10dp"
    android:background="@drawable/orange_selector"
    android:gravity="right|bottom"
    android:text="="
    android:textSize="30sp"
    android:paddingRight="15sp"
    android:paddingBottom="15sp"
    />
 
  </LinearLayout>
</LinearLayout>

Mainactivity的代码:

?
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
package com.example.administrator.calculatordemo;
 
import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
 
public class MainActivity extends Activity implements View.OnClickListener{
 Button bt_0,bt_1,bt_2,bt_3,bt_4,bt_5,bt_6,bt_7,bt_8,bt_9,bt_pt;
 Button bt_mul,bt_div,bt_add,bt_sub;
 Button bt_clr,bt_del,bt_eq;
 EditText et_input;
 boolean clr_flag; //判断et中是否清空
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //实例化对象
  setContentView(R.layout.activity_main);
  bt_0= (Button) findViewById(R.id.bt_0);
  bt_1= (Button) findViewById(R.id.bt_1);
  bt_2= (Button) findViewById(R.id.bt_2);
  bt_3= (Button) findViewById(R.id.bt_3);
  bt_4= (Button) findViewById(R.id.bt_4);
  bt_5= (Button) findViewById(R.id.bt_5);
  bt_6= (Button) findViewById(R.id.bt_6);
  bt_7= (Button) findViewById(R.id.bt_7);
  bt_8= (Button) findViewById(R.id.bt_8);
  bt_9= (Button) findViewById(R.id.bt_9);
  bt_pt= (Button) findViewById(R.id.bt_pt);
  bt_add= (Button) findViewById(R.id.bt_add);
  bt_sub= (Button) findViewById(R.id.bt_sub);
  bt_mul= (Button) findViewById(R.id.bt_mul);
  bt_div= (Button) findViewById(R.id.bt_div);
  bt_clr= (Button) findViewById(R.id.bt_clr);
  bt_del= (Button) findViewById(R.id.bt_del);
  bt_eq= (Button) findViewById(R.id.bt_eq);
  et_input= (EditText) findViewById(R.id.et_input);
 
  //设置按钮的点击事件
  bt_0.setOnClickListener(this);
  bt_1.setOnClickListener(this);
  bt_2.setOnClickListener(this);
  bt_3.setOnClickListener(this);
  bt_4.setOnClickListener(this);
  bt_5.setOnClickListener(this);
  bt_6.setOnClickListener(this);
  bt_7.setOnClickListener(this);
  bt_8.setOnClickListener(this);
  bt_9.setOnClickListener(this);
  bt_pt.setOnClickListener(this);
  bt_add.setOnClickListener(this);
  bt_sub.setOnClickListener(this);
  bt_mul.setOnClickListener(this);
  bt_div.setOnClickListener(this);
  bt_clr.setOnClickListener(this);
  bt_del.setOnClickListener(this);
  bt_eq.setOnClickListener(this);
 }
 
 @Override
 public void onClick(View v) {
  String str=et_input.getText().toString();
   switch (v.getId()){
    case R.id.bt_0:
    case R.id.bt_1:
    case R.id.bt_2:
    case R.id.bt_3:
    case R.id.bt_4:
    case R.id.bt_5:
    case R.id.bt_6:
    case R.id.bt_7:
    case R.id.bt_8:
    case R.id.bt_9:
    case R.id.bt_pt:
     if(clr_flag){
      clr_flag=false;
      str="";
      et_input.setText("");
     }
     et_input.setText(str+((Button)v).getText());
     break;
    case R.id.bt_add:
    case R.id.bt_sub:
    case R.id.bt_mul:
    case R.id.bt_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.bt_clr:
     if(clr_flag)
      clr_flag=false;
     str="";
     et_input.setText("");
     break;
    case R.id.bt_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.bt_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("");
  }
 }
}

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

原文链接:https://blog.csdn.net/rikkatheworld/article/details/77119625

延伸 · 阅读

精彩推荐
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03