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

本文链接:https://my.lmcjl.com/post/7976.html

展开阅读全文

4 评论

留下您的评论.