激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费观看高清视频网站 | 黄色av.com | 九九热在线视频免费观看 | 久久男人视频 | 国产亚洲精品综合一区91555 | 奇米影视奇米色777欧美 | 成年人网站视频免费 | 免费国产在线观看 | 中国漂亮护士一级a毛片 | 一级片999 | 欧美色视频免费 | 91看片淫黄大片欧美看国产片 | 亚州视频在线 | 国产大片全部免费看 | 黄色免费小视频网站 | 欧美日韩夜夜 | 久久综合久久精品 | 激情小说另类 | 91精品影视| 精品久久久久久中文字幕 | 国产手机在线视频 | 久久国产不卡 | av在线一区二区三区四区 | 成人444kkkk在线观看 | 欧美一区2区三区4区公司二百 | 伊人在线 | 亚洲草逼视频 | 亚洲午夜在线视频 | 1314成人网| 日韩aⅴ一区二区三区 | 免费a级观看 | 香蕉视频破解 | 亚洲一区二区三区四区精品 | 欧美成人免费在线视频 | 久久最新视频 | japanese massage tube| 亚洲精品久久久久久 | 亚洲日色 | a免费毛片 | 毛片毛片免费看 | 欧美成人精品一区二区 |