写此时为了记录自己的开发历程,方便以后的查询。二则运算(能力有限,也没有想往更深处写),菜鸟级别,代码为参考慕课网上,听课代码,并加上自己的理解和参考的一些博客!有问题的地方欢迎指正,感激不尽!开发工具为Android studio。(第一次写博客,可读性估计很差)

废话不多说,代码轮上来(顺序按慕课老师讲解顺序)

1.先是xml(UI吧)

  效果图:

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
290
291
<EditText
android:layout_width="fill_parent"
android:layout_marginTop="20dp"
android:layout_height="70dip"
android:id="@+id/et_input"
android:background="@color/white"
android:editable="false"
android:gravity="right|bottom"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:gravity="center_horizontal"
>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="C"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_clear"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="DEL"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_del"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="÷"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_divide"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="×"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_multiply"
/>
</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:layout_width="70dp"
android:layout_height="70dp"
android:text="7"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_7"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_8"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_9"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="-"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_minus"
/>
</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:layout_width="70dp"
android:layout_height="70dp"
android:text="4"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_4"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_5"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_6"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="+"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:layout_marginLeft="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_plus"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_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="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_1"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:id="@+id/btn_2"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:gravity="right|bottom"
android:layout_marginLeft="10dp"
android:id="@+id/btn_3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:layout_width="150dp"
android:layout_height="70dp"
android:text="0"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_0"
/>
<Button
android:layout_width="70dp"
android:layout_height="70dp"
android:text="."
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:background="@drawable/white_select"
android:textSize="20sp"
android:layout_marginLeft="10dp"
android:gravity="right|bottom"
android:id="@+id/btn_point"
/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="70dp"
android:layout_height="150dp"
android:text="="
android:background="@drawable/orange_select"
android:layout_marginLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textSize="20sp"
android:gravity="right|bottom"
android:id="@+id/btn_equal"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:editable="false"
android:text="能力有限,只能进行二则运算——by sorgs"
android:textSize="16dp"
android:textColor="@color/black"
android:layout_marginTop="30dp"
android:background="@color/sandybrown"
android:id="@+id/textureView" />

xml里面注释不多,想必也不需太多解释,都是很简单布局和控件。其中有个背景颜色的问题需要详细说明

1.存放颜色

首先我们需要一个在value/下添加一个colors.xml文件用来存放我们需要的颜色不然你全用#xxxxxx得多累,而且还不还用颜色,这里我们可以百度,有配好的颜色的代码。我们直接复制到我们的代码中,然后引用。

建立xml

为了在方便我们布局和颜色的搭配,也是在慕课老师那里学了一招。可以在res/drawable下面建立xml。

我们来依次看看这些文件都是怎么做的。

###(1).ashend_bg.xml(几个_bg都是颜色不一样,意义差不多)
目的是提供背景颜色,其实也可以不要颜色。直接在xml中添加颜色,这个主要是提供圆角

1
2
3
4
5
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid
android:color="@color/beige" />
</shape>

此处使用shape。(这里有更加完美的解释 http://www.cnblogs.com/cyanfei/archive/2012/07/27/2612023.html

(2).orange_select.xml(select也是颜色不一样,内容差不多)

目的是为了按钮提供颜色和点击时会有另外一种颜色

1
2
3
4
5
6
7
8
<span style="font-size:18px;">
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ashend_bg"
android:state_pressed="true"/>
<item android:drawable="@drawable/orange_bg"/>
</selector></span>

此处使用select(详细用法请参考http://blog.csdn.net/shakespeare001/article/details/7788400/

2.然后是我们的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
package com.sorgs.administrator.caclulatordemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{ `
Button btn_0;//0数字按钮
Button btn_1;//1数字按钮
Button btn_2;//2数字按钮
Button btn_3;//3数字按钮
Button btn_4;//4数字按钮
Button btn_5;//5数字按钮
Button btn_6;//6数字按钮
Button btn_7;//7数字按钮
Button btn_8;//8数字按钮
Button btn_9;//9数字按钮
Button btn_point;//小数点按钮
Button btn_clear;//清除按钮
Button btn_del;//删除按钮
Button btn_plus;//加好按钮
Button btn_minus;//减号按钮
Button btn_divide;//除号按钮
Button btn_multiply;//乘号按钮
Button btn_equle;//等于按钮
//以上建立按钮
EditText et_input;//显示输出内容的显示屏
boolean clear_flag;//清空标识,用于等号之后清空
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);//控制xml为content_main
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_point = (Button) findViewById(R.id.btn_point);
btn_del = (Button) findViewById(R.id.btn_del);
btn_plus = (Button) findViewById(R.id.btn_plus);
btn_clear = (Button) findViewById(R.id.btn_clear);
btn_minus = (Button) findViewById(R.id.btn_minus);
btn_multiply = (Button) findViewById(R.id.btn_multiply);
btn_divide = (Button) findViewById(R.id.btn_divide);
btn_equle = (Button) findViewById(R.id.btn_equal);
//以上是实例化按钮
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_point.setOnClickListener(this);
btn_del.setOnClickListener(this);
btn_plus.setOnClickListener(this);
btn_clear.setOnClickListener(this);
btn_minus.setOnClickListener(this);
btn_multiply.setOnClickListener(this);
btn_divide.setOnClickListener(this);
btn_equle.setOnClickListener(this);
//以上设置按钮的点击事件
}
@Override
public void onClick(View v) {
String str = et_input.getText().toString(); //取出显示屏内容
switch (v.getId()){ //判断点的是那个按钮
case R.id.btn_0: //建立数字0—9和.
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_point:
if(clear_flag){ //
clear_flag =false;
str = ""; //计算下一个时候,应将原来的设置为空
et_input.setText("");
}
et_input.setText(str+((Button)v).getText()); //将点击的文字添加到输入框里面(str原来输入框中内容)
break;
case R.id.btn_plus: //建立+-×÷
case R.id.btn_minus:
case R.id.btn_multiply:
case R.id.btn_divide:
if(clear_flag){
clear_flag =false;
str = ""; //计算下一个时候,应将原来的设置为空
et_input.setText("");
}
et_input.setText(str+" "+((Button)v).getText()+" ");//将点击的运算符添加到输入框前后有“ ”用于区别
break;
case R.id.btn_del: //建立删除
if(clear_flag){
clear_flag= false;
str = ""; //计算下一个时候,应将原来的设置为空
et_input.setText("");
}else if (str != null &&!str.equals("")) { //如果显示屏里面不是NULL也不是空
et_input.setText(str.substring(0,str.length()-1)); //从后面长度减一
}
break;
case R.id.btn_clear: //建立清除
clear_flag = false;
str = ""; //计算下一个时候,应将原来的设置为空
et_input.setText(""); //将显示屏内容置空
break;
case R.id.btn_equal: //建立等于
getResult(); //获取结算结果
break;
}
}
//进行计算
private void getResult(){
String exp = et_input.getText().toString(); //取出显示屏内容并转化为String
if (exp == null||exp.equals("")){//如果内容为null和空,直接返回
return;
}
if(!exp.contains(" ")){//如果不包含空格(运算符前面有空格),直接返回(比如点了数字,没有运算符)
return;
}
if(clear_flag){
clear_flag = false;
return;
}
clear_flag = true;
double result = 0; //定义一个double的result=0
String s1 = exp.substring(0,exp.indexOf(' '));//截取运算符前面的字符
String op = exp.substring(exp.indexOf(' ')+1,exp.indexOf(' ')+2);//截取运算符
String s2 = exp.substring(exp.indexOf(' ')+3);//截取运算符后面的字符
if(!s1.equals("")&&!s2.equals("")){ //如果S1或者S2不为空
double d1 = Double.parseDouble(s1); //强制将S1转换为double类型
double d2 = Double.parseDouble(s2); //强制将S2转换为double类型
if(op.equals("+")){ //如果op为四中情况的方案
result = d1+d2;
}else if(op.equals("-")){
result = d1-d2;
}else if(op.equals("×")){
result = d1*d2;
}else if(op.equals("÷")){
if(d2==0){
Toast.makeText(MainActivity.this, "除数不能为0!!!",Toast.LENGTH_LONG).show();
et_input.setText("0");
}else{
result = d1/d2;
}
}
if(!s1.contains(".")&&!s2.contains(".")&&!op.equals("÷")){ //如果没有小数点则为int类型且op不为÷
int r = (int)result; //强制转换为int类型
et_input.setText(r+"");
}else{ //其中含有小数点,则输出double类型
et_input.setText(result+"");
}
}else if(!s1.equals("")&&s2.equals("")){ //S1不为空,S2为空
double d1 = Double.parseDouble(s1);
result = d1;
Toast.makeText(MainActivity.this, "不具备运算",Toast.LENGTH_LONG).show();
et_input.setText(result+""); //不进行计算,返回S1
}else if(s1.equals("")&&!s2.equals("")){ //S1为空,S2不为空
double d2 = Double.parseDouble(s2);
if(op.equals("+")){
result = 0+d2;
}else if(op.equals("-")){
result = 0-d2;
}else if(op.equals("×")){
result = 0;
}else if(op.equals("÷")){
result = 0;
}
if(!s2.contains(".")){
int r = (int)result;
et_input.setText(r+"");
}else{
et_input.setText(result+"");
}
}else{
et_input.setText("");
}
}
}

这里采用消息显示的一个方法,个人觉得这样很好看。

1
Toast.makeText(MainActivity.this, "除数不能为0!!!",Toast.LENGTH_LONG).show();

效果图:

Toast.makeText的方法也不在赘述了,因为很多博客都有写(http://www.cnblogs.com/ycxyyzw/archive/2013/03/12/2955845.html