激情久久久_欧美视频区_成人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: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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人黄色小视频网站 | 在线成人av | 日本在线不卡一区二区 | av免费不卡国产观看 | 国产精品成人久久 | 黄色大片网站在线观看 | 欧美成人二区 | 九九热精品在线 | 电影91| 久久精品视频首页 | 性片网站 | 国产午夜亚洲精品午夜鲁丝片 | 免费毛片随便看 | 欧美日韩在线免费观看 | 欧美色另类 | 国产在线精品一区二区 | a级高清免费毛片av在线 | 黄色毛片a级 | 91美女视频在线观看 | 免费观看视频网站 | 国产一级毛片av | 国产精品视频一区二区三区四 | 在线中文字幕播放 | 午夜精品久久久久久中宇 | 黄色一级片免费在线观看 | 日日狠狠久久偷偷四色综合免费 | 亚洲综合网站 | 在线视频 欧美日韩 | 日夜操天天干 | 日韩黄色片在线观看 | 精品一区二区三区日本 | 成人毛片网站 | 草草视频免费观看 | 羞羞色院91精品网站 | 久久国产精品系列 | 国产精品久久久久久久久久了 | 欧美人人干 | 亚洲欧洲日韩av | 久久久成人999亚洲区美女 | 久久久日韩精品一区二区三区 | 香蕉视频破解 |