当前位置:   article > 正文

Android Studio 简单项目——简易计算器_androidstudio计算机项目源码(简易计算器app含源码+文档和ppt)

androidstudio计算机项目源码(简易计算器app含源码+文档和ppt)

Android Studio 简单项目——简易计算器

目录

Android Studio 简单项目——简易计算器

一、前言

二、设计界面

三、主要代码

四、完整资源


一、前言

软件:Android Studio

需求:学习Android Studio,做一个简易计算器深入学习

(附:刚开始学习,部分代码参考,在此基础上增删改)

主要功能实现:

1.简单计算器加减乘除

2.错误提示

3.追加模式

4.写入读取

二、设计界面

1.主界面

2.功能解释

①第一行输入数据

第二行为输出结果

②为错误提示行

如果除数为0则算式无意义,输出错误。如下图

③计算器键盘

CE 为清零 , BA 为清除

①写入读取提示

② 读取成功,会出现读取算是

③计算表达式

三、主要代码

1.MainActivity

  1. package com.example.administrator.helloworld;
  2. import android.os.Bundle;
  3. import android.support.design.widget.FloatingActionButton;
  4. import android.support.design.widget.Snackbar;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.support.v7.widget.Toolbar;
  7. import android.view.View;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.app.Activity;
  11. import android.content.Context;
  12. import android.os.Bundle;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.CheckBox;
  17. import android.widget.EditText;
  18. import java.io.FileInputStream;
  19. import java.io.FileNotFoundException;
  20. import java.io.FileOutputStream;
  21. import java.io.IOException;
  22. public class MainActivity extends Activity {
  23. /**
  24. * Called when the activity is first created.
  25. */
  26. private EditText output = null;
  27. private EditText input = null;
  28. private Button btn0 = null;
  29. private Button btn1 = null;
  30. private Button btn2 = null;
  31. private Button btn3 = null;
  32. private Button btn4 = null;
  33. private Button btn5 = null;
  34. private Button btn6 = null;
  35. private Button btn7 = null;
  36. private Button btn8 = null;
  37. private Button btn9 = null;
  38. private Button btnadd = null;
  39. private Button btnsubtract = null;
  40. private Button btnmultiply = null;
  41. private Button btndivide = null;
  42. private Button btnclear = null;
  43. private Button btnresult = null;
  44. private Button btndot = null;
  45. private Button btnback = null;
  46. private EditText errorzero = null;
  47. private EditText resultText = null;
  48. private Button writeButton = null;
  49. private Button readButton = null;
  50. private CheckBox appendBox = null;
  51. private EditText textView = null;
  52. private EditText displayView = null;
  53. public String FILE_NAME = "fileDemo.txt";
  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. @Override
  63. public void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.activity_main);
  66. output = (EditText) findViewById(R.id.output);
  67. input = (EditText) findViewById(R.id.input);
  68. errorzero = (EditText) findViewById(R.id.errorzero);
  69. resultText = (EditText) findViewById(R.id.resultText);
  70. writeButton = (Button) findViewById(R.id.writeButton);
  71. readButton = (Button) findViewById(R.id.readButton);
  72. textView = (EditText) findViewById(R.id.textView);
  73. displayView = (EditText) findViewById(R.id.displayView);
  74. appendBox = (CheckBox) findViewById(R.id.appendBox);
  75. btn0 = (Button) findViewById(R.id.zero);
  76. btn1 = (Button) findViewById(R.id.one);
  77. btn2 = (Button) findViewById(R.id.two);
  78. btn3 = (Button) findViewById(R.id.three);
  79. btn4 = (Button) findViewById(R.id.four);
  80. btn5 = (Button) findViewById(R.id.five);
  81. btn6 = (Button) findViewById(R.id.six);
  82. btn7 = (Button) findViewById(R.id.seven);
  83. btn8 = (Button) findViewById(R.id.eight);
  84. btn9 = (Button) findViewById(R.id.nine);
  85. btnadd = (Button) findViewById(R.id.add);
  86. btnsubtract = (Button) findViewById(R.id.subtract);
  87. btnmultiply = (Button) findViewById(R.id.multiply);
  88. btndivide = (Button) findViewById(R.id.divide);
  89. btnclear = (Button) findViewById(R.id.clear);
  90. btnback = (Button) findViewById(R.id.back);
  91. btnresult = (Button) findViewById(R.id.result);
  92. btndot = (Button) findViewById(R.id.dot);
  93. //设置按钮侦听事件
  94. btn0.setOnClickListener(listener);
  95. btn1.setOnClickListener(listener);
  96. btn2.setOnClickListener(listener);
  97. btn3.setOnClickListener(listener);
  98. btn4.setOnClickListener(listener);
  99. btn5.setOnClickListener(listener);
  100. btn6.setOnClickListener(listener);
  101. btn7.setOnClickListener(listener);
  102. btn8.setOnClickListener(listener);
  103. btn9.setOnClickListener(listener);
  104. //执行运算
  105. btnadd.setOnClickListener(listener);
  106. btnsubtract.setOnClickListener(listener);
  107. btnmultiply.setOnClickListener(listener);
  108. btndivide.setOnClickListener(listener);
  109. btnclear.setOnClickListener(listener);
  110. btnresult.setOnClickListener(listener);
  111. btnback.setOnClickListener(listener);
  112. btndot.setOnClickListener(listener);
  113. writeButton.setOnClickListener(writelistener);
  114. readButton.setOnClickListener(readlistener);
  115. // ATTENTION: This was auto-generated to implement the App Indexing API.
  116. // See https://g.co/AppIndexing/AndroidStudio for more information.
  117. }
  118. private OnClickListener listener = new OnClickListener() {
  119. public void onClick(View v) {
  120. // TODO Auto-generated method stub
  121. switch (v.getId()) {
  122. //输入数字
  123. case R.id.zero:
  124. num(0);
  125. break;
  126. case R.id.one:
  127. num(1);
  128. break;
  129. case R.id.two:
  130. num(2);
  131. break;
  132. case R.id.three:
  133. num(3);
  134. break;
  135. case R.id.four:
  136. num(4);
  137. break;
  138. case R.id.five:
  139. num(5);
  140. break;
  141. case R.id.six:
  142. num(6);
  143. break;
  144. case R.id.seven:
  145. num(7);
  146. break;
  147. case R.id.eight:
  148. num(8);
  149. break;
  150. case R.id.nine:
  151. num(9);
  152. break;
  153. case R.id.dot:
  154. dot();
  155. break;
  156. //执行运算
  157. case R.id.add:
  158. add();
  159. break;
  160. case R.id.subtract:
  161. sub();
  162. break;
  163. case R.id.multiply:
  164. multiply();
  165. break;
  166. case R.id.divide:
  167. divide();
  168. break;
  169. case R.id.clear:
  170. clear();
  171. break;
  172. case R.id.back:
  173. back();
  174. break;
  175. //计算结果
  176. case R.id.result:
  177. result();
  178. if (!errBoolean && flagBoolean) {
  179. output.setText(String.valueOf(result));
  180. }
  181. resultText.setText(strold + act + str + "=" + result+" ");
  182. break;
  183. default:
  184. break;
  185. }
  186. input.setText(strold + act + str);
  187. output.setText(String.valueOf(result));
  188. }
  189. };
  190. private OnClickListener writelistener = new OnClickListener() {
  191. @Override
  192. public void onClick(View view) {
  193. //textView.setText("");
  194. FileOutputStream fos = null;
  195. try {
  196. if (appendBox.isChecked()) {
  197. fos = openFileOutput(FILE_NAME, Context.MODE_APPEND);
  198. } else {
  199. fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
  200. }
  201. String text = resultText.getText().toString();
  202. fos.write(text.getBytes());
  203. textView.setText("文件写入成功,写入长度:" + text.length());
  204. } catch (FileNotFoundException e) {
  205. e.printStackTrace();
  206. } catch (IOException e) {
  207. e.printStackTrace();
  208. }
  209. finally {
  210. if (fos != null)
  211. try {
  212. fos.flush();
  213. fos.close();
  214. } catch (IOException e) {
  215. e.printStackTrace();
  216. }
  217. }
  218. }
  219. };
  220. private OnClickListener readlistener = new OnClickListener() {
  221. @Override
  222. public void onClick(View view) {
  223. displayView.setText("");
  224. FileInputStream fis = null;
  225. try {
  226. fis = openFileInput(FILE_NAME);
  227. if (fis.available() == 0) {
  228. return;
  229. }
  230. byte[] readBytes = new byte[fis.available()];
  231. while (fis.read(readBytes) != -1) {
  232. }
  233. String text = new String(readBytes);
  234. displayView.setText(text);
  235. textView.setText("文件读取成功,写入长度:" + text.length());
  236. } catch (FileNotFoundException e) {
  237. e.printStackTrace();
  238. } catch (IOException e) {
  239. e.printStackTrace();
  240. }
  241. }
  242. };
  243. private void dot() {
  244. // TODO Auto-generated method stub
  245. if (!flagDot) {
  246. str = str + ".";
  247. flagBoolean = false;
  248. flagDot = true;
  249. }
  250. }
  251. private void back() {
  252. str = strold = "";
  253. count = 0;
  254. act = ' ';
  255. result = null;
  256. flagBoolean = false;
  257. flagDot = false;
  258. resultText.setText("");
  259. input.setText(strold + act + str);
  260. /* if(str==null){
  261. str="";
  262. input.setText("");
  263. }
  264. else if(str!=null&&!str.equals("")){
  265. input.setText(str.substring(0,str.length()-1));
  266. }
  267. */
  268. }
  269. private void clear() {
  270. // TODO Auto-generated method stub
  271. str = strold = "";
  272. count = 0;
  273. act = ' ';
  274. result = null;
  275. flagBoolean = false;
  276. flagDot = false;
  277. input.setText(strold + act + str);
  278. output.setText("");
  279. errorzero.setText("");
  280. displayView.setText("");
  281. textView.setText("");
  282. resultText.setText("");
  283. }
  284. private void divide() {
  285. // TODO Auto-generated method stub
  286. if (flagBoolean) {
  287. check();
  288. act = '/';
  289. flagBoolean = false;
  290. }
  291. }
  292. private void multiply() {
  293. // TODO Auto-generated method stub
  294. if (flagBoolean) {
  295. check();
  296. act = '*';
  297. flagBoolean = false;
  298. }
  299. }
  300. private void sub() {
  301. // TODO Auto-generated method stub
  302. if (flagBoolean) {
  303. check();
  304. act = '-';
  305. flagBoolean = false;
  306. }
  307. }
  308. private void add() {
  309. // TODO Auto-generated method stub
  310. if (flagBoolean) {
  311. check();
  312. act = '+';
  313. flagBoolean = false;
  314. }
  315. }
  316. private void check() {
  317. // TODO Auto-generated method stub
  318. if (count >= 1) {
  319. result();
  320. str = String.valueOf(result);
  321. }
  322. strold = str;
  323. str = "";
  324. count++;
  325. flagDot = false;
  326. errorzero.setText("");
  327. }
  328. //计算输出结果
  329. private void result() {
  330. // TODO Auto-generated method stub
  331. if (flagBoolean) {
  332. Float a, b;
  333. a = Float.parseFloat(strold);
  334. b = Float.parseFloat(str);
  335. if (b == 0 && act == '/') {
  336. clear();
  337. errorzero.setText("除数不能为零!");
  338. //errBoolean=true;
  339. }
  340. if (!errBoolean) {
  341. switch (act) {
  342. case '+':
  343. result = a + b;
  344. break;
  345. case '-':
  346. result = a - b;
  347. break;
  348. case '*':
  349. result = a * b;
  350. break;
  351. case '/':
  352. result = a / b;
  353. break;
  354. default:
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. private void num(int i) {
  361. // TODO Auto-generated method stub
  362. str = str + String.valueOf(i);
  363. flagBoolean = true;
  364. errorzero.setText("");
  365. }
  366. }

2.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. <EditText
  18. android:id="@+id/output"
  19. android:layout_width="fill_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_gravity="center"
  22. android:editable="true"
  23. android:gravity="right"
  24. android:hint="@string/shuchu" />
  25. <EditText
  26. android:id="@+id/errorzero"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content"
  29. android:layout_gravity="center"
  30. android:editable="false"
  31. android:gravity="center" />
  32. <RelativeLayout
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content">
  35. <Button
  36. android:id="@+id/seven"
  37. android:layout_width="80dp"
  38. android:layout_height="70dp"
  39. android:layout_alignParentLeft="true"
  40. android:text="@string/seven"
  41. android:textSize="40sp" />
  42. <Button
  43. android:id="@+id/eight"
  44. android:layout_width="80dp"
  45. android:layout_height="70dp"
  46. android:layout_toRightOf="@id/seven"
  47. android:text="@string/eight"
  48. android:textSize="40sp" />
  49. <Button
  50. android:id="@+id/nine"
  51. android:layout_width="80dp"
  52. android:layout_height="70dp"
  53. android:layout_toRightOf="@id/eight"
  54. android:text="@string/nine"
  55. android:textSize="40sp" />
  56. <Button
  57. android:id="@+id/add"
  58. android:layout_width="80dp"
  59. android:layout_height="70dp"
  60. android:layout_alignParentRight="true"
  61. android:layout_toRightOf="@id/nine"
  62. android:background="#FF9800"
  63. android:text="@string/add"
  64. android:textSize="40sp" />
  65. <Button
  66. android:id="@+id/four"
  67. android:layout_width="80dp"
  68. android:layout_height="70dp"
  69. android:layout_below="@id/seven"
  70. android:layout_alignParentLeft="true"
  71. android:text="@string/four"
  72. android:textSize="40sp" />
  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. <Button
  82. android:id="@+id/six"
  83. android:layout_width="80dp"
  84. android:layout_height="70dp"
  85. android:layout_below="@id/nine"
  86. android:layout_toRightOf="@id/five"
  87. android:text="@string/six"
  88. android:textSize="40sp" />
  89. <Button
  90. android:id="@+id/subtract"
  91. android:layout_width="80dp"
  92. android:layout_height="70dp"
  93. android:layout_below="@id/add"
  94. android:layout_alignParentRight="true"
  95. android:layout_toRightOf="@id/six"
  96. android:background="#FF9800"
  97. android:text="@string/subtract"
  98. android:textSize="40sp" />
  99. <Button
  100. android:id="@+id/one"
  101. android:layout_width="80dp"
  102. android:layout_height="70dp"
  103. android:layout_below="@id/four"
  104. android:layout_alignParentLeft="true"
  105. android:text="@string/one"
  106. android:textSize="40sp" />
  107. <Button
  108. android:id="@+id/two"
  109. android:layout_width="80dp"
  110. android:layout_height="70dp"
  111. android:layout_below="@id/five"
  112. android:layout_toRightOf="@id/one"
  113. android:text="@string/two"
  114. android:textSize="40sp" />
  115. <Button
  116. android:id="@+id/three"
  117. android:layout_width="80dp"
  118. android:layout_height="70dp"
  119. android:layout_below="@id/six"
  120. android:layout_toRightOf="@id/two"
  121. android:text="@string/three"
  122. android:textSize="40sp" />
  123. <Button
  124. android:id="@+id/multiply"
  125. android:layout_width="80dp"
  126. android:layout_height="70dp"
  127. android:layout_below="@id/subtract"
  128. android:layout_alignParentRight="true"
  129. android:layout_toRightOf="@id/three"
  130. android:background="#FF9800"
  131. android:text="@string/multiply"
  132. android:textSize="40sp" />
  133. <Button
  134. android:id="@+id/zero"
  135. android:layout_width="240dp"
  136. android:layout_height="70dp"
  137. android:layout_below="@id/three"
  138. android:layout_alignParentLeft="true"
  139. android:text="@string/zero"
  140. android:textSize="40sp" />
  141. <Button
  142. android:id="@+id/clear"
  143. android:layout_width="80dp"
  144. android:layout_height="70dp"
  145. android:layout_below="@id/zero"
  146. android:layout_alignParentLeft="true"
  147. android:text="@string/clear"
  148. android:textSize="40sp" />
  149. <Button
  150. android:id="@+id/back"
  151. android:layout_width="80dp"
  152. android:layout_height="70dp"
  153. android:layout_toRightOf="@id/clear"
  154. android:layout_below="@id/zero"
  155. android:text="@string/back"
  156. android:textSize="40sp" />
  157. <Button
  158. android:id="@+id/result"
  159. android:layout_width="80dp"
  160. android:layout_height="70dp"
  161. android:layout_below="@id/divide"
  162. android:layout_alignParentRight="true"
  163. android:layout_toRightOf="@id/dot"
  164. android:background="#FFC107"
  165. android:text="@string/result"
  166. android:textSize="40sp" />
  167. <Button
  168. android:id="@+id/divide"
  169. android:layout_width="80dp"
  170. android:layout_height="70dp"
  171. android:layout_below="@id/multiply"
  172. android:layout_alignParentRight="true"
  173. android:layout_toRightOf="@id/zero"
  174. android:background="#FF9800"
  175. android:text="@string/divide"
  176. android:textSize="40sp" />
  177. <Button
  178. android:id="@+id/dot"
  179. android:layout_width="80dp"
  180. android:layout_height="70dp"
  181. android:layout_below="@id/zero"
  182. android:layout_toRightOf="@id/back"
  183. android:text="@string/dot"
  184. android:textSize="40sp" />
  185. <Button
  186. android:id="@+id/writeButton"
  187. android:layout_width="160dp"
  188. android:layout_height="70dp"
  189. android:layout_below="@id/result"
  190. android:layout_alignParentLeft="true"
  191. android:text="@string/write"
  192. android:textSize="40sp" />
  193. <Button
  194. android:id="@+id/readButton"
  195. android:layout_width="170dp"
  196. android:layout_height="70dp"
  197. android:layout_below="@id/result"
  198. android:layout_alignParentRight="true"
  199. android:text="@string/read"
  200. android:textSize="40sp" />
  201. <CheckBox
  202. android:id="@+id/appendBox"
  203. android:layout_width="80dp"
  204. android:layout_height="70dp"
  205. android:layout_below="@id/dot"
  206. android:layout_toRightOf="@id/writeButton"
  207. android:layout_alignParentBottom="true"
  208. android:text="@string/appendBox" />
  209. </RelativeLayout>
  210. <EditText
  211. android:layout_width="match_parent"
  212. android:layout_height="wrap_content"
  213. android:id="@+id/textView" />
  214. <EditText
  215. android:id="@+id/displayView"
  216. android:layout_width="match_parent"
  217. android:layout_height="44dp" />
  218. <EditText
  219. android:id="@+id/resultText"
  220. android:layout_width="fill_parent"
  221. android:layout_height="45dp"
  222. android:layout_gravity="center"
  223. android:editable="false"
  224. android:gravity="left"
  225. android:text="@string/resultText" />
  226. </LinearLayout>
  227. </ScrollView>

四、完整资源

https://download.csdn.net/download/qi2456/11203862

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/325279?site
推荐阅读
相关标签
  

闽ICP备14008679号