当前位置:   article > 正文

Android——在线计算器完整代码_安卓计算器代码

安卓计算器代码

 实现效果

 

一、xml布局文件

这里采用线性布局,关于计算器的布局,可以查看之前的文章。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:id="@+id/textResult"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:textSize="30sp"
  10. android:gravity="right"
  11. android:text="" />
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:orientation="horizontal">
  16. <Button
  17. android:id="@+id/btnCLs"
  18. android:layout_width="0dp"
  19. android:layout_height="wrap_content"
  20. android:layout_weight="2"
  21. android:text="C"/>
  22. <Button
  23. android:id="@+id/btnDel"
  24. android:layout_width="0dp"
  25. android:layout_height="wrap_content"
  26. android:layout_weight="2"
  27. android:text="Del"/>
  28. </LinearLayout>
  29. <LinearLayout
  30. android:layout_width="match_parent"
  31. android:layout_height="wrap_content"
  32. android:orientation="horizontal">
  33. <Button
  34. android:id="@+id/btnOne"
  35. android:layout_width="0dp"
  36. android:layout_height="wrap_content"
  37. android:layout_weight="1"
  38. android:text="1"/>
  39. <Button
  40. android:id="@+id/btnTwo"
  41. android:layout_width="0dp"
  42. android:layout_height="wrap_content"
  43. android:layout_weight="1"
  44. android:text="2"/>
  45. <Button
  46. android:id="@+id/btnThree"
  47. android:layout_width="0dp"
  48. android:layout_height="wrap_content"
  49. android:layout_weight="1"
  50. android:text="3"/>
  51. <Button
  52. android:id="@+id/btnAdd"
  53. android:layout_width="0dp"
  54. android:layout_height="wrap_content"
  55. android:layout_weight="1"
  56. android:text="+"/>
  57. </LinearLayout>
  58. <LinearLayout
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:orientation="horizontal">
  62. <Button
  63. android:id="@+id/btnFour"
  64. android:layout_width="0dp"
  65. android:layout_height="wrap_content"
  66. android:layout_weight="1"
  67. android:text="4"/>
  68. <Button
  69. android:id="@+id/btnFive"
  70. android:layout_width="0dp"
  71. android:layout_height="wrap_content"
  72. android:layout_weight="1"
  73. android:text="5"/>
  74. <Button
  75. android:id="@+id/btnSix"
  76. android:layout_width="0dp"
  77. android:layout_height="wrap_content"
  78. android:layout_weight="1"
  79. android:text="6"/>
  80. <Button
  81. android:id="@+id/btnReduce"
  82. android:layout_width="0dp"
  83. android:layout_height="wrap_content"
  84. android:layout_weight="1"
  85. android:text="-"/>
  86. </LinearLayout>
  87. <LinearLayout
  88. android:layout_width="match_parent"
  89. android:layout_height="wrap_content"
  90. android:orientation="horizontal">
  91. <Button
  92. android:id="@+id/btnSeven"
  93. android:layout_width="0dp"
  94. android:layout_height="wrap_content"
  95. android:layout_weight="1"
  96. android:text="7"/>
  97. <Button
  98. android:id="@+id/btnEight"
  99. android:layout_width="0dp"
  100. android:layout_height="wrap_content"
  101. android:layout_weight="1"
  102. android:text="8"/>
  103. <Button
  104. android:id="@+id/btnNine"
  105. android:layout_width="0dp"
  106. android:layout_height="wrap_content"
  107. android:layout_weight="1"
  108. android:text="9"/>
  109. <Button
  110. android:id="@+id/btnMul"
  111. android:layout_width="0dp"
  112. android:layout_height="wrap_content"
  113. android:layout_weight="1"
  114. android:text="*"/>
  115. </LinearLayout>
  116. <LinearLayout
  117. android:layout_width="match_parent"
  118. android:layout_height="wrap_content"
  119. android:orientation="horizontal">
  120. <Button
  121. android:id="@+id/btnZero"
  122. android:layout_width="0dp"
  123. android:layout_height="wrap_content"
  124. android:layout_weight="1"
  125. android:text="0"/>
  126. <Button
  127. android:id="@+id/btnPoint"
  128. android:layout_width="0dp"
  129. android:layout_height="wrap_content"
  130. android:layout_weight="1"
  131. android:text="."/>
  132. <Button
  133. android:id="@+id/btnEquals"
  134. android:layout_width="0dp"
  135. android:layout_height="wrap_content"
  136. android:layout_weight="1"
  137. android:text="="/>
  138. <Button
  139. android:id="@+id/btnChu"
  140. android:layout_width="0dp"
  141. android:layout_height="wrap_content"
  142. android:layout_weight="1"
  143. android:text="÷"/>
  144. </LinearLayout>
  145. </LinearLayout>

二、MainActivity.java文件 

1.创建每个按钮的对象

2.实例化每个按钮 通过每个按钮的id进行实例化创建

3.设置每个按钮的点击事件即监听按钮
switch通过id判断被点击的按钮属于哪个控件。如果是数字或小数点,setText(str + ((Button) view).getText())。
如果是加减乘除,setText(str + " " + ((Button) view).getText() + " ")。这里的空格是很巧妙的,对下面运算结果中提取两个数字大有帮助。
如果是清空按钮,setText("")。
如果是删了后面一位数字,setText(str.substring(0,str.length()-1))。调用substring(beginIndex,endIndex)函数。substring函数是用来截取一个字段的一部分。
如果是等于按钮,调用getresult()方法。

4.创建运算结果的方法getresult()。
首先将其中的两个数字和运算符分割出来,调用substring(0,exp.indexOf(" "))函数。indexOf函数用于返回[目标字符串]在[源字符串]中的位置。
接着判断两个数都有,还是第一个数/第二个数为空,分情况写代码。

5.MainActivity.java代码如下:

  1. import android.support.v7.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.Button;
  5. import android.widget.TextView;
  6. public class MainActivity extends AppCompatActivity implements View.OnClickListener{//实现鼠标点击的事件的监听接口
  7. //创建每个按钮的对象
  8. //数字按钮
  9. Button btnOne,btnTwo,btnThree,btnFour,btnFive,btnSix,btnSeven,btnEight,btnNine,btnZero;
  10. //加减乘除
  11. Button btnChu,btnAdd,btnReduce,btnMul;
  12. //其他按钮
  13. Button btnCLs,btnDel,btnEquals,btnPoint;
  14. //文本框
  15. TextView textResult;
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_main);//设置采用哪一个布局文件
  20. //实例化每个按钮 通过每个按钮的id进行实例化创建
  21. btnOne = (Button) findViewById(R.id.btnOne);
  22. btnTwo = (Button) findViewById(R.id.btnTwo);
  23. btnThree = (Button) findViewById(R.id.btnThree);
  24. btnFour = (Button) findViewById(R.id.btnFour);
  25. btnFive = (Button) findViewById(R.id.btnFive);
  26. btnSix = (Button) findViewById(R.id.btnSix);
  27. btnSeven = (Button) findViewById(R.id.btnSeven);
  28. btnEight = (Button) findViewById(R.id.btnEight);
  29. btnNine = (Button) findViewById(R.id.btnNine);
  30. btnZero = (Button) findViewById(R.id.btnZero);
  31. btnChu = (Button) findViewById(R.id.btnChu);
  32. btnAdd = (Button) findViewById(R.id.btnAdd);
  33. btnReduce = (Button) findViewById(R.id.btnReduce);
  34. btnMul = (Button) findViewById(R.id.btnMul);
  35. btnCLs = (Button) findViewById(R.id.btnCLs);
  36. btnDel = (Button) findViewById(R.id.btnDel);
  37. btnEquals = (Button) findViewById(R.id.btnEquals);
  38. btnPoint = (Button) findViewById(R.id.btnPoint);
  39. textResult = (TextView)findViewById(R.id.textResult);
  40. //设置每个按钮的点击事件即监听按钮
  41. btnOne.setOnClickListener(this);
  42. btnTwo.setOnClickListener(this);
  43. btnThree.setOnClickListener(this);
  44. btnFour.setOnClickListener(this);
  45. btnFive.setOnClickListener(this);
  46. btnSix.setOnClickListener(this);
  47. btnSeven.setOnClickListener(this);
  48. btnEight.setOnClickListener(this);
  49. btnNine.setOnClickListener(this);
  50. btnZero.setOnClickListener(this);
  51. btnChu.setOnClickListener(this);
  52. btnAdd.setOnClickListener(this);
  53. btnReduce.setOnClickListener(this);
  54. btnMul.setOnClickListener(this);
  55. btnCLs.setOnClickListener(this);
  56. btnDel.setOnClickListener(this);
  57. btnEquals.setOnClickListener(this);
  58. btnPoint.setOnClickListener(this);
  59. }
  60. @Override
  61. public void onClick(View view) {
  62. String str = textResult.getText().toString(); //获取当前textview文本
  63. switch (view.getId()) {
  64. case R.id.btnOne:
  65. case R.id.btnTwo:
  66. case R.id.btnThree:
  67. case R.id.btnFour:
  68. case R.id.btnFive:
  69. case R.id.btnSix:
  70. case R.id.btnSeven:
  71. case R.id.btnEight:
  72. case R.id.btnNine:
  73. case R.id.btnZero:
  74. case R.id.btnPoint:
  75. textResult.setText(str + ((Button) view).getText());
  76. break;
  77. case R.id.btnChu:
  78. case R.id.btnAdd:
  79. case R.id.btnReduce:
  80. case R.id.btnMul:
  81. textResult.setText(str + " " + ((Button) view).getText() + " ");
  82. break;
  83. case R.id.btnCLs:
  84. textResult.setText("");
  85. break;
  86. case R.id.btnDel:
  87. if(str != null && !str.equals("")){
  88. textResult.setText(str.substring(0,str.length()-1)); // substring(beginIndex, endIndex) [beginIndex, endIndex)
  89. } // str="123456" str.substring(0,3) ="123"; "1237 + 10"
  90. break;
  91. case R.id.btnEquals:
  92. getresult();
  93. }
  94. }
  95. private void getresult() {
  96. String exp = textResult.getText().toString(); //从textview获取整个文本
  97. if(exp == null || exp.equals("")){
  98. return;
  99. }
  100. if(!exp.contains(" ")){
  101. return;
  102. }
  103. //将其中的两个数字和运算符分割出来
  104. float result = 0; // exp ="123 + 5"
  105. String num1 = exp.substring(0,exp.indexOf(" ")); // num1 ="123"
  106. String ex = exp.substring(exp.indexOf(" ") + 1,exp.indexOf(" ") + 2); // ex="+"
  107. String num2 = exp.substring(exp.indexOf(" ") + 3); // num2="5"
  108. //如果两个数字都不为空则判断运算符进行运算
  109. if(!num1.equals("") && !num2.equals("")) {
  110. float d1 = Float.parseFloat(num1);
  111. float d2 = Float.parseFloat(num2);
  112. if (ex.equals("+")) {
  113. result = d1 + d2;
  114. } else if (ex.equals("-")) {
  115. result = d1 - d2;
  116. } else if (ex.equals("*")) {
  117. result = d1 * d2;
  118. } else if (ex.equals("÷")) {
  119. if (d2 == 0) {
  120. result = 0;
  121. } else {
  122. result = d1 / d2;
  123. }
  124. }
  125. textResult.setText(result + "");
  126. }else if(!num1.equals("") && num2.equals("")){
  127. textResult.setText(exp.substring(0,exp.indexOf(" "))); //textResult.setText(exp);
  128. }else if(num1.equals("") && !num2.equals("")){
  129. float d3 = Float.parseFloat(num2);
  130. if(ex.equals("+")){
  131. result = 0 + d3;
  132. }else if(ex.equals("-")){
  133. result = 0 - d3;
  134. }else if(ex.equals("*")){
  135. result = 0 * d3;
  136. }else if(ex.equals("÷")){
  137. result = 0;
  138. }
  139. textResult.setText(result + "");
  140. }else{
  141. textResult.setText("");
  142. }
  143. }
  144. }

四、总结

总体来说很不错,也实现了清除后面一个数的功能,但还未能实现负数的运算 。

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

闽ICP备14008679号