当前位置:   article > 正文

Android studio 简易计算器App的实现及实现加减乘除功能(附完整源码)_安卓加减乘除源码

安卓加减乘除源码

        在Android studio 实现简易计算器App并实现加减乘除功能

结果

activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:paddingBottom="10dp"
  7. android:paddingLeft="10dp"
  8. android:paddingRight="10dp"
  9. android:paddingTop="10dp">
  10. <EditText
  11. android:id="@+id/et_result"
  12. android:hint="请输入数字"
  13. android:layout_width="match_parent"
  14. android:layout_height="100dp"
  15. android:layout_gravity="left"
  16. android:gravity="right"
  17. android:textSize="50sp" />
  18. <GridLayout
  19. android:layout_width="match_parent"
  20. android:layout_height="match_parent"
  21. android:background="@drawable/sz"
  22. android:layout_gravity="center"
  23. android:orientation="horizontal"
  24. android:columnCount="4"
  25. android:rowCount="5">
  26. <Button
  27. android:id="@+id/btn_clear"
  28. android:layout_columnSpan="3"
  29. android:layout_gravity="fill_horizontal"
  30. android:text="C DEL"
  31. android:layout_rowWeight="1"
  32. android:textSize="26sp" />
  33. <Button
  34. android:id="@+id/btn_div"
  35. android:text="÷"
  36. android:layout_rowWeight="1"
  37. android:textSize="26sp" />
  38. <Button
  39. android:id="@+id/btn_1"
  40. android:text="1"
  41. android:layout_rowWeight="1"
  42. android:textSize="26sp"/>
  43. <Button
  44. android:id="@+id/btn_2"
  45. android:text="2"
  46. android:layout_rowWeight="1"
  47. android:textSize="26sp" />
  48. <Button
  49. android:id="@+id/btn_3"
  50. android:text="3"
  51. android:layout_rowWeight="1"
  52. android:textSize="26sp" />
  53. <Button
  54. android:id="@+id/btn_add"
  55. android:text="+"
  56. android:layout_rowWeight="1"
  57. android:textSize="26sp" />
  58. <Button
  59. android:id="@+id/btn_4"
  60. android:text="4"
  61. android:layout_rowWeight="1"
  62. android:textSize="26sp" />
  63. <Button
  64. android:id="@+id/btn_5"
  65. android:text="5"
  66. android:layout_rowWeight="1"
  67. android:textSize="26sp" />
  68. <Button
  69. android:id="@+id/btn_6"
  70. android:text="6"
  71. android:layout_rowWeight="1"
  72. android:textSize="26sp" />
  73. <Button
  74. android:id="@+id/btn_sub"
  75. android:text="-"
  76. android:layout_rowWeight="1"
  77. android:textSize="26sp" />
  78. <Button
  79. android:id="@+id/btn_7"
  80. android:text="7"
  81. android:layout_rowWeight="1"
  82. android:textSize="26sp" />
  83. <Button
  84. android:id="@+id/btn_8"
  85. android:text="8"
  86. android:layout_rowWeight="1"
  87. android:textSize="26sp" />
  88. <Button
  89. android:id="@+id/btn_9"
  90. android:text="9"
  91. android:layout_rowWeight="1"
  92. android:textSize="26sp" />
  93. <Button
  94. android:id="@+id/btn_mul"
  95. android:text="×"
  96. android:layout_rowWeight="1"
  97. android:textSize="26sp" />
  98. <Button
  99. android:id="@+id/btn_0"
  100. android:text="0"
  101. android:layout_rowWeight="1"
  102. android:textSize="26sp" />
  103. <Button
  104. android:id="@+id/btn_dot"
  105. android:text="."
  106. android:layout_rowWeight="1"
  107. android:textSize="26sp" />
  108. <Button
  109. android:id="@+id/btn_equ"
  110. android:text="="
  111. android:layout_rowWeight="1"
  112. android:layout_columnSpan="2"
  113. android:layout_gravity="fill_horizontal"
  114. android:textSize="26sp" />
  115. </GridLayout>
  116. </LinearLayout>

MainActivity.java

  1. package com.example.a86189.szcounter;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Button;
  6. import android.widget.EditText;
  7. public class MainActivity extends AppCompatActivity {
  8. Button btn1; // 数字1
  9. Button btn2; // 数字2
  10. Button btn3; // 数字3
  11. Button btn4; // 数字4
  12. Button btn5; // 数字5
  13. Button btn6; // 数字6
  14. Button btn7; // 数字7
  15. Button btn8; // 数字8
  16. Button btn9; // 数字9
  17. Button btn0; // 数字0
  18. Button add; // +号
  19. Button sub; // -号
  20. Button mul; // *号
  21. Button div; // 除号
  22. Button dot; // 小数点
  23. Button equ; // =号
  24. Button clear; //清除
  25. EditText result; // 显示文本
  26. double num1 = 0, num2 = 0; //计算的数字
  27. double Result = 0;//计算结果
  28. int op = 0;//判断操作符
  29. String opd = " ";//显示操作符
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. // 获取页面上的控件
  35. btn1 = (Button) findViewById(R.id.btn_1);
  36. btn2 = (Button)findViewById(R.id.btn_2);
  37. btn3 = (Button)findViewById(R.id.btn_3);
  38. btn4 = (Button)findViewById(R.id.btn_4);
  39. btn5 = (Button)findViewById(R.id.btn_5);
  40. btn6 = (Button)findViewById(R.id.btn_6);
  41. btn7 = (Button)findViewById(R.id.btn_7);
  42. btn8 = (Button)findViewById(R.id.btn_8);
  43. btn9 = (Button)findViewById(R.id.btn_9);
  44. btn0 = (Button)findViewById(R.id.btn_0);
  45. add = (Button)findViewById(R.id.btn_add);
  46. sub = (Button)findViewById(R.id.btn_sub);
  47. mul = (Button)findViewById(R.id.btn_mul);
  48. div = (Button)findViewById(R.id.btn_div);
  49. equ = (Button)findViewById(R.id.btn_equ);
  50. dot = (Button)findViewById(R.id.btn_dot);
  51. clear =(Button) findViewById(R.id.btn_clear);
  52. result =(EditText) findViewById(R.id.et_result);
  53. // 按钮的单击事件
  54. btn1.setOnClickListener(new Click());
  55. btn2.setOnClickListener(new Click());
  56. btn3.setOnClickListener(new Click());
  57. btn4.setOnClickListener(new Click());
  58. btn5.setOnClickListener(new Click());
  59. btn6.setOnClickListener(new Click());
  60. btn7.setOnClickListener(new Click());
  61. btn8.setOnClickListener(new Click());
  62. btn9.setOnClickListener(new Click());
  63. btn0.setOnClickListener(new Click());
  64. add.setOnClickListener(new Click());
  65. sub.setOnClickListener(new Click());
  66. mul.setOnClickListener(new Click());
  67. div.setOnClickListener(new Click());
  68. equ.setOnClickListener(new Click());
  69. dot.setOnClickListener(new Click());
  70. clear.setOnClickListener(new Click());
  71. result.setOnClickListener(new Click());
  72. }
  73. // 设置按钮点击后的监听
  74. class Click implements View.OnClickListener {
  75. public void onClick(View v) {
  76. switch (v.getId()) { //switch循环获取点击按钮后的值
  77. case R.id.btn_0: //获取,0-9、小数点,并在编辑框显示
  78. String myString = result.getText().toString();
  79. myString += "0";
  80. result.setText(myString);
  81. break;
  82. case R.id.btn_1:
  83. String myString1 = result.getText().toString();
  84. myString1 += "1";
  85. result.setText(myString1);
  86. break;
  87. case R.id.btn_2:
  88. String myString2 = result.getText().toString();
  89. myString2 += "2";
  90. result.setText(myString2);
  91. break;
  92. case R.id.btn_3:
  93. String myString3 = result.getText().toString();
  94. myString3 += "3";
  95. result.setText(myString3);
  96. break;
  97. case R.id.btn_4:
  98. String myString4 = result.getText().toString();
  99. myString4 += "4";
  100. result.setText(myString4);
  101. break;
  102. case R.id.btn_5:
  103. String myString5 = result.getText().toString();
  104. myString5 += "5";
  105. result.setText(myString5);
  106. break;
  107. case R.id.btn_6:
  108. String myString6 = result.getText().toString();
  109. myString6 += "6";
  110. result.setText(myString6);
  111. break;
  112. case R.id.btn_7:
  113. String myString7 = result.getText().toString();
  114. myString7 += "7";
  115. result.setText(myString7);
  116. break;
  117. case R.id.btn_8:
  118. String myString8 = result.getText().toString();
  119. myString8 += "8";
  120. result.setText(myString8);
  121. break;
  122. case R.id.btn_9:
  123. String myString9 = result.getText().toString();
  124. myString9 += "9";
  125. result.setText(myString9);
  126. break;
  127. case R.id.btn_dot:
  128. String myStringDot = result.getText().toString();
  129. myStringDot += ".";
  130. result.setText(myStringDot);
  131. break;
  132. case R.id.btn_add: //判断,使用加减乘除的操作符
  133. String myStringAdd = result.getText().toString();
  134. if (myStringAdd.equals(null)) {
  135. return;
  136. }
  137. num1 = Double.valueOf(myStringAdd);
  138. result.setText(null);
  139. op = 1;
  140. opd = "+";
  141. break;
  142. case R.id.btn_sub:
  143. String myStringSub = result.getText().toString();
  144. if (myStringSub.equals(null)) {
  145. return;
  146. }
  147. num1 = Double.valueOf(myStringSub);
  148. result.setText(null);
  149. op = 2;
  150. opd = "-";
  151. break;
  152. case R.id.btn_mul:
  153. String myStringMul = result.getText().toString();
  154. if (myStringMul.equals(null)) {
  155. return;
  156. }
  157. num1 = Double.valueOf(myStringMul);
  158. result.setText(null);
  159. op = 3;
  160. opd = "×";
  161. break;
  162. case R.id.btn_div:
  163. String myStringDiv = result.getText().toString();
  164. if (myStringDiv.equals(null)) {
  165. return;
  166. }
  167. num1 = Double.valueOf(myStringDiv);
  168. result.setText(null);
  169. op = 4;
  170. opd = "÷";
  171. break;
  172. case R.id.btn_clear: //清除,将编辑框文本显示为空
  173. result.setText(null);
  174. break;
  175. case R.id.btn_equ: //计算,以操作符为判断,选择所需的运算,并将结果输出
  176. String myStringEqu = result.getText().toString();
  177. if (myStringEqu.equals(null)) {
  178. return;
  179. }
  180. num2 = Double.valueOf(myStringEqu);
  181. result.setText(null);
  182. switch (op) {
  183. case 0:
  184. Result = num2;
  185. break;
  186. case 1:
  187. Result = num1 + num2;
  188. break;
  189. case 2:
  190. Result = num1 - num2;
  191. break;
  192. case 3:
  193. Result = num1 * num2;
  194. break;
  195. case 4:
  196. if (num2 == 0) //除法中分子与分母之分
  197. Result = 0;
  198. else
  199. Result = num1 / num2;
  200. break;
  201. default:
  202. Result = 0;
  203. break;
  204. }
  205. result.setText(Double.toString(num1) + opd + Double.toString(num2) + "=" + Double.toString(Result)); //将结果完整输出
  206. break;
  207. default:
  208. break;
  209. }
  210. }
  211. }
  212. }

⭐⭐⭐持续更新.....

⭐⭐⭐每天提醒自己,自己就是个菜鸡!

⭐⭐⭐如果对您有帮助留下的每一个点赞、收藏、关注是对菜鸡创作的最大鼓励❀

⭐⭐⭐有相关问题可以写在评论区,一起学习,一起进步。

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