当前位置:   article > 正文

AndroidStudio案例——简单计算器_android studio计算器

android studio计算器

效果展示

实验内容及步骤 

            设计一款带有可视化界面的简单计算器,供用户输入数据并查看结果。用户通过点击相应按钮(加减乘除运算符、等号、数字)输入正确的表达式,计算器进行相应的加减乘除运算,且可以进行小数和整数的运算;长按清除按钮3秒,可以清除已录入的内容。

步骤:

  • Layout文件夹中建立布局文件activity_main.xml,完成计算器界面的网格布局设计,包括了一个文本编辑框和17个按钮。
  • 为每一个按钮编写单击事件,实现对应功能;点击数字和加减乘除按钮实现表达式的录入,并显示在TextView中;点击等号按钮,根据表达式计算结果;长按清除按钮3秒以上,清除录入的表达式

代码

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:padding="10dp">
  10. <EditText
  11. android:id="@+id/result"
  12. android:layout_width="match_parent"
  13. android:layout_height="60dp"
  14. android:layout_marginLeft="4dp"
  15. android:paddingLeft="10dp"
  16. android:textSize="30dp"
  17. android:textColor="@color/colorPrimary"
  18. android:enabled="false"
  19. />
  20. <GridLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:rowCount="5"
  24. android:columnCount="4"
  25. >
  26. <Button
  27. android:id="@+id/cls"
  28. android:layout_width="match_parent"
  29. android:layout_height="60dp"
  30. android:text="清除"
  31. android:textSize="20dp"
  32. android:textColor="@color/black"
  33. android:layout_columnSpan="4"
  34. android:background="@drawable/btn"
  35. />
  36. <Button
  37. android:id="@+id/one"
  38. android:layout_width="wrap_content"
  39. android:layout_height="60dp"
  40. android:text="1"
  41. android:textSize="20dp"
  42. android:layout_columnWeight="1"
  43. android:background="@drawable/btn"
  44. />
  45. <Button
  46. android:id="@+id/two"
  47. android:layout_width="wrap_content"
  48. android:layout_height="60dp"
  49. android:text="2"
  50. android:textSize="20dp"
  51. android:layout_columnWeight="1"
  52. android:background="@drawable/btn"
  53. />
  54. <Button
  55. android:id="@+id/three"
  56. android:layout_width="wrap_content"
  57. android:layout_height="60dp"
  58. android:text="3"
  59. android:textSize="20dp"
  60. android:layout_columnWeight="1"
  61. android:background="@drawable/btn"
  62. />
  63. <Button
  64. android:id="@+id/plus"
  65. android:layout_width="wrap_content"
  66. android:layout_height="60dp"
  67. android:text="+"
  68. android:textSize="20dp"
  69. android:layout_columnWeight="1"
  70. android:background="@drawable/btn"
  71. />
  72. <Button
  73. android:id="@+id/four"
  74. android:layout_width="wrap_content"
  75. android:layout_height="60dp"
  76. android:text="4"
  77. android:textSize="20dp"
  78. android:layout_columnWeight="1"
  79. android:background="@drawable/btn"
  80. />
  81. <Button
  82. android:id="@+id/five"
  83. android:layout_width="wrap_content"
  84. android:layout_height="60dp"
  85. android:text="5"
  86. android:textSize="20dp"
  87. android:layout_columnWeight="1"
  88. android:background="@drawable/btn"
  89. />
  90. <Button
  91. android:id="@+id/six"
  92. android:layout_width="wrap_content"
  93. android:layout_height="60dp"
  94. android:text="6"
  95. android:textSize="20dp"
  96. android:layout_columnWeight="1"
  97. android:background="@drawable/btn"
  98. />
  99. <Button
  100. android:id="@+id/min"
  101. android:layout_width="wrap_content"
  102. android:layout_height="60dp"
  103. android:text="-"
  104. android:textSize="20dp"
  105. android:layout_columnWeight="1"
  106. android:background="@drawable/btn"
  107. />
  108. <Button
  109. android:id="@+id/seven"
  110. android:layout_width="wrap_content"
  111. android:layout_height="60dp"
  112. android:text="7"
  113. android:textSize="20dp"
  114. android:layout_columnWeight="1"
  115. android:background="@drawable/btn"
  116. />
  117. <Button
  118. android:id="@+id/eight"
  119. android:layout_width="wrap_content"
  120. android:layout_height="60dp"
  121. android:text="8"
  122. android:textSize="20dp"
  123. android:layout_columnWeight="1"
  124. android:background="@drawable/btn"
  125. />
  126. <Button
  127. android:id="@+id/nine"
  128. android:layout_width="wrap_content"
  129. android:layout_height="60dp"
  130. android:text="9"
  131. android:textSize="20dp"
  132. android:layout_columnWeight="1"
  133. android:background="@drawable/btn"
  134. />
  135. <Button
  136. android:id="@+id/mul"
  137. android:layout_width="wrap_content"
  138. android:layout_height="60dp"
  139. android:text="×"
  140. android:textSize="20dp"
  141. android:layout_columnWeight="1"
  142. android:background="@drawable/btn"
  143. />
  144. <Button
  145. android:id="@+id/spot"
  146. android:layout_width="wrap_content"
  147. android:layout_height="60dp"
  148. android:text="."
  149. android:textSize="20dp"
  150. android:layout_columnWeight="1"
  151. android:background="@drawable/btn"
  152. />
  153. <Button
  154. android:id="@+id/zero"
  155. android:layout_width="wrap_content"
  156. android:layout_height="60dp"
  157. android:text="0"
  158. android:textSize="20dp"
  159. android:layout_columnWeight="1"
  160. android:background="@drawable/btn"
  161. />
  162. <Button
  163. android:id="@+id/equal"
  164. android:layout_width="wrap_content"
  165. android:layout_height="60dp"
  166. android:text="="
  167. android:textSize="20dp"
  168. android:layout_columnWeight="1"
  169. android:background="@drawable/btn"
  170. />
  171. <Button
  172. android:id="@+id/div"
  173. android:layout_width="wrap_content"
  174. android:layout_height="60dp"
  175. android:text="÷"
  176. android:textSize="20dp"
  177. android:layout_columnWeight="1"
  178. android:background="@drawable/btn"
  179. />
  180. </GridLayout>
  181. </LinearLayout>

btn.xml(按钮的样式)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:state_pressed="true" android:drawable="@drawable/btn_pink_bg"/>
  4. <item android:drawable="@drawable/btn_pink"/>
  5. </selector>

btn_pink.xml(按钮点击前)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <stroke
  5. android:width="2dp"
  6. android:color="#ff9999" />
  7. <corners
  8. android:radius="5dp"/>
  9. </shape>

btn_pink_bg.xml(按钮点击后)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="#ff9999"/>
  5. <stroke
  6. android:width="2dp"
  7. android:color="#ff9999" />
  8. <corners
  9. android:radius="5dp"/>
  10. </shape>

效果如下

JAVA代码部分

MainActivity.java

  1. package com.example.a1108;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.EditText;
  8. import java.util.Date;
  9. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  10. EditText result;
  11. // 定义数字按钮
  12. Button zero,one,two,three,four,five,six,seven,eight,nine,spot;
  13. // 定义加减乘除按钮
  14. Button plus,min,mul,div;
  15. // 定义等号按钮
  16. Button equals;
  17. // 标识符,标识运算完成
  18. Boolean clr_flag=false;
  19. // 清除按钮
  20. Button cls;
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25. result=(EditText) findViewById(R.id.result);
  26. zero=findViewById(R.id.zero);
  27. one=findViewById(R.id.one);
  28. two=findViewById(R.id.two);
  29. three=findViewById(R.id.three);
  30. four=findViewById(R.id.four);
  31. five=findViewById(R.id.five);
  32. six=findViewById(R.id.six);
  33. seven=findViewById(R.id.seven);
  34. eight=findViewById(R.id.eight);
  35. nine=findViewById(R.id.nine);
  36. spot=findViewById(R.id.spot);
  37. zero.setOnClickListener(this);
  38. one.setOnClickListener(this);
  39. two.setOnClickListener(this);
  40. three.setOnClickListener(this);
  41. four.setOnClickListener(this);
  42. five.setOnClickListener(this);
  43. six.setOnClickListener(this);
  44. seven.setOnClickListener(this);
  45. eight.setOnClickListener(this);
  46. nine.setOnClickListener(this);
  47. spot.setOnClickListener(this);
  48. plus=findViewById(R.id.plus);
  49. min=findViewById(R.id.min);
  50. mul=findViewById(R.id.mul);
  51. div=findViewById(R.id.div);
  52. plus.setOnClickListener(this);
  53. min.setOnClickListener(this);
  54. mul.setOnClickListener(this);
  55. div.setOnClickListener(this);
  56. equals=findViewById(R.id.equal);
  57. equals.setOnClickListener(this);
  58. cls=findViewById(R.id.cls);
  59. // 为清除设置事件
  60. cls.setOnTouchListener(new View.OnTouchListener() {
  61. Date curDate=new Date(System.currentTimeMillis());
  62. Date endDate=new Date(System.currentTimeMillis());
  63. @Override
  64. public boolean onTouch(View view, MotionEvent motionEvent) {
  65. switch (motionEvent.getAction()){
  66. // 按下
  67. case MotionEvent.ACTION_DOWN:
  68. curDate=new Date((System.currentTimeMillis()));
  69. break;
  70. // 抬起
  71. case MotionEvent.ACTION_UP:
  72. endDate=new Date(System.currentTimeMillis());
  73. long durationMS=endDate.getTime()-curDate.getTime();
  74. if(durationMS>3000)
  75. result.setText("");
  76. break;
  77. }
  78. return true;
  79. }
  80. });
  81. }
  82. @Override
  83. public void onClick(View view) {
  84. // getText()获取的内容是一个对象,所以要转换一下
  85. String str=result.getText().toString();
  86. // 根据当前按钮按下的id进行判断
  87. switch (view.getId())
  88. {
  89. case R.id.zero:
  90. case R.id.one:
  91. case R.id.two:
  92. case R.id.three:
  93. case R.id.four:
  94. case R.id.five:
  95. case R.id.six:
  96. case R.id.seven:
  97. case R.id.eight:
  98. case R.id.nine:
  99. case R.id.spot:
  100. // 如果标识符为真,让值为空
  101. if(clr_flag)
  102. str="";
  103. // 把现在的内容追加上,现在的内容来自于按钮的文本
  104. // 按钮这个view对象先转换为Button
  105. result.setText(str+((Button)view).getText());
  106. clr_flag=false;
  107. break;
  108. case R.id.plus:
  109. case R.id.min:
  110. case R.id.mul:
  111. case R.id.div:
  112. // 如果标识符为真,让值为空
  113. if(clr_flag)
  114. str="";
  115. if(str.contains("+")||str.contains("-")||str.contains("×")||str.contains("÷"))
  116. // 从起始位置开始,我们只要运算符之前的内容
  117. str=str.substring(0,str.indexOf(" "));
  118. // 所以在运算符的前面和后面都追加一个“ ”
  119. result.setText(str+" "+((Button)view).getText()+" ");
  120. clr_flag=false;
  121. break;
  122. case R.id.equal:
  123. getResult();
  124. break;
  125. }
  126. }
  127. // 点了等号后
  128. private void getResult(){
  129. clr_flag=true;
  130. // 获取到字符串
  131. String exp=result.getText().toString();
  132. // 按照空格分隔字符串,形成字符串数组,第一个元素是左侧操作数,第二个元素是运算符,第三个元素是右侧操作数
  133. String [] exp_arr=exp.split(" ");
  134. // 定义结果
  135. double cnt=0;
  136. // 定义操作数
  137. double d1=Double.parseDouble(exp_arr[0]);
  138. double d2=Double.parseDouble(exp_arr[2]);
  139. // 判断运算符
  140. if(exp_arr[1].equals("+"))
  141. cnt=d1+d2;
  142. else if(exp_arr[1].equals("-"))
  143. cnt=d1-d2;
  144. else if(exp_arr[1].equals("×"))
  145. cnt=d1*d2;
  146. else if(exp_arr[1].equals("÷"))
  147. cnt=d1/d2;
  148. // 设置结果
  149. result.setText(String.valueOf(cnt));
  150. }
  151. }

 注释都写在里面了

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

闽ICP备14008679号