当前位置:   article > 正文

Android studio计算器

android studio计算器

计算器,但是还有一点bug,我不知道怎么改了

文件列表

MainActivity.java

  1. package com.example.calculator;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.TextView;
  6. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  7. private TextView tv_result;
  8. //第一个操作数
  9. private String firstNum="";
  10. //操作符
  11. private String operator="";
  12. //第二个操作数
  13. private String secoundNum="";
  14. //当前计算结果
  15. private String result="";
  16. //显示的文本内容
  17. private String showText="";
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. //从布局文件中获取名叫tv_result的文本视图
  23. tv_result=findViewById(R.id.tv_result);
  24. //下面给每个按钮控件都注册了点击监听器
  25. findViewById(R.id.btn_cancel).setOnClickListener(this);//C
  26. findViewById(R.id.btn_clear).setOnClickListener(this);
  27. findViewById(R.id.btn_divide).setOnClickListener(this);
  28. findViewById(R.id.btn_dot).setOnClickListener(this);
  29. findViewById(R.id.btn_eight).setOnClickListener(this);
  30. findViewById(R.id.btn_equal).setOnClickListener(this);
  31. findViewById(R.id.btn_five).setOnClickListener(this);
  32. findViewById(R.id.btn_four).setOnClickListener(this);
  33. findViewById(R.id.btn_minus).setOnClickListener(this);
  34. findViewById(R.id.btn_multiply).setOnClickListener(this);
  35. findViewById(R.id.btn_nine).setOnClickListener(this);
  36. findViewById(R.id.btn_one).setOnClickListener(this);
  37. findViewById(R.id.btn_plus).setOnClickListener(this);
  38. findViewById(R.id.btn_reciprocal).setOnClickListener(this);
  39. findViewById(R.id.btn_seven).setOnClickListener(this);
  40. findViewById(R.id.btn_six).setOnClickListener(this);
  41. findViewById(R.id.btn_sqrt).setOnClickListener(this);
  42. findViewById(R.id.btn_three).setOnClickListener(this);
  43. findViewById(R.id.btn_two).setOnClickListener(this);
  44. findViewById(R.id.btn_zero).setOnClickListener(this);
  45. }
  46. @Override
  47. public void onClick(View v) {
  48. String inputText;
  49. //如果是开根号的
  50. if(v.getId()==R.id.btn_sqrt){
  51. inputText="√";
  52. }
  53. else{
  54. inputText=((TextView) v).getText().toString();
  55. }
  56. switch (v.getId()){
  57. case R.id.btn_clear:
  58. clear();
  59. break;
  60. case R.id.btn_cancel:
  61. if(showText=="")
  62. break;
  63. else{
  64. showText=showText.substring(0,showText.length()-1);
  65. refreshText(showText);}
  66. break;
  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. case R.id.btn_equal:
  75. if(operator==""&&firstNum!=""||secoundNum==""){//6=6,6+=6
  76. showText=firstNum;
  77. refreshText(showText);
  78. }else{
  79. double calculate_result=calculateFour();
  80. refreshOperate(String.valueOf(calculate_result));
  81. refreshText(showText+"="+result);}
  82. break;
  83. case R.id.btn_sqrt:
  84. double sqrt_result=Math.sqrt(Double.parseDouble(firstNum));
  85. refreshOperate(String.valueOf(sqrt_result));
  86. refreshText(showText+"√="+result);
  87. break;
  88. case R.id.btn_reciprocal:
  89. double reciprocal_result=1.0/Double.parseDouble(firstNum);
  90. refreshOperate(String.valueOf(reciprocal_result));
  91. refreshText("1.0/"+showText+"="+result);
  92. break;
  93. default:
  94. //上次结果已经出来了,在输入数字时,之前的应该清空
  95. if(result.length()>0&&operator.equals("")){
  96. clear();
  97. }
  98. if(operator.equals("")){//没有运算符,先输入第一个操作数
  99. firstNum=firstNum+inputText;
  100. }else//如果有运算符,就输入第二个操作数
  101. secoundNum=secoundNum+inputText;
  102. if(showText.equals("0")&& !inputText.equals(".")) {
  103. refreshText(inputText);//如果前面是一个0,后面不是小数点,而是一个数,就不需要接上前面的0,直接显示输入的数
  104. }else{
  105. refreshText(showText + inputText);//这个零的后面如果是小数点,那么前面的0就拼接上
  106. }
  107. break;
  108. }
  109. }
  110. //刷新文本显示
  111. private void refreshText(String text){
  112. showText=text;
  113. tv_result.setText(showText);
  114. }
  115. //清空并初始化
  116. private void clear(){
  117. refreshOperate("");
  118. refreshText("");
  119. }
  120. //刷新运算结果,第一次运算4+5=9,第二次运算时,这个9就变成了firstNum,secoundNum和operator都为空再次重新赋值
  121. private void refreshOperate(String new_result){
  122. result=new_result;
  123. firstNum=result;
  124. secoundNum="";
  125. operator="";
  126. }
  127. //四则运算
  128. private double calculateFour(){
  129. switch (operator){
  130. case"+":
  131. return Double .parseDouble(firstNum)+Double.parseDouble(secoundNum);
  132. case"":
  133. return Double .parseDouble(firstNum)-Double.parseDouble(secoundNum);
  134. case"×":
  135. return Double .parseDouble(firstNum)*Double.parseDouble(secoundNum);
  136. default:
  137. return Double .parseDouble(firstNum)/Double.parseDouble(secoundNum);
  138. }
  139. }
  140. }

 activity_main.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. tools:context=".MainActivity"
  8. android:orientation="vertical"
  9. android:background="@color/purple_200"
  10. android:padding="5dp">
  11. <ScrollView
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content">
  14. <LinearLayout
  15. android:layout_width="match_parent"
  16. android:layout_height="wrap_content"
  17. android:orientation="vertical">
  18. <TextView
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:text="@string/simple_calculator"
  22. android:lines="3"
  23. android:gravity="center"
  24. android:textSize="25sp"
  25. android:textColor="@color/black"/>
  26. <TextView
  27. android:id="@+id/tv_result"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:background="@color/white"
  31. android:text="0"
  32. android:textColor="@color/black"
  33. android:textSize="25sp"
  34. android:lines="4"
  35. android:gravity="right|bottom"/>
  36. <GridLayout
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:columnCount="4"
  40. android:rowCount="5">
  41. <Button
  42. android:id="@+id/btn_cancel"
  43. android:layout_width="0dp"
  44. android:layout_height="@dimen/button_height"
  45. android:layout_columnWeight="1"
  46. android:gravity="center"
  47. android:text="@string/cancel"
  48. android:textColor="@color/black"
  49. android:textSize="@dimen/button_font_size"/>
  50. "
  51. <Button
  52. android:id="@+id/btn_divide"
  53. android:layout_width="0dp"
  54. android:layout_height="@dimen/button_height"
  55. android:layout_columnWeight="1"
  56. android:gravity="center"
  57. android:text="@string/divide"
  58. android:textColor="@color/black"
  59. android:textSize="@dimen/button_font_size"/>
  60. <Button
  61. android:id="@+id/btn_multiply"
  62. android:layout_width="0dp"
  63. android:layout_height="@dimen/button_height"
  64. android:layout_columnWeight="1"
  65. android:gravity="center"
  66. android:text="@string/multiplt"
  67. android:textColor="@color/black"
  68. android:textSize="@dimen/button_font_size"/>
  69. <Button
  70. android:id="@+id/btn_clear"
  71. android:layout_width="0dp"
  72. android:layout_height="@dimen/button_height"
  73. android:layout_columnWeight="1"
  74. android:gravity="center"
  75. android:text="@string/clear"
  76. android:textColor="@color/black"
  77. android:textSize="@dimen/button_font_size"/>
  78. </GridLayout>
  79. <GridLayout
  80. android:layout_width="match_parent"
  81. android:layout_height="wrap_content"
  82. android:columnCount="4"
  83. android:rowCount="5">
  84. <Button
  85. android:id="@+id/btn_seven"
  86. android:layout_width="0dp"
  87. android:layout_height="@dimen/button_height"
  88. android:layout_columnWeight="1"
  89. android:gravity="center"
  90. android:text="@string/seven"
  91. android:textColor="@color/black"
  92. android:textSize="@dimen/button_font_size"/>
  93. "
  94. <Button
  95. android:id="@+id/btn_eight"
  96. android:layout_width="0dp"
  97. android:layout_height="@dimen/button_height"
  98. android:layout_columnWeight="1"
  99. android:gravity="center"
  100. android:text="@string/eight"
  101. android:textColor="@color/black"
  102. android:textSize="@dimen/button_font_size"/>
  103. <Button
  104. android:id="@+id/btn_nine"
  105. android:layout_width="0dp"
  106. android:layout_height="@dimen/button_height"
  107. android:layout_columnWeight="1"
  108. android:gravity="center"
  109. android:text="@string/nine"
  110. android:textColor="@color/black"
  111. android:textSize="@dimen/button_font_size"/>
  112. <Button
  113. android:id="@+id/btn_plus"
  114. android:layout_width="0dp"
  115. android:layout_height="@dimen/button_height"
  116. android:layout_columnWeight="1"
  117. android:gravity="center"
  118. android:text="@string/plus"
  119. android:textColor="@color/black"
  120. android:textSize="@dimen/button_font_size"/>
  121. </GridLayout>
  122. <GridLayout
  123. android:layout_width="match_parent"
  124. android:layout_height="wrap_content"
  125. android:columnCount="4"
  126. android:rowCount="5">
  127. <Button
  128. android:id="@+id/btn_four"
  129. android:layout_width="0dp"
  130. android:layout_height="@dimen/button_height"
  131. android:layout_columnWeight="1"
  132. android:gravity="center"
  133. android:text="@string/four"
  134. android:textColor="@color/black"
  135. android:textSize="@dimen/button_font_size"/>
  136. "
  137. <Button
  138. android:id="@+id/btn_five"
  139. android:layout_width="0dp"
  140. android:layout_height="@dimen/button_height"
  141. android:layout_columnWeight="1"
  142. android:gravity="center"
  143. android:text="@string/five"
  144. android:textColor="@color/black"
  145. android:textSize="@dimen/button_font_size"/>
  146. <Button
  147. android:id="@+id/btn_six"
  148. android:layout_width="0dp"
  149. android:layout_height="@dimen/button_height"
  150. android:layout_columnWeight="1"
  151. android:gravity="center"
  152. android:text="@string/six"
  153. android:textColor="@color/black"
  154. android:textSize="@dimen/button_font_size"/>
  155. <Button
  156. android:id="@+id/btn_minus"
  157. android:layout_width="0dp"
  158. android:layout_height="@dimen/button_height"
  159. android:layout_columnWeight="1"
  160. android:gravity="center"
  161. android:text="@string/minus"
  162. android:textColor="@color/black"
  163. android:textSize="@dimen/button_font_size"/>
  164. </GridLayout>
  165. <GridLayout
  166. android:layout_width="match_parent"
  167. android:layout_height="wrap_content"
  168. android:columnCount="4"
  169. android:rowCount="5">
  170. <Button
  171. android:id="@+id/btn_one"
  172. android:layout_width="0dp"
  173. android:layout_height="@dimen/button_height"
  174. android:layout_columnWeight="1"
  175. android:gravity="center"
  176. android:text="@string/one"
  177. android:textColor="@color/black"
  178. android:textSize="@dimen/button_font_size"/>
  179. "
  180. <Button
  181. android:id="@+id/btn_two"
  182. android:layout_width="0dp"
  183. android:layout_height="@dimen/button_height"
  184. android:layout_columnWeight="1"
  185. android:gravity="center"
  186. android:text="@string/two"
  187. android:textColor="@color/black"
  188. android:textSize="@dimen/button_font_size"/>
  189. <Button
  190. android:id="@+id/btn_three"
  191. android:layout_width="0dp"
  192. android:layout_height="@dimen/button_height"
  193. android:layout_columnWeight="1"
  194. android:gravity="center"
  195. android:text="@string/three"
  196. android:textColor="@color/black"
  197. android:textSize="@dimen/button_font_size"/>
  198. <Button
  199. android:id="@+id/btn_sqrt"
  200. android:layout_width="0dp"
  201. android:layout_height="@dimen/button_height"
  202. android:layout_columnWeight="1"
  203. android:gravity="center"
  204. android:text="@string/sqrt"
  205. android:textColor="@color/black"
  206. android:textSize="@dimen/button_font_size"/>
  207. </GridLayout>
  208. <GridLayout
  209. android:layout_width="match_parent"
  210. android:layout_height="wrap_content"
  211. android:columnCount="4"
  212. android:rowCount="5">
  213. <Button
  214. android:id="@+id/btn_reciprocal"
  215. android:layout_width="0dp"
  216. android:layout_height="@dimen/button_height"
  217. android:layout_columnWeight="1"
  218. android:gravity="center"
  219. android:text="@string/reciprocal"
  220. android:textColor="@color/black"
  221. android:textSize="@dimen/button_font_size"/>
  222. "
  223. <Button
  224. android:id="@+id/btn_zero"
  225. android:layout_width="0dp"
  226. android:layout_height="@dimen/button_height"
  227. android:layout_columnWeight="1"
  228. android:gravity="center"
  229. android:text="@string/zero"
  230. android:textColor="@color/black"
  231. android:textSize="@dimen/button_font_size"/>
  232. <Button
  233. android:id="@+id/btn_dot"
  234. android:layout_width="0dp"
  235. android:layout_height="@dimen/button_height"
  236. android:layout_columnWeight="1"
  237. android:gravity="center"
  238. android:text="@string/dot"
  239. android:textColor="@color/black"
  240. android:textSize="@dimen/button_font_size"/>
  241. <Button
  242. android:id="@+id/btn_equal"
  243. android:layout_width="0dp"
  244. android:layout_height="@dimen/button_height"
  245. android:layout_columnWeight="1"
  246. android:gravity="center"
  247. android:text="@string/equal"
  248. android:textColor="@color/black"
  249. android:textSize="@dimen/button_font_size"/>
  250. </GridLayout>
  251. </LinearLayout>
  252. </ScrollView>
  253. </LinearLayout>

dimens.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  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">Calculator</string>
  3. <string name="simple_calculator">简单计算器</string>
  4. <string name="cancel">CE</string>
  5. <string name="divide">÷</string>
  6. <string name="multiplt">×</string>
  7. <string name="clear">C</string>
  8. <string name="seven">7</string>
  9. <string name="eight">8</string>
  10. <string name="nine">9</string>
  11. <string name="plus">+</string>
  12. <string name="four">4</string>
  13. <string name="five">5</string>
  14. <string name="six">6</string>
  15. <string name="minus">-</string>
  16. <string name="one">1</string>
  17. <string name="two">2</string>
  18. <string name="three">3</string>
  19. <string name="reciprocal">1/x</string>
  20. <string name="zero">0</string>
  21. <string name="dot">.</string>
  22. <string name="equal">=</string>
  23. <string name="sqrt">√ ̄</string>
  24. </resources>

效果:

 

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

闽ICP备14008679号