当前位置:   article > 正文

Android实现计算器_android计算器

android计算器

布局

布局展示:

 布局分析:

首先是一个线性布局所以整体布局采用<LinearLayout>

其次采用一个垂直方向滚动视图<ScrollView>

显示标题用一个<TextView>,显示输出结果用一个<TextView>

下方按钮行列均匀,采用网格布局<GridLayout>

XML布局代码展示:

  1. //前后端分离,存放路径app-->src-->main-->res-->layout-->activity_calculator.xml
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:tools="http://schemas.android.com/tools"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. android:background="#EEEEEE"
  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:gravity="center"
  22. android:textColor="@color/black"
  23. android:textSize="20sp"
  24. android:text="@string/calculator"/>
  25. <TextView
  26. android:id="@+id/tv_result"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:background="@color/white"
  30. android:lines="3"
  31. android:text="0"
  32. android:textColor="@color/black"
  33. android:textSize="25sp"
  34. android:gravity="right|bottom"/>
  35. <GridLayout
  36. android:layout_width="match_parent"
  37. android:layout_height="wrap_content"
  38. android:columnCount="4"
  39. android:rowCount="5">
  40. <Button
  41. android:id="@+id/btn_cancel"
  42. android:layout_width="0dp"
  43. android:layout_height="@dimen/button_height"
  44. android:layout_columnWeight="1"
  45. android:gravity="center"
  46. android:text="@string/cancel"
  47. android:textColor="@color/black"
  48. android:textSize="@dimen/button_font_size"
  49. />
  50. <Button
  51. android:id="@+id/btn_divide"
  52. android:layout_width="0dp"
  53. android:layout_height="@dimen/button_height"
  54. android:layout_columnWeight="1"
  55. android:gravity="center"
  56. android:text="@string/divide"
  57. android:textColor="@color/black"
  58. android:textSize="@dimen/button_font_size"
  59. />
  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/multiply"
  67. android:textColor="@color/black"
  68. android:textSize="@dimen/button_font_size"
  69. />
  70. <Button
  71. android:id="@+id/btn_clear"
  72. android:layout_width="0dp"
  73. android:layout_height="@dimen/button_height"
  74. android:layout_columnWeight="1"
  75. android:gravity="center"
  76. android:text="@string/clear"
  77. android:textColor="@color/black"
  78. android:textSize="@dimen/button_font_size"
  79. />
  80. <Button
  81. android:id="@+id/btn_seven"
  82. android:layout_width="0dp"
  83. android:layout_height="@dimen/button_height"
  84. android:layout_columnWeight="1"
  85. android:gravity="center"
  86. android:text="7"
  87. android:textColor="@color/black"
  88. android:textSize="@dimen/button_font_size"
  89. />
  90. <Button
  91. android:id="@+id/eight"
  92. android:layout_width="0dp"
  93. android:layout_height="@dimen/button_height"
  94. android:layout_columnWeight="1"
  95. android:gravity="center"
  96. android:text="8"
  97. android:textColor="@color/black"
  98. android:textSize="@dimen/button_font_size"
  99. />
  100. <Button
  101. android:id="@+id/nine"
  102. android:layout_width="0dp"
  103. android:layout_height="@dimen/button_height"
  104. android:layout_columnWeight="1"
  105. android:gravity="center"
  106. android:text="9"
  107. android:textColor="@color/black"
  108. android:textSize="@dimen/button_font_size"
  109. />
  110. <Button
  111. android:id="@+id/btn_add"
  112. android:layout_width="0dp"
  113. android:layout_height="@dimen/button_height"
  114. android:layout_columnWeight="1"
  115. android:gravity="center"
  116. android:text="@string/add"
  117. android:textColor="@color/black"
  118. android:textSize="@dimen/button_font_size"
  119. />
  120. <Button
  121. android:id="@+id/btn_four"
  122. android:layout_width="0dp"
  123. android:layout_height="@dimen/button_height"
  124. android:layout_columnWeight="1"
  125. android:gravity="center"
  126. android:text="4"
  127. android:textColor="@color/black"
  128. android:textSize="@dimen/button_font_size"
  129. />
  130. <Button
  131. android:id="@+id/btn_five"
  132. android:layout_width="0dp"
  133. android:layout_height="@dimen/button_height"
  134. android:layout_columnWeight="1"
  135. android:gravity="center"
  136. android:text="5"
  137. android:textColor="@color/black"
  138. android:textSize="@dimen/button_font_size"
  139. />
  140. <Button
  141. android:id="@+id/btn_six"
  142. android:layout_width="0dp"
  143. android:layout_height="@dimen/button_height"
  144. android:layout_columnWeight="1"
  145. android:gravity="center"
  146. android:text="6"
  147. android:textColor="@color/black"
  148. android:textSize="@dimen/button_font_size"
  149. />
  150. <Button
  151. android:id="@+id/btn_minus"
  152. android:layout_width="0dp"
  153. android:layout_height="@dimen/button_height"
  154. android:layout_columnWeight="1"
  155. android:gravity="center"
  156. android:text="@string/minus"
  157. android:textColor="@color/black"
  158. android:textSize="@dimen/button_font_size"
  159. />
  160. <Button
  161. android:id="@+id/btn_one"
  162. android:layout_width="0dp"
  163. android:layout_height="@dimen/button_height"
  164. android:layout_columnWeight="1"
  165. android:gravity="center"
  166. android:text="1"
  167. android:textColor="@color/black"
  168. android:textSize="@dimen/button_font_size"
  169. />
  170. <Button
  171. android:id="@+id/btn_two"
  172. android:layout_width="0dp"
  173. android:layout_height="@dimen/button_height"
  174. android:layout_columnWeight="1"
  175. android:gravity="center"
  176. android:text="2"
  177. android:textColor="@color/black"
  178. android:textSize="@dimen/button_font_size"
  179. />
  180. <Button
  181. android:id="@+id/btn_three"
  182. android:layout_width="0dp"
  183. android:layout_height="@dimen/button_height"
  184. android:layout_columnWeight="1"
  185. android:gravity="center"
  186. android:text="3"
  187. android:textColor="@color/black"
  188. android:textSize="@dimen/button_font_size"
  189. />
  190. <Button
  191. android:id="@+id/btn_square"
  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/square"
  197. android:textColor="@color/black"
  198. android:textSize="@dimen/button_font_size"
  199. />
  200. <Button
  201. android:id="@+id/btn_inverse"
  202. android:layout_width="0dp"
  203. android:layout_height="@dimen/button_height"
  204. android:layout_columnWeight="1"
  205. android:gravity="center"
  206. android:text="@string/inverse"
  207. android:textColor="@color/black"
  208. android:textSize="@dimen/button_font_size"
  209. />
  210. <Button
  211. android:id="@+id/btn_zero"
  212. android:layout_width="0dp"
  213. android:layout_height="@dimen/button_height"
  214. android:layout_columnWeight="1"
  215. android:gravity="center"
  216. android:text="0"
  217. android:textColor="@color/black"
  218. android:textSize="@dimen/button_font_size"
  219. />
  220. <Button
  221. android:id="@+id/btn_point"
  222. android:layout_width="0dp"
  223. android:layout_height="@dimen/button_height"
  224. android:layout_columnWeight="1"
  225. android:gravity="center"
  226. android:text="@string/point"
  227. android:textColor="@color/black"
  228. android:textSize="@dimen/button_font_size"
  229. />
  230. <Button
  231. android:id="@+id/btn_equal"
  232. android:layout_width="0dp"
  233. android:layout_height="@dimen/button_height"
  234. android:layout_columnWeight="1"
  235. android:gravity="center"
  236. android:text="@string/equal"
  237. android:textColor="@color/black"
  238. android:textSize="@dimen/button_font_size"
  239. />
  240. </GridLayout>
  241. </LinearLayout>
  242. </ScrollView>
  243. </LinearLayout>

逻辑部分:

 首先要定义几个变量存放运行数据。

private TextView tv_result;    //返回
private String firstNum="";    //第一个操作数
private String secondNum="";   //第二个操作数
private String operator="";    //操作符号
private String result="";      //当前返回
private String showText="";    //显示文本

   在Calculate开始时获取到点击事件数据并在onCreate()中保存。在对于数据进行处理。其中定义了calculateFour()函数处理加减乘四则运算, clear()用于清除字符串,refreshOperate()用于刷新运算结果.......具体详解代码如下。

Java代码展示:

  1. //app-->src-->main-->java-->CalculatorActivity
  2. package com.example.myapplication;
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Switch;
  7. import android.widget.TextView;
  8. public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
  9. private TextView tv_result; //返回
  10. private String firstNum=""; //第一个操作数
  11. private String secondNum=""; //第二个操作数
  12. private String operator=""; //操作符号
  13. private String result=""; //当前返回
  14. private String showText=""; //显示文本
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_calculator);
  19. tv_result = findViewById(R.id.tv_result);
  20. findViewById(R.id.btn_cancel).setOnClickListener(this); //CE 取消
  21. findViewById(R.id.btn_clear).setOnClickListener(this);// C 清除
  22. findViewById(R.id.btn_add).setOnClickListener(this); // +加法
  23. findViewById(R.id.btn_minus).setOnClickListener(this);// -减法
  24. findViewById(R.id.btn_multiply).setOnClickListener(this);//乘法
  25. findViewById(R.id.btn_divide).setOnClickListener(this); //除法
  26. findViewById(R.id.btn_equal).setOnClickListener(this); //等于
  27. findViewById(R.id.btn_square).setOnClickListener(this); //开平方
  28. findViewById(R.id.btn_point).setOnClickListener(this); //点
  29. findViewById(R.id.btn_inverse).setOnClickListener(this);//倒数
  30. findViewById(R.id.btn_zero).setOnClickListener(this); //0
  31. findViewById(R.id.btn_one).setOnClickListener(this); //1
  32. findViewById(R.id.btn_two).setOnClickListener(this); //2
  33. findViewById(R.id.btn_three).setOnClickListener(this); //3
  34. findViewById(R.id.btn_four).setOnClickListener(this); //4
  35. findViewById(R.id.btn_five).setOnClickListener(this); //5
  36. findViewById(R.id.btn_six).setOnClickListener(this); //6
  37. findViewById(R.id.btn_seven).setOnClickListener(this); //7
  38. findViewById(R.id.eight).setOnClickListener(this); //8
  39. findViewById(R.id.nine).setOnClickListener(this); //9
  40. }
  41. @Override
  42. public void onClick(View view) {
  43. String inputText; //变量用于输出信息
  44. if(view.getId()==R.id.btn_square)
  45. {inputText="√";}
  46. else
  47. {inputText = ((TextView)view).getText().toString();}
  48. switch (view.getId())
  49. {//点击了清除按钮
  50. case R.id.btn_clear: //清除按钮
  51. clear();
  52. break;
  53. case R.id.btn_clear: //回退按钮(逻辑未做)
  54. break;
  55. case R.id.btn_add: //加
  56. case R.id.btn_minus: //减
  57. case R.id.btn_multiply: //乘
  58. case R.id.btn_divide: //除法
  59. {operator=inputText; //运算符
  60. refreshText(showText+operator);}
  61. break;
  62. case R.id.btn_equal: //等号
  63. {double calculate_result= calculateFour();
  64. refreshOperate(String.valueOf(calculate_result));
  65. refreshOperate(showText+"="+result);}
  66. break;
  67. case R.id.btn_square: //开方
  68. {double btn_square=Math.sqrt(Double.parseDouble(firstNum)); //处理开发
  69. refreshOperate(String.valueOf(btn_square));
  70. refreshOperate(showText+"√="+result);}
  71. break;
  72. case R.id.btn_inverse: //求倒数
  73. {double inverse_result=1.0/Double.parseDouble(firstNum); //处理倒数
  74. refreshOperate(String.valueOf(inverse_result));
  75. refreshOperate(showText+"/="+result);}
  76. break;
  77. default:
  78. if(result.length()>0&&operator.equals("")) //判断运算结束时情况
  79. { clear();}
  80. //无运算符,则继续拼接第一个操作数
  81. if(operator.equals(""))
  82. {
  83. firstNum=firstNum+inputText;
  84. }else {
  85. secondNum=secondNum+inputText;
  86. }
  87. //整数不需要前面的0
  88. if(showText.equals(0)&&!inputText.equals("."))
  89. {
  90. refreshText(inputText);
  91. }
  92. refreshText(showText+inputText);
  93. break;
  94. }
  95. }
  96. private double calculateFour() { //四则运算
  97. switch (operator)
  98. { case "+":
  99. return Double.parseDouble(firstNum)+Double.parseDouble(secondNum);
  100. case "-":
  101. return Double.parseDouble(firstNum)-Double.parseDouble(secondNum);
  102. case "*":
  103. return Double.parseDouble(firstNum)*Double.parseDouble(secondNum);
  104. default:
  105. return Double.parseDouble(firstNum)/Double.parseDouble(secondNum);
  106. }
  107. }
  108. //清空字符串
  109. private void clear() {
  110. refreshOperate("");
  111. refreshText("");
  112. }
  113. //刷新运算结果
  114. private void refreshOperate(String new_result)
  115. {
  116. result=new_result;
  117. firstNum=result;
  118. secondNum="";
  119. operator="";
  120. }
  121. //刷新显示文本
  122. private void refreshText(String text){
  123. showText=text;
  124. tv_result.setText(showText);
  125. }
  126. }

Android开发,前后端分离,数据与代码分离所以定义的字符串都单读放在string.xml里

其余部分:

  1. //app-->src-->res-->values-->strings.xml
  2. <resources>
  3. <string name="app_name">My Application</string>
  4. <string name="app_val">value1</string>
  5. <string name="calculator">简易计算器</string>
  6. <string name="cancel">CE</string>
  7. <string name="add">+</string>
  8. <string name="minus">-</string>
  9. <string name="divide">/</string>
  10. <string name="multiply">*</string>
  11. <string name="clear">C</string>
  12. <string name="inverse">1/X</string>
  13. <string name="point">.</string>
  14. <string name="equal">=</string>
  15. <string name="square"></string>
  16. </resources>

总结:

   这个简易计算器还有很多bug例如,没有判断错误情况,三操作数情况,负数,科学计算器的功能等等,有待完善。(仅用于对前段时间知识的总结)。

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号