当前位置:   article > 正文

Android Studio 简易计算机源码_android studio计算机代码

android studio计算机代码

效果图:

 

源码

布局样式:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="horizontal"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:baselineAligned="false"
  7. android:background="@color/grey"
  8. android:columnCount="4"
  9. android:rowCount="6">
  10. <TextView
  11. android:id="@+id/textview_01"
  12. android:layout_columnSpan="4"
  13. android:layout_gravity="fill"
  14. android:gravity="right"
  15. android:layout_marginLeft="5dp"
  16. android:layout_marginRight="5dp"
  17. android:background="@color/grey"
  18. android:paddingRight="20dp"
  19. android:paddingTop="40dp"
  20. android:text="0"
  21. android:layout_columnWeight="1"
  22. android:textSize="88sp">
  23. </TextView>
  24. <Button
  25. android:id="@+id/button_return"
  26. android:layout_columnSpan="2"
  27. android:gravity="center"
  28. android:layout_columnWeight="1"
  29. android:layout_rowWeight="1"
  30. android:layout_margin="2dp"
  31. android:background="@color/white"
  32. android:textSize="30dp"
  33. android:textStyle="bold"
  34. android:text="回退" />
  35. <Button
  36. android:id="@+id/button_clear"
  37. android:layout_columnSpan="2"
  38. android:gravity="center"
  39. android:layout_columnWeight="1"
  40. android:layout_rowWeight="1"
  41. android:layout_margin="2dp"
  42. android:background="@color/white"
  43. android:textSize="30dp"
  44. android:textStyle="bold"
  45. android:text="清空"/>
  46. <Button
  47. android:id="@+id/button_add"
  48. android:layout_columnSpan="1"
  49. android:gravity="center"
  50. android:layout_columnWeight="1"
  51. android:layout_rowWeight="1"
  52. android:layout_margin="1dp"
  53. android:background="@color/white"
  54. android:textSize="50dp"
  55. android:text="+"/>
  56. <Button
  57. android:id="@+id/button_01"
  58. android:layout_columnSpan="1"
  59. android:gravity="center"
  60. android:layout_columnWeight="1"
  61. android:layout_rowWeight="1"
  62. android:layout_margin="1dp"
  63. android:background="@color/white"
  64. android:textSize="50dp"
  65. android:text="1"/>
  66. <Button
  67. android:id="@+id/button_02"
  68. android:layout_columnSpan="1"
  69. android:gravity="center"
  70. android:layout_columnWeight="1"
  71. android:layout_rowWeight="1"
  72. android:layout_margin="1dp"
  73. android:background="@color/white"
  74. android:textSize="50dp"
  75. android:text="2"/>
  76. <Button
  77. android:id="@+id/button_03"
  78. android:layout_columnSpan="1"
  79. android:gravity="center"
  80. android:layout_columnWeight="1"
  81. android:layout_rowWeight="1"
  82. android:layout_margin="1dp"
  83. android:background="@color/white"
  84. android:textSize="50dp"
  85. android:text="3"/>
  86. <Button
  87. android:id="@+id/button_reduce"
  88. android:layout_columnSpan="1"
  89. android:gravity="center"
  90. android:layout_columnWeight="1"
  91. android:layout_rowWeight="1"
  92. android:layout_margin="1dp"
  93. android:background="@color/white"
  94. android:textSize="50dp"
  95. android:text="-"/>
  96. <Button
  97. android:id="@+id/button_04"
  98. android:layout_columnSpan="1"
  99. android:gravity="center"
  100. android:layout_columnWeight="1"
  101. android:layout_rowWeight="1"
  102. android:layout_margin="1dp"
  103. android:background="@color/white"
  104. android:textSize="50dp"
  105. android:text="4"/>
  106. <Button
  107. android:id="@+id/button_05"
  108. android:layout_columnSpan="1"
  109. android:gravity="center"
  110. android:layout_columnWeight="1"
  111. android:layout_rowWeight="1"
  112. android:layout_margin="1dp"
  113. android:background="@color/white"
  114. android:textSize="50dp"
  115. android:text="5"/>
  116. <Button
  117. android:id="@+id/button_06"
  118. android:layout_columnSpan="1"
  119. android:gravity="center"
  120. android:layout_columnWeight="1"
  121. android:layout_rowWeight="1"
  122. android:layout_margin="1dp"
  123. android:background="@color/white"
  124. android:textSize="50dp"
  125. android:text="6"/>
  126. <Button
  127. android:id="@+id/button_ride"
  128. android:layout_columnSpan="1"
  129. android:gravity="center"
  130. android:layout_columnWeight="1"
  131. android:layout_rowWeight="1"
  132. android:layout_margin="1dp"
  133. android:background="@color/white"
  134. android:textSize="50dp"
  135. android:text="*"/>
  136. <Button
  137. android:id="@+id/button_07"
  138. android:layout_columnSpan="1"
  139. android:gravity="center"
  140. android:layout_columnWeight="1"
  141. android:layout_rowWeight="1"
  142. android:layout_margin="1dp"
  143. android:background="@color/white"
  144. android:textSize="50dp"
  145. android:text="7"/>
  146. <Button
  147. android:id="@+id/button_08"
  148. android:layout_columnSpan="1"
  149. android:gravity="center"
  150. android:layout_columnWeight="1"
  151. android:layout_rowWeight="1"
  152. android:layout_margin="1dp"
  153. android:background="@color/white"
  154. android:textSize="50dp"
  155. android:text="8"/>
  156. <Button
  157. android:id="@+id/button_09"
  158. android:layout_columnSpan="1"
  159. android:gravity="center"
  160. android:layout_columnWeight="1"
  161. android:layout_rowWeight="1"
  162. android:layout_margin="1dp"
  163. android:background="@color/white"
  164. android:textSize="50dp"
  165. android:text="9"/>
  166. <Button
  167. android:id="@+id/button_except"
  168. android:layout_columnSpan="1"
  169. android:gravity="center"
  170. android:layout_columnWeight="1"
  171. android:layout_rowWeight="1"
  172. android:layout_margin="1dp"
  173. android:background="@color/white"
  174. android:textSize="50dp"
  175. android:text="/"/>
  176. <Button
  177. android:id="@+id/button_spot"
  178. android:layout_columnSpan="1"
  179. android:gravity="center"
  180. android:layout_columnWeight="1"
  181. android:layout_rowWeight="1"
  182. android:layout_margin="1dp"
  183. android:background="@color/white"
  184. android:textSize="50dp"
  185. android:text="."/>
  186. <Button
  187. android:id="@+id/button_00"
  188. android:layout_columnSpan="1"
  189. android:gravity="center"
  190. android:layout_columnWeight="1"
  191. android:layout_rowWeight="1"
  192. android:layout_margin="1dp"
  193. android:background="@color/white"
  194. android:textSize="50dp"
  195. android:text="0"/>
  196. <Button
  197. android:id="@+id/button_equal"
  198. android:layout_columnSpan="1"
  199. android:gravity="center"
  200. android:layout_columnWeight="1"
  201. android:layout_rowWeight="1"
  202. android:layout_margin="1dp"
  203. android:background="@color/white"
  204. android:textSize="50dp"
  205. android:text="="/>
  206. </GridLayout>

 Java部分源码:

  1. package com.example.pyfcalculator;
  2. import androidx.appcompat.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.text.TextUtils;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. public class MainActivity extends AppCompatActivity implements View.OnClickListener {
  9. TextView textView;
  10. Button button_return,button_00,button_01,button_02,button_03,
  11. button_04,button_05,button_06,button_07,button_08,button_09,button_add,
  12. button_clear,button_equal,button_except,button_reduce,button_ride,button_spot;
  13. @Override
  14. protected void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.activity_main);
  17. initViewAndListener();
  18. }
  19. //注册监听方法
  20. protected void initViewAndListener(){
  21. button_return=findViewById(R.id.button_return);
  22. button_00=findViewById(R.id.button_00);
  23. button_01=findViewById(R.id.button_01);
  24. button_02=findViewById(R.id.button_02);
  25. button_03=findViewById(R.id.button_03);
  26. button_04=findViewById(R.id.button_04);
  27. button_05=findViewById(R.id.button_05);
  28. button_06=findViewById(R.id.button_06);
  29. button_07=findViewById(R.id.button_07);
  30. button_08=findViewById(R.id.button_08);
  31. button_09=findViewById(R.id.button_09);
  32. button_add=findViewById(R.id.button_add);
  33. button_clear=findViewById(R.id.button_clear);
  34. button_equal=findViewById(R.id.button_equal);
  35. button_except=findViewById(R.id.button_except);
  36. button_reduce=findViewById(R.id.button_reduce);
  37. button_ride=findViewById(R.id.button_ride);
  38. button_spot=findViewById(R.id.button_spot);
  39. button_return.setOnClickListener(this);
  40. button_00.setOnClickListener(this);
  41. button_01.setOnClickListener(this);
  42. button_02.setOnClickListener(this);
  43. button_03.setOnClickListener(this);
  44. button_04.setOnClickListener(this);
  45. button_05.setOnClickListener(this);
  46. button_06.setOnClickListener(this);
  47. button_07.setOnClickListener(this);
  48. button_08.setOnClickListener(this);
  49. button_09.setOnClickListener(this);
  50. button_add.setOnClickListener(this);
  51. button_clear.setOnClickListener(this);
  52. button_equal.setOnClickListener(this);
  53. button_except.setOnClickListener(this);
  54. button_reduce.setOnClickListener(this);
  55. button_ride.setOnClickListener(this);
  56. button_spot.setOnClickListener(this);
  57. textView=findViewById(R.id.textview_01);
  58. }
  59. private String num_in_now;//记录当前结果
  60. private boolean flag=false;//记录计算器状态
  61. int now_num=0;
  62. @Override
  63. public void onClick(View view) {
  64. //获取文本框内容
  65. String currentText=textView.getText().toString();
  66. switch (view.getId()){
  67. case R.id.button_00:
  68. if ((textView.getText().toString().equals("0")))
  69. return;
  70. isflag("0");
  71. break;
  72. case R.id.button_01:
  73. isflag("1");
  74. break;
  75. case R.id.button_02:
  76. isflag("2");
  77. break;
  78. case R.id.button_03:
  79. isflag("3");
  80. break;
  81. case R.id.button_04:
  82. isflag("4");
  83. break;
  84. case R.id.button_05:
  85. isflag("5");
  86. break;
  87. case R.id.button_06:
  88. isflag("6");
  89. break;
  90. case R.id.button_07:
  91. isflag("7");
  92. break;
  93. case R.id.button_08:
  94. isflag("8");
  95. break;
  96. case R.id.button_09:
  97. isflag("9");
  98. break;
  99. case R.id.button_add:
  100. // textView.setText(textView.getText()+"+");
  101. now_num++;
  102. if(now_num>1){
  103. equal();
  104. }
  105. isflag("+");
  106. break;
  107. case R.id.button_except:
  108. // textView.setText(textView.getText()+"/");
  109. now_num++;
  110. if(now_num>1){
  111. equal();
  112. }
  113. isflag("/");
  114. break;
  115. case R.id.button_spot:
  116. // textView.setText(textView.getText()+".");
  117. isflag(".");
  118. break;
  119. case R.id.button_reduce:
  120. // textView.setText(textView.getText()+"-");
  121. now_num++;
  122. if(now_num>1){
  123. equal();
  124. }
  125. isflag("-");
  126. break;
  127. case R.id.button_ride:
  128. // textView.setText(textView.getText()+"*");
  129. now_num++;
  130. if(now_num>1){
  131. equal();
  132. }
  133. isflag("*");
  134. break;
  135. case R.id.button_clear:
  136. textView.setText("0");
  137. flag=false;
  138. now_num=0;
  139. break;
  140. case R.id.button_return:
  141. if(TextUtils.isEmpty(textView.getText())){
  142. return;
  143. }
  144. if (currentText.substring(0,currentText.length()-1).length()>0){
  145. textView.setText(currentText.substring(0,currentText.length()-1));
  146. }
  147. else{
  148. textView.setText("0");
  149. flag=false;
  150. now_num=0;
  151. }
  152. break;
  153. case R.id.button_equal:
  154. if(TextUtils.isEmpty(textView.getText())){
  155. return;
  156. }
  157. equal();
  158. flag=false;
  159. now_num=0;
  160. break;
  161. }
  162. }
  163. private void equal(){
  164. textView.setText(operation(textView.getText().toString()));
  165. }
  166. //运算方法
  167. private String operation(String string){
  168. int index;
  169. double num_1,num_2;
  170. if (string.contains("+")){
  171. index=string.indexOf("+");
  172. num_1= Double.parseDouble(string.substring(0,index));
  173. num_2= Double.parseDouble(string.substring(index+1,string.length()));
  174. return num_1+num_2+"";
  175. }
  176. if (string.contains("-")){
  177. index=string.indexOf("-");
  178. if(index==0){
  179. String string_2=string.substring(1,string.length());
  180. int index_2=string_2.indexOf("-");
  181. num_1= Double.parseDouble(string_2.substring(0,index_2));
  182. num_2= Double.parseDouble(string_2.substring(index_2+1,string_2.length()));
  183. return (-(num_1+num_2))+"";
  184. }else{
  185. num_1= Double.parseDouble(string.substring(0,index));
  186. num_2= Double.parseDouble(string.substring(index+1,string.length()));
  187. return num_1-num_2+"";
  188. }
  189. }
  190. if (string.contains("*")){
  191. index=string.indexOf("*");
  192. num_1= Double.parseDouble(string.substring(0,index));
  193. num_2= Double.parseDouble(string.substring(index+1,string.length()));
  194. return num_1*num_2+"";
  195. }
  196. if (string.contains("/")){
  197. index=string.indexOf("/");
  198. num_1= Double.parseDouble(string.substring(0,index));
  199. num_2= Double.parseDouble(string.substring(index+1,string.length()));
  200. return num_1/num_2+"";
  201. }
  202. return "error!!!";
  203. }
  204. //判断0要不要被替换 看flag的状态 输入数字
  205. private void isflag(String info){
  206. if (flag){
  207. textView.setText(textView.getText()+info);
  208. }else{
  209. if(info.equals(".")||info.equals("+")||info.equals("-")||info.equals("*")||info.equals("/")){
  210. textView.setText(textView.getText()+info);
  211. }else {
  212. textView.setText(info);
  213. }
  214. flag=true;
  215. }
  216. }
  217. }

其余资源没有太大难度,就不一展现了!!! 

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