服务器之家:专注于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:32Super__M Android

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

本文实例为大家分享了android studio实现计算器的具体代码,供大家参考,具体内容如下

效果图:

android studio实现计算器

资源文件:

color.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="colorPrimary">#3F51B5</color>
 <color name="colorPrimaryDark">#303F9F</color>
 <color name="colorAccent">#FF4081</color>
 <color name="white">#FFFFFF</color>
 <color name="black">#000000</color>
 <color name="zi">#FFFFFF</color>
 <color name="gray">#BEBEBE</color>
 <color name="green">#9AFF9A</color>
 <color name="littlegreen">#F0FFFF</color>
</resources>

white.xml

设置input text的填充色为白色

?
1
2
3
4
5
6
<?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"/>
</shape>

selector.xml

点击按钮时产生阴影效果

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

equeal.xml

同理,等号的阴影效果

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

布局文件

?
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
<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/jisuanqi"
 
 tools:context="com.example.administrator.calculate.MainActivity"
 >
 
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="30dp"
  android:paddingLeft="20dp"
  android:paddingRight="20dp"
  android:paddingTop="20dp">
 
  <EditText
   android:id="@+id/input"
   android:layout_width="fill_parent"
   android:layout_height="60dp"
   android:background="@drawable/white"
   android:editable="false"
   android:gravity="right|bottom"
   />
 </LinearLayout>
 <LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="20dp"
 android:orientation="horizontal"
 android:gravity="center">
 <Button
  android:layout_width="65dp"
  android:layout_height="65dp"
  android:text="C"
  android:background="@drawable/selector"
  android:gravity="center"
  android:textSize="25sp"
  android:id="@+id/clear"
  />
 <Button
  android:layout_width="65dp"
  android:layout_height="65dp"
  android:text="←"
  android:background="@drawable/selector"
  android:gravity="center"
  android:layout_marginLeft="10dp"
  android:textSize="23sp"
  android:id="@+id/delete"
  />
 <Button
  android:layout_width="65dp"
  android:layout_height="65dp"
  android:text="×"
  android:background="@drawable/selector"
  android:gravity="center"
  android:layout_marginLeft="10dp"
  android:textSize="25sp"
  android:id="@+id/cheng"
  />
 <Button
  android:layout_width="65dp"
  android:layout_height="65dp"
  android:text="÷"
  android:background="@drawable/selector"
  android:gravity="center"
  android:layout_marginLeft="10dp"
  android:textSize="25sp"
  android:id="@+id/clu"
  />
 
</LinearLayout>
 
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:orientation="horizontal"
  android:gravity="center">
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="7"
   android:background="@drawable/selector"
   android:gravity="center"
   android:textSize="25sp"
   android:id="@+id/num7"
   />
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="8"
   android:background="@drawable/selector"
   android:gravity="center"
   android:layout_marginLeft="10dp"
   android:textSize="25sp"
   android:id="@+id/num8"
   />
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="9"
   android:background="@drawable/selector"
   android:gravity="center"
   android:layout_marginLeft="10dp"
   android:textSize="25sp"
   android:id="@+id/num9"
   />
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="-"
   android:background="@drawable/selector"
   android:gravity="center"
   android:layout_marginLeft="10dp"
   android:textSize="25sp"
   android:id="@+id/charjian"
   />
 
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginTop="20dp"
  android:orientation="horizontal"
  android:gravity="center">
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="4"
   android:background="@drawable/selector"
   android:gravity="center"
   android:textSize="25sp"
   android:id="@+id/num4"
   />
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="5"
   android:background="@drawable/selector"
   android:gravity="center"
   android:layout_marginLeft="10dp"
   android:textSize="25sp"
   android:id="@+id/num5"
   />
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="6"
   android:background="@drawable/selector"
   android:gravity="center"
   android:layout_marginLeft="10dp"
   android:textSize="25sp"
   android:id="@+id/num6"
   />
  <Button
   android:layout_width="65dp"
   android:layout_height="65dp"
   android:text="+"
   android:background="@drawable/selector"
   android:gravity="center"
   android:layout_marginLeft="10dp"
   android:textSize="25sp"
   android:id="@+id/charadd"
   />
 
 </LinearLayout>
 
 <LinearLayout
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:gravity="center_horizontal"
  android:layout_marginTop="10dp"
  >
  <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="65dp"
     android:layout_height="65dp"
     android:background="@drawable/selector"
     android:text="1"
     android:gravity="center"
     android:textSize="25sp"
     android:id="@+id/num1"
     />
    <Button
     android:layout_width="65dp"
     android:layout_height="65dp"
     android:text="2"
     android:background="@drawable/selector"
     android:gravity="center"
     android:layout_marginLeft="10dp"
     android:textSize="25sp"
     android:id="@+id/num2"/>
    <Button
     android:layout_width="65dp"
     android:layout_height="65dp"
     android:text="3"
     android:background="@drawable/selector"
     android:gravity="center"
     android:layout_marginLeft="10dp"
     android:textSize="25sp"
     android:id="@+id/num3"/>
   </LinearLayout>
 
   <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="10dp">
 
    <Button
     android:id="@+id/num0"
     android:layout_width="140dp"
     android:layout_height="65dp"
     android:text="0"
     android:background="@drawable/selector"
     android:gravity="center"
     android:textSize="25sp" />
 
    <Button
     android:id="@+id/dian"
     android:layout_width="65dp"
     android:layout_height="65dp"
     android:layout_marginLeft="10dp"
     android:background="@drawable/selector"
     android:gravity="center"
     android:text="."
     android:textSize="25sp" />
 
   </LinearLayout>
 
  </LinearLayout>
 
  <LinearLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <Button
    android:layout_width="65dp"
    android:layout_height="140dp"
    android:text="="
    android:background="@drawable/equal"
    android:gravity="center"
    android:layout_marginLeft="10dp"
    android:textSize="25sp"
    android:id="@+id/equai"/>
  </LinearLayout>
 
 </LinearLayout>
 
</LinearLayout>

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
package com.example.administrator.calculate;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
 
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
 
public class MainActivity extends AppCompatActivity {
 
 @BindView(R.id.input)
 EditText input;
 @BindView(R.id.clear)
 Button clear;
 @BindView(R.id.delete)
 Button delete;
 @BindView(R.id.cheng)
 Button cheng;
 @BindView(R.id.clu)
 Button chu;
 @BindView(R.id.num7)
 Button num7;
 @BindView(R.id.num8)
 Button num8;
 @BindView(R.id.num9)
 Button num9;
 @BindView(R.id.charjian)
 Button charjian;
 @BindView(R.id.num4)
 Button num4;
 @BindView(R.id.num5)
 Button num5;
 @BindView(R.id.num6)
 Button num6;
 @BindView(R.id.charadd)
 Button charadd;
 @BindView(R.id.num1)
 Button num1;
 @BindView(R.id.num2)
 Button num2;
 @BindView(R.id.num3)
 Button num3;
 @BindView(R.id.num0)
 Button num0;
 @BindView(R.id.dian)
 Button dian;
 @BindView(R.id.equai)
 Button equal;
 
 private String ss="";
 private boolean fu=false;
 private boolean num=false;
 private boolean point=false;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ButterKnife.bind(this);
 
 
 }
 
 @OnClick({R.id.clear, R.id.delete, R.id.cheng, R.id.clu, R.id.num7, R.id.num8, R.id.num9, R.id.charjian, R.id.num4, R.id.num5, R.id.num6, R.id.charadd, R.id.num1, R.id.num2, R.id.num3, R.id.num0, R.id.dian, R.id.equai})
 public void onViewClicked(View view) {
  switch (view.getId()) {
   case R.id.clear:
   {
    ss="";
    input.setText(ss);
   }
    break;
   case R.id.delete:
   {
    if(ss.indexOf(" ")==ss.length()-3)
    {
     ss= ss.substring(0,ss.length() - 2);
    }
    if(ss.length()>0)
    {
     ss= ss.substring(0,ss.length() - 1);
    }
    input.setText(ss);
   }
    break;
   case R.id.cheng:
   {
    if(ss.length()==0)
    {
     break;
    }
    if(ss.contains(" "))
    {
     if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break;
     getResult();
    }
    fu=true;
    ss+=" × ";
    input.setText(ss);
   }
    break;
   case R.id.clu:
   {
    if(ss.length()==0)
    {
     break;
    }
    if(ss.contains(" "))
    {
     if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break;
     getResult();
    }
    fu=true;
    ss+=" ÷ ";
    input.setText(ss);
   }
    break;
   case R.id.num7:
   {
    ss+="7";
    input.setText(ss);
   }
    break;
   case R.id.num8:
   {
    ss+="8";
    input.setText(ss);
   }
    break;
   case R.id.num9:
   {
    ss+="9";
    input.setText(ss);
   }
    break;
   case R.id.charjian:
   {
    if(ss.length()==0)
    {
     break;
    }
    if(ss.contains(" "))
    {
     if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break;
     getResult();
    }
    fu=true;
    ss+=" - ";
    input.setText(ss);
   }
    break;
   case R.id.num4:
   {
    ss+="4";
    input.setText(ss);
   }
    break;
   case R.id.num5:
   {
    ss+="5";
    input.setText(ss);
   }
    break;
   case R.id.num6:
   {
    ss+="6";
    input.setText(ss);
   }
    break;
   case R.id.charadd:
   {
    if(ss.length()==0)
    {
     break;
    }
    if(ss.contains(" "))
    {
     if(ss.indexOf(" ")==ss.length()-3||ss.indexOf(" ")==ss.length()-2||ss.indexOf(" ")==ss.length()-1) break;
     getResult();
    }
    fu=true;
    ss+=" + ";
    input.setText(ss);
   }
    break;
   case R.id.num1:
   {
    ss+="1";
    input.setText(ss);
   }
    break;
   case R.id.num2:
   {
    ss+="2";
    input.setText(ss);
   }
    break;
   case R.id.num3:
   {
    ss+="3";
    input.setText(ss);
   }
    break;
   case R.id.num0:
   {
    ss+="0";
    input.setText(ss);
   }
    break;
   case R.id.dian:
   {
    if(ss.length()==0||ss.indexOf(" ")==ss.length()-3||ss.lastIndexOf(".")>ss.indexOf(" "))
    {
     break;
    }
    else
    {
     ss+=".";
     input.setText(ss);
    }
   }
    break;
   case R.id.equai:
    getResult();
    break;
  }
 }
 private void getResult()
 {
  double result=0;
  if(ss==null||ss.equals("")) return;
  if(!ss.contains(" ")) return;
  String s1=ss.substring(0,ss.indexOf(" "));
  String op=ss.substring(ss.indexOf(" ")+1,ss.indexOf(" ")+2);
  String s2=ss.substring(ss.indexOf(" ")+3);
  if(!s1.equals("")&&!s2.equals(""))
  {
   double d1=Double.parseDouble(s1);
   double d2=Double.parseDouble(s2 );
   switch (op)
   {
    case "+": result=d1+d2;break;
    case "-": result=d1-d2;break;
    case "×": result=d1*d2;break;
    case "÷":
    {
     if(d2==0)
     {
      Toast.makeText(this, "不能除以零", Toast.LENGTH_SHORT).show();
      break;
     }
     result=d1/d2*1.0;
    }
    break;
   }
 
   int r = (int) result;
   if(r==result)
   {
    input.setText(""+r);
    ss=""+r;
   }
   else
   {
    input.setText(result+"");
    ss=""+result;
   }
 
  }
 }
}

在AndroidManifest.xml文件中activity 后面添加

?
1
android:theme=”@style/Theme.AppCompat.DayNight.NoActionBar”

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

原文链接:https://blog.csdn.net/sb_Ihateyou/article/details/75699844

延伸 · 阅读

精彩推荐
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

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

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

    Abby代黎明9602022-03-02
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

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

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

    Android开发网4672021-01-03
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

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

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

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

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

    鉴客6192021-03-27
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

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

    Android开发网7452021-03-11
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

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

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

    liuhe68810052021-05-03
  • Android汇总Android视频录制中常见问题

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

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

    yh_thu5192021-04-28
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

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

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

    Ruthless8342021-03-31