制作一个简易计算器——基于Android Studio实现
Posted GGBeng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了制作一个简易计算器——基于Android Studio实现相关的知识,希望对你有一定的参考价值。
一个计算器android程序的源码部分分为主干和细节两部分。
一、主干
1. 主干的构成
- 计算器的布局
- 事件(即计算器上的按钮、文本框)监听
- 实现计算
2. 详细解释
假设我们的项目名为Calculator,而布局名称(Layout Name)为默认的activity_main 。即设置如下图所示:
在这种前提下,有:
- 设置计算器布局的文件:Calculator/app/src/main/res/layout/activity_main.xml
- 事件监听和计算实现在同一个文件里:Calculator/app/src/main/java/下的一个子目录里的MainActivity.java
即如下图所示:
3. 主干代码部分
- 计算器布局代码(写在activity_main.xml文件里):
1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 > 6 <LinearLayout 7 android:layout_width="fill_parent" 8 android:layout_height="fill_parent" 9 android:orientation="vertical" > 10 <EditText 11 android:id="@+id/input" 12 android:layout_width="fill_parent" 13 android:layout_height="wrap_content" 14 android:layout_gravity="center" 15 android:editable="false" 16 android:hint="@string/shuru" /> 17 18 <EditText 19 android:id="@+id/output" 20 android:layout_width="fill_parent" 21 android:layout_height="wrap_content" 22 android:layout_gravity="center" 23 android:editable="true" 24 android:gravity="right" 25 android:hint="@string/shuchu" /> 26 27 <RelativeLayout 28 android:layout_width="fill_parent" 29 android:layout_height="wrap_content" > 30 31 <Button 32 android:id="@+id/seven" 33 android:layout_width="80dp" 34 android:layout_height="70dp" 35 android:layout_alignParentLeft="true" 36 android:text="@string/seven" 37 android:textSize="40sp" /> 38 39 <Button 40 android:id="@+id/eight" 41 android:layout_width="80dp" 42 android:layout_height="70dp" 43 android:layout_toRightOf="@id/seven" 44 android:text="@string/eight" 45 android:textSize="40sp" /> 46 47 <Button 48 android:id="@+id/nine" 49 android:layout_width="80dp" 50 android:layout_height="70dp" 51 android:layout_toRightOf="@id/eight" 52 android:text="@string/nine" 53 android:textSize="40sp" /> 54 55 <Button 56 android:id="@+id/add" 57 android:layout_width="80dp" 58 android:layout_height="70dp" 59 android:layout_alignParentRight="true" 60 android:layout_toRightOf="@id/nine" 61 android:text="@string/add" 62 android:textSize="40sp" /> 63 64 <Button 65 android:id="@+id/four" 66 android:layout_width="80dp" 67 android:layout_height="70dp" 68 android:layout_alignParentLeft="true" 69 android:layout_below="@id/seven" 70 android:text="@string/four" 71 android:textSize="40sp" /> 72 73 <Button 74 android:id="@+id/five" 75 android:layout_width="80dp" 76 android:layout_height="70dp" 77 android:layout_below="@id/eight" 78 android:layout_toRightOf="@id/four" 79 android:text="@string/five" 80 android:textSize="40sp" /> 81 82 <Button 83 android:id="@+id/six" 84 android:layout_width="80dp" 85 android:layout_height="70dp" 86 android:layout_below="@id/nine" 87 android:layout_toRightOf="@id/five" 88 android:text="@string/six" 89 android:textSize="40sp" /> 90 91 <Button 92 android:id="@+id/subtract" 93 android:layout_width="80dp" 94 android:layout_height="70dp" 95 android:layout_alignParentRight="true" 96 android:layout_below="@id/add" 97 android:layout_toRightOf="@id/six" 98 android:text="@string/subtract" 99 android:textSize="40sp" /> 100 101 <Button 102 android:id="@+id/one" 103 android:layout_width="80dp" 104 android:layout_height="70dp" 105 android:layout_alignParentLeft="true" 106 android:layout_below="@id/four" 107 android:text="@string/one" 108 android:textSize="40sp" /> 109 110 <Button 111 android:id="@+id/two" 112 android:layout_width="80dp" 113 android:layout_height="70dp" 114 android:layout_below="@id/five" 115 android:layout_toRightOf="@id/one" 116 android:text="@string/two" 117 android:textSize="40sp" /> 118 119 <Button 120 android:id="@+id/three" 121 android:layout_width="80dp" 122 android:layout_height="70dp" 123 android:layout_below="@id/six" 124 android:layout_toRightOf="@id/two" 125 android:text="@string/three" 126 android:textSize="40sp" /> 127 128 <Button 129 android:id="@+id/multiply" 130 android:layout_width="80dp" 131 android:layout_height="70dp" 132 android:layout_alignParentRight="true" 133 android:layout_below="@id/subtract" 134 android:layout_toRightOf="@id/three" 135 android:text="@string/multiply" 136 android:textSize="40sp" /> 137 138 <Button 139 android:id="@+id/zero" 140 android:layout_width="80dp" 141 android:layout_height="70dp" 142 android:layout_alignParentLeft="true" 143 android:layout_below="@id/one" 144 android:text="@string/zero" 145 android:textSize="40sp" /> 146 147 <Button 148 android:id="@+id/clear" 149 android:layout_width="80dp" 150 android:layout_height="70dp" 151 android:layout_below="@id/two" 152 android:layout_toRightOf="@id/zero" 153 android:text="@string/clear" 154 android:textSize="40sp" /> 155 156 <Button 157 android:id="@+id/result" 158 android:layout_width="80dp" 159 android:layout_height="70dp" 160 android:layout_below="@id/three" 161 android:layout_toRightOf="@id/clear" 162 android:text="@string/result" 163 android:textSize="40sp" /> 164 165 <Button 166 android:id="@+id/divide" 167 android:layout_width="80dp" 168 android:layout_height="70dp" 169 android:layout_alignParentRight="true" 170 android:layout_below="@id/multiply" 171 android:layout_toRightOf="@id/result" 172 android:text="@string/divide" 173 android:textSize="40sp" /> 174 175 <Button 176 android:id="@+id/dot" 177 android:layout_width="80dp" 178 android:layout_height="70dp" 179 android:layout_alignParentLeft="true" 180 android:layout_below="@id/zero" 181 android:text="@string/dot" 182 android:textSize="40sp" /> 183 <Button 184 android:id="@+id/writeButton" 185 android:layout_width="wrap_content" 186 android:layout_height="wrap_content" 187 android:layout_alignParentLeft="true" 188 android:layout_below="@id/dot" 189 android:text="@string/write" 190 android:textSize="40sp" /> 191 <Button 192 android:id="@+id/readButton" 193 android:layout_width="wrap_content" 194 android:layout_height="wrap_content" 195 android:layout_alignParentRight="true" 196 android:layout_below="@id/dot" 197 android:text="@string/read" 198 android:textSize="40sp" /> 199 200 <CheckBox 201 android:id="@+id/appendBox" 202 android:text="@string/appendBox" 203 android:layout_width="wrap_content" 204 android:layout_height="wrap_content" 205 android:layout_alignParentBottom="true" 206 android:layout_toLeftOf="@+id/divide" 207 android:layout_toStartOf="@+id/divide" 208 android:layout_marginBottom="12dp" 209 /> 210 211 </RelativeLayout> 212 213 <EditText 214 215 android:layout_width="match_parent" 216 android:layout_height="wrap_content" 217 android:id="@+id/textView" /> 218 219 <EditText 220 221 android:layout_width="match_parent" 222 android:layout_height="wrap_content" 223 android:id="@+id/displayView" /> 224 225 <EditText 226 android:id="@+id/errorzero" 227 android:layout_width="fill_parent" 228 android:layout_height="wrap_content" 229 android:layout_gravity="center" 230 android:editable="false" 231 android:gravity="center" 232 /> 233 <EditText 234 android:id="@+id/resultText" 235 android:layout_width="fill_parent" 236 android:layout_height="wrap_content" 237 android:layout_gravity="center" 238 android:editable="false" 239 android:gravity="left" 240 android:text="@string/resultText" 241 /> 242 </LinearLayout> 243 </ScrollView>
- 事件监听和实现计算代码(写在MainActivity.java文件里)
1 package com.example.lenovo.calculator; 2 3 4 5 import android.app.Activity; 6 import android.content.Context; 7 import android.os.Bundle; 8 import android.view.View; 9 import android.view.View.OnClickListener; 10 import android.widget.Button; 11 import android.widget.CheckBox; 12 import android.widget.EditText; 13 14 import java.io.FileInputStream; 15 import java.io.FileNotFoundException; 16 import java.io.FileOutputStream; 17 import java.io.IOException; 18 19 public class MainActivity extends Activity { 20 /** 21 * Called when the activity is first created. 22 */ 23 private EditText output = null; 24 private EditText input = null; 25 private Button btn0 = null; 26 private Button btn1 = null; 27 private Button btn2 = null; 28 private Button btn3 = null; 29 private Button btn4 = null; 30 private Button btn5 = null; 31 private Button btn6 = null; 32 private Button btn7 = null; 33 private Button btn8 = null; 34 private Button btn9 = null; 35 private Button btnadd = null; 36 private Button btnsubtract = null; 37 private Button btnmultiply = null; 38 private Button btndivide = null; 39 private Button btnclear = null; 40 private Button btnresult = null; 41 private Button btndot = null; 42 43 private EditText errorzero = null; 44 45 private EditText resultText = null; 46 private Button writeButton = null; 47 private Button readButton = null; 48 private CheckBox appendBox = null; 49 private EditText textView = null; 50 private EditText displayView = null; 51 public String FILE_NAME = "fileDemo.txt"; 52 53 54 private String str = "";//保存数字 55 private String strold = "";//原数字 56 private char act = \' \';//记录“加减乘除等于”符号 57 private int count = 0;//判断要计算的次数,如果超过一个符号,先算出来一部分 58 private Float result = null;//计算的输出结果 59 private Boolean errBoolean = false;//有错误的时候为true,无错为false 60 private Boolean flagBoolean = false;//一个标志,如果为true,可以响应运算消息,如果为false,不响应运算消息,只有前面是数字才可以响应运算消息 61 private Boolean flagDot = false; //小数点标志位 62 63 64 65 @Override 66 public void onCreate(Bundle savedInstanceState) { 67 super.onCreate(savedInstanceState); 68 setContentView(R.layout.activity_main); 69 70 output = (EditText) findViewById(R.id.output); 71 input = (EditText) findViewById(R.id.input); 72 73 errorzero = (EditText) findViewById(R.id.errorzero); 74 resultText = (EditText) findViewById(R.id.resultText); 75 writeButton = (Button) findViewById(R.id.writeButton); 76 readButton = (Button) findViewById(R.id.readButton); 77 textView = (EditText) findViewById(R.id.textView); 78 displayView = (EditText) findViewById(R.id.displayView); 79 appendBox = (CheckBox) findViewById(R.id.appendBox); 80 81 btn0 = (Button) findViewById(R.id.zero); 82 btn1 = (Button) findViewById(R.id.one); 83 btn2 = (Button) findViewById(R.id.two); 84 btn3 = (Button) findViewById(R.id.three); 85 btn4 = (Button) findViewById(R.id.four); 86 btn5 = (Button) findViewById(R.id.five); 87 btn6 = (Button) findViewById(R.id.six); 88 btn7 = (Button) findViewById(R.id.seven); 89 btn8 = (Button) findViewById(R.id.eight); 90 btn9 = (Button) findViewById(R.id.nine); 91 btnadd = (Button) findViewById(R.id.add); 92 btnsubtract = (Button) findViewById(R.id.subtract); 93 btnmultiply = (Button) findViewById(R.id.multiply); 94 btndivide = (Button) findViewById(R.id.divide); 95 btnclear = (Button) findViewById(R.id.clear); 96 btnresult = (Button) findViewById(R.id.result); 97 btndot = (Button) findViewById(R.id.dot); 98 //设置按钮侦听事件 99 btn0.setOnClickListener(listener); 100 btn1.setOnClickListener(listener); 101 btn2.setOnClickListener(listener); 102 btn3.setOnClickListener(listener); 103 btn4.setOnClickListener(listener); 104 btn5.setOnClickListener(listener); 105 btn6.setOnClickListener(listener); 106 btn7.setOnClickListener(listener); 107 btn8.setOnClickListener(listen以上是关于制作一个简易计算器——基于Android Studio实现的主要内容,如果未能解决你的问题,请参考以下文章