当前位置:   article > 正文

AndroidStudio设计一个计算器_android studio加减乘除计算器代码

android studio加减乘除计算器代码

界面设计

 

activity_calcuator.xml 设计:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="#EEEEEE"
  8. android:orientation ="vertical"
  9. android:padding="5dp">
  10. <ScrollView
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content">
  13. <LinearLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="wrap_content"
  16. android:orientation="vertical">
  17. <TextView
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. android:gravity="center"
  21. android:text="@string/simple_calculator"
  22. android:textColor="@color/black"
  23. android:textSize="20sp" />
  24. <TextView
  25. android:id="@+id/tv_result"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:background="@color/white"
  29. android:gravity="right|bottom"
  30. android:lines="3"
  31. android:text="0"
  32. android:textColor="@color/black"
  33. android:textSize="25sp" />
  34. <GridLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content"
  37. android:columnCount="4"
  38. android:rowCount="5">
  39. <Button
  40. android:id="@+id/btn_cancel"
  41. android:layout_width="0dp"
  42. android:layout_height="75dp"
  43. android:layout_columnWeight="1"
  44. android:gravity="center"
  45. android:text="@string/cancel"
  46. android:textColor="@color/black"
  47. android:textSize="@dimen/button_font_size" />
  48. <Button
  49. android:id="@+id/btn_divide"
  50. android:layout_width="0dp"
  51. android:layout_height="75dp"
  52. android:layout_columnWeight="1"
  53. android:gravity="center"
  54. android:text="➗"
  55. android:textColor="@color/black"
  56. android:textSize="30sp" />
  57. <Button
  58. android:id="@+id/btn_multiply"
  59. android:layout_width="0dp"
  60. android:layout_height="75dp"
  61. android:layout_columnWeight="1"
  62. android:gravity="center"
  63. android:text="x"
  64. android:textColor="@color/black"
  65. android:textSize="30sp" />
  66. <Button
  67. android:id="@+id/btn_clear"
  68. android:layout_width="0dp"
  69. android:layout_height="75dp"
  70. android:layout_columnWeight="1"
  71. android:gravity="center"
  72. android:text="C"
  73. android:textColor="@color/black"
  74. android:textSize="30sp" />
  75. <Button
  76. android:id="@+id/btn_seven"
  77. android:layout_width="0dp"
  78. android:layout_height="75dp"
  79. android:layout_columnWeight="1"
  80. android:gravity="center"
  81. android:text="7"
  82. android:textColor="@color/black"
  83. android:textSize="30sp" />
  84. <Button
  85. android:id="@+id/btn_eight"
  86. android:layout_width="0dp"
  87. android:layout_height="75dp"
  88. android:layout_columnWeight="1"
  89. android:gravity="center"
  90. android:text="8"
  91. android:textColor="@color/black"
  92. android:textSize="30sp" />
  93. <Button
  94. android:id="@+id/btn_nine"
  95. android:layout_width="0dp"
  96. android:layout_height="75dp"
  97. android:layout_columnWeight="1"
  98. android:gravity="center"
  99. android:text="9"
  100. android:textColor="@color/black"
  101. android:textSize="30sp" />
  102. <Button
  103. android:id="@+id/btn_plus"
  104. android:layout_width="0dp"
  105. android:layout_height="75dp"
  106. android:layout_columnWeight="1"
  107. android:gravity="center"
  108. android:text="+"
  109. android:textColor="@color/black"
  110. android:textSize="30sp" />
  111. <Button
  112. android:id="@+id/btn_four"
  113. android:layout_width="0dp"
  114. android:layout_height="75dp"
  115. android:layout_columnWeight="1"
  116. android:gravity="center"
  117. android:text="4"
  118. android:textColor="@color/black"
  119. android:textSize="30sp" />
  120. <Button
  121. android:id="@+id/btn_five"
  122. android:layout_width="0dp"
  123. android:layout_height="75dp"
  124. android:layout_columnWeight="1"
  125. android:gravity="center"
  126. android:text="5"
  127. android:textColor="@color/black"
  128. android:textSize="30sp" />
  129. <Button
  130. android:id="@+id/btn_six"
  131. android:layout_width="0dp"
  132. android:layout_height="75dp"
  133. android:layout_columnWeight="1"
  134. android:gravity="center"
  135. android:text="6"
  136. android:textColor="@color/black"
  137. android:textSize="30sp" />
  138. <Button
  139. android:id="@+id/btn_minus"
  140. android:layout_width="0dp"
  141. android:layout_height="75dp"
  142. android:layout_columnWeight="1"
  143. android:gravity="center"
  144. android:text="-"
  145. android:textColor="@color/black"
  146. android:textSize="30sp" />
  147. <Button
  148. android:id="@+id/btn_one"
  149. android:layout_width="0dp"
  150. android:layout_height="75dp"
  151. android:layout_columnWeight="1"
  152. android:gravity="center"
  153. android:text="1"
  154. android:textColor="@color/black"
  155. android:textSize="30sp" />
  156. <Button
  157. android:id="@+id/btn_two"
  158. android:layout_width="0dp"
  159. android:layout_height="75dp"
  160. android:layout_columnWeight="1"
  161. android:gravity="center"
  162. android:text="2"
  163. android:textColor="@color/black"
  164. android:textSize="30sp" />
  165. <Button
  166. android:id="@+id/btn_three"
  167. android:layout_width="0dp"
  168. android:layout_height="75dp"
  169. android:layout_columnWeight="1"
  170. android:gravity="center"
  171. android:text="3"
  172. android:textColor="@color/black"
  173. android:textSize="30sp" />
  174. <ImageButton
  175. android:id="@+id/ib_sqrt"
  176. android:layout_width="0dp"
  177. android:layout_height="@dimen/button_height"
  178. android:layout_columnWeight="1"
  179. android:scaleType="centerInside"
  180. android:src="@drawable/sqtr" />
  181. <Button
  182. android:id="@+id/btn_reciprocal"
  183. android:layout_width="0dp"
  184. android:layout_height="@dimen/button_height"
  185. android:layout_columnWeight="1"
  186. android:gravity="center"
  187. android:text="@string/reciprocal"
  188. android:textColor="@color/black"
  189. android:textSize="@dimen/button_font_size" />
  190. <Button
  191. android:id="@+id/btn_zero"
  192. android:layout_width="0dp"
  193. android:layout_height="@dimen/button_height"
  194. android:layout_columnWeight="1"
  195. android:gravity="center"
  196. android:text="@string/zero"
  197. android:textColor="@color/black"
  198. android:textSize="@dimen/button_font_size" />
  199. <Button
  200. android:id="@+id/btn_dot"
  201. android:layout_width="0dp"
  202. android:layout_height="@dimen/button_height"
  203. android:layout_columnWeight="1"
  204. android:gravity="center"
  205. android:text="@string/dot"
  206. android:textColor="@color/black"
  207. android:textSize="@dimen/button_font_size" />
  208. <Button
  209. android:id="@+id/btn_equal"
  210. android:layout_width="0dp"
  211. android:layout_height="@dimen/button_height"
  212. android:layout_columnWeight="1"
  213. android:gravity="center"
  214. android:text="@string/equal"
  215. android:textColor="@color/black"
  216. android:textSize="@dimen/button_font_size" />
  217. </GridLayout>
  218. </LinearLayout>
  219. </ScrollView>
  220. </LinearLayout>

 

dimens.xml

  1. <resources>
  2. <dimen name="fab_margin">16dp</dimen>
  3. <dimen name="button_font_size">30sp</dimen>
  4. <dimen name="button_height">75dp</dimen>
  5. </resources>

 strings.xml

  1. <resources>
  2. <string name="app_name">chapter03</string>
  3. <string name="action_settings">Settings</string>
  4. <!-- Strings used for fragments for navigation -->
  5. <string name="first_fragment_label">First Fragment</string>
  6. <string name="second_fragment_label">Second Fragment</string>
  7. <string name="next">Next</string>
  8. <string name="previous">Previous</string>
  9. <string name="hello">你好,世界</string>
  10. <string name="simple_calculator">牛逼算器</string>
  11. <string name="cancel">CE</string>
  12. <string name="divide">➗</string>
  13. <string name="multiply">x</string>
  14. <string name="clear">C</string>
  15. <string name="seven">7</string>
  16. <string name="eight">8</string>
  17. <string name="nine">9</string>
  18. <string name="plus">+</string>
  19. <string name="four">4</string>
  20. <string name="five">5</string>
  21. <string name="six">6</string>
  22. <string name="minus">-</string>
  23. <string name="one">1</string>
  24. <string name="two">2</string>
  25. <string name="three">3</string>
  26. <string name="reciprocal">1/x</string>
  27. <string name="zero">0</string>
  28. <string name="dot">.</string>
  29. <string name="equal">=</string>
  30. </resources>

CalcuatorActivity.java逻辑代码:
  1. package com.example.myapplication;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.TextView;
  7. public class CalcuatorActivity extends AppCompatActivity {
  8. private TextView tv_result;
  9. //第一个操作数
  10. private String firstNum = "";
  11. //运算符
  12. private String operator = "";
  13. //第二个操作数
  14. private String secondNum = "";
  15. //当前的计算结果
  16. private String result = "";
  17. //显示的文本内容
  18. private String showText = "";
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. Log.d("CalcuatorActiviy","onCreat() method called");
  23. setContentView(R.layout.activity_calcuator);
  24. //从布局中获取名交tv_result的文本视图
  25. tv_result = findViewById(R.id.tv_result);
  26. //下面每个按钮控件都组册了点击监听
  27. findViewById(R.id.btn_cancel).setOnClickListener((View.OnClickListener) this);
  28. findViewById(R.id.btn_divide).setOnClickListener((View.OnClickListener) this);
  29. findViewById(R.id.btn_multiply).setOnClickListener((View.OnClickListener) this);
  30. findViewById(R.id.btn_clear).setOnClickListener((View.OnClickListener) this);
  31. findViewById(R.id.btn_seven).setOnClickListener((View.OnClickListener) this);
  32. findViewById(R.id.btn_eight).setOnClickListener((View.OnClickListener) this);
  33. findViewById(R.id.btn_nine).setOnClickListener((View.OnClickListener) this);
  34. findViewById(R.id.btn_plus).setOnClickListener((View.OnClickListener) this);
  35. findViewById(R.id.btn_four).setOnClickListener((View.OnClickListener) this);
  36. findViewById(R.id.btn_five).setOnClickListener((View.OnClickListener) this);
  37. findViewById(R.id.btn_six).setOnClickListener((View.OnClickListener) this);
  38. findViewById(R.id.btn_minus).setOnClickListener((View.OnClickListener) this);
  39. findViewById(R.id.btn_one).setOnClickListener((View.OnClickListener) this);
  40. findViewById(R.id.btn_two).setOnClickListener((View.OnClickListener) this);
  41. findViewById(R.id.btn_three).setOnClickListener((View.OnClickListener) this);
  42. findViewById(R.id.btn_reciprocal).setOnClickListener((View.OnClickListener) this);
  43. findViewById(R.id.btn_zero).setOnClickListener((View.OnClickListener) this);
  44. findViewById(R.id.btn_dot).setOnClickListener((View.OnClickListener) this);
  45. findViewById(R.id.btn_equal).setOnClickListener((View.OnClickListener) this);
  46. findViewById(R.id.ib_sqrt).setOnClickListener((View.OnClickListener) this);
  47. }
  48. //@Override
  49. public void onClick(View v){
  50. String inputText;
  51. //如果是开根号按钮
  52. if(v.getId() == R.id.ib_sqrt){
  53. inputText = "√";
  54. }else{
  55. //除了开根号之外的其他按钮
  56. inputText = ((TextView) v).getText().toString();
  57. }
  58. switch(v.getId()){
  59. //点击了清除按钮
  60. case R.id.btn_clear:
  61. clear();
  62. break;
  63. //点击了取消按钮
  64. case R.id.btn_cancel:
  65. break;
  66. //点击了加减乘除
  67. case R.id.btn_plus:
  68. case R.id.btn_minus:
  69. case R.id.btn_multiply:
  70. case R.id.btn_divide:
  71. operator = inputText;//运算符
  72. refreshText(showText + operator);
  73. break;
  74. //等号按钮
  75. case R.id.btn_equal:
  76. //加减乘除四则运算
  77. double calculate_result = calculateFour();
  78. refreshOperate(String.valueOf(calculate_result));
  79. refreshText(showText + "=" + result);
  80. // 开根号按钮
  81. case R.id.ib_sqrt:
  82. double sqrt_result = Math.sqrt(Double.parseDouble(firstNum));
  83. refreshOperate(String.valueOf(sqrt_result));
  84. refreshText(showText + "√=" + result);
  85. break;
  86. //点击了求倒数按钮
  87. case R.id.btn_reciprocal:
  88. double reciprocal_result = 1.0/Double.parseDouble(firstNum);
  89. refreshOperate(String.valueOf(reciprocal_result));
  90. refreshText(showText + "/=" + result);
  91. break;
  92. default:
  93. //上次结果已经出来了
  94. if(result.length()>0&&operator.equals("")){
  95. clear();
  96. }
  97. //无运算符,则继续拼接第一个操作数
  98. if(operator.equals("")){
  99. firstNum = firstNum + inputText;
  100. }else{
  101. //有运算符,则继续拼接第二个操作符
  102. secondNum = secondNum + inputText;
  103. }
  104. if(showText.equals("0")&&!inputText.equals(".")){
  105. refreshText(inputText);
  106. }else {
  107. refreshText(showText + inputText);
  108. }
  109. break;
  110. }
  111. }
  112. //四则运算,返回计算结果
  113. private double calculateFour(){
  114. switch (operator){
  115. case "+":
  116. return Double.parseDouble(firstNum) + Double.parseDouble(secondNum);
  117. case "-":
  118. return Double.parseDouble(firstNum) - Double.parseDouble(secondNum);
  119. case "8":
  120. return Double.parseDouble(firstNum) * Double.parseDouble(secondNum);
  121. default:
  122. return Double.parseDouble(firstNum) / Double.parseDouble(secondNum);
  123. }
  124. }
  125. private void clear(){
  126. refreshOperate("");
  127. refreshText("");
  128. }
  129. //刷新运算结果
  130. private void refreshOperate(String new_result){
  131. result = new_result;
  132. firstNum = result;
  133. secondNum = "";
  134. operator = "";
  135. }
  136. //刷新文本显示
  137. private void refreshText(String text){
  138. showText = text;
  139. tv_result.setText(showText);
  140. }
  141. }

这完全是前端的领域我觉得

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

闽ICP备14008679号