• 用android如何实现计算机计算功能


    一.新建一个项目

    步骤:

    1.新建项目

    2.选择 

     

    二.用户界面构建 

    找到项目的res的下面layout里面的activity.xml文件进行约束布局界面构建。

    activity.xml代码如下:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout 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. <GridLayout
    9. android:id="@+id/gridLayout"
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:orientation="vertical"
    13. tools:ignore="MissingConstraints">
    14. <EditText
    15. android:id="@+id/ed_input"
    16. android:layout_width="match_parent"
    17. android:layout_height="100dp"
    18. android:hint="输入框" />
    19. <EditText
    20. android:id="@+id/ed_output"
    21. android:layout_width="match_parent"
    22. android:layout_height="100dp"
    23. android:hint="输出口" />
    24. </GridLayout>
    25. <GridLayout
    26. android:layout_width="424dp"
    27. android:layout_height="329dp"
    28. android:columnCount="4"
    29. android:rowCount="4"
    30. app:layout_constraintBottom_toBottomOf="parent"
    31. app:layout_constraintTop_toBottomOf="@+id/gridLayout"
    32. tools:ignore="MissingConstraints">
    33. <Button
    34. android:id="@+id/buttonc"
    35. android:layout_width="180dp"
    36. android:layout_height="60dp"
    37. android:layout_columnSpan="2"
    38. android:backgroundTint="#a6a6a6"
    39. android:text="c" />
    40. <Button
    41. android:id="@+id/buttondel"
    42. android:layout_width="90dp"
    43. android:layout_height="60dp"
    44. android:layout_columnSpan="1"
    45. android:backgroundTint="#a6a6a6"
    46. android:text="DEL" />
    47. <Button
    48. android:id="@+id/buttonchu"
    49. android:layout_width="90dp"
    50. android:layout_height="60dp"
    51. android:backgroundTint="#ff9500"
    52. android:text="/" />
    53. <Button
    54. android:id="@+id/button7"
    55. android:layout_width="90dp"
    56. android:layout_height="60dp"
    57. android:backgroundTint="#333333"
    58. android:text="7" />
    59. <Button
    60. android:id="@+id/button8"
    61. android:layout_width="90dp"
    62. android:layout_height="60dp"
    63. android:backgroundTint="#333333"
    64. android:text="8" />
    65. <Button
    66. android:id="@+id/button9"
    67. android:layout_width="90dp"
    68. android:layout_height="60dp"
    69. android:backgroundTint="#333333"
    70. android:text="9" />
    71. <Button
    72. android:id="@+id/buttoncheng"
    73. android:layout_width="90dp"
    74. android:layout_height="60dp"
    75. android:backgroundTint="#ff9500"
    76. android:text="*" />
    77. <Button
    78. android:id="@+id/button4"
    79. android:layout_width="90dp"
    80. android:layout_height="60dp"
    81. android:backgroundTint="#333333"
    82. android:text="4" />
    83. <Button
    84. android:id="@+id/button5"
    85. android:layout_width="90dp"
    86. android:layout_height="60dp"
    87. android:backgroundTint="#333333"
    88. android:text="5" />
    89. <Button
    90. android:id="@+id/button6"
    91. android:layout_width="90dp"
    92. android:layout_height="60dp"
    93. android:backgroundTint="#333333"
    94. android:text="6" />
    95. <Button
    96. android:id="@+id/buttonjian"
    97. android:layout_width="90dp"
    98. android:layout_height="60dp"
    99. android:backgroundTint="#ff9500"
    100. android:text="-" />
    101. <Button
    102. android:id="@+id/button1"
    103. android:layout_width="90dp"
    104. android:layout_height="60dp"
    105. android:backgroundTint="#333333"
    106. android:text="1" />
    107. <Button
    108. android:id="@+id/button2"
    109. android:layout_width="90dp"
    110. android:layout_height="60dp"
    111. android:backgroundTint="#333333"
    112. android:text="2" />
    113. <Button
    114. android:id="@+id/button3"
    115. android:layout_width="90dp"
    116. android:layout_height="60dp"
    117. android:backgroundTint="#333333"
    118. android:text="3" />
    119. <Button
    120. android:id="@+id/buttonjia"
    121. android:layout_width="90dp"
    122. android:layout_height="60dp"
    123. android:backgroundTint="#ff9500"
    124. android:text="+" />
    125. <Button
    126. android:id="@+id/buttonyuliu"
    127. android:layout_width="90dp"
    128. android:layout_height="60dp"
    129. android:backgroundTint="#333333"
    130. android:text="预留" />
    131. <Button
    132. android:id="@+id/button0"
    133. android:layout_width="90dp"
    134. android:layout_height="60dp"
    135. android:backgroundTint="#333333"
    136. android:text="0" />
    137. <Button
    138. android:id="@+id/buttondian"
    139. android:layout_width="90dp"
    140. android:layout_height="60dp"
    141. android:backgroundTint="#333333"
    142. android:text="." />
    143. <Button
    144. android:id="@+id/buttondeng"
    145. android:layout_width="90dp"
    146. android:layout_height="60dp"
    147. android:backgroundTint="#ff9500"
    148. android:text="=" />
    149. </GridLayout>
    150. </androidx.constraintlayout.widget.ConstraintLayout>

    三.设置实现计算功能的关键 

    找到Java里面的MainActiviy.java写入实现代码。

    MainActiviy.java代码如下:

    1. package com.example.myapplication2;
    2. import androidx.appcompat.app.AppCompatActivity;
    3. import android.os.Bundle;
    4. import android.view.View;
    5. import android.widget.Button;
    6. import android.widget.EditText;
    7. public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    8. private Button mbutton1,mbutton2,mbutton3,mbutton4,mbutton5,mbutton6,mbutton7,mbutton8,mbutton9,mbutton0,
    9. mbuttonc,mbuttondel,mbuttonyuliu,mbuttonjia,mbuttonjian,
    10. mbuttoncheng,mbuttonchu,mbuttondian,mbuttondeng;
    11. private EditText edinput,edoutput;
    12. private boolean deng_flag=false;
    13. @Override
    14. protected void onCreate(Bundle savedInstanceState) {
    15. super.onCreate(savedInstanceState);
    16. setContentView(R.layout.activity_main);
    17. //数字0-9
    18. mbutton1=findViewById(R.id.button1);
    19. mbutton2=findViewById(R.id.button2);
    20. mbutton3=findViewById(R.id.button3);
    21. mbutton4=findViewById(R.id.button4);
    22. mbutton5=findViewById(R.id.button5);
    23. mbutton6=findViewById(R.id.button6);
    24. mbutton7=findViewById(R.id.button7);
    25. mbutton8=findViewById(R.id.button8);
    26. mbutton9=findViewById(R.id.button9);
    27. mbutton0=findViewById(R.id.button0);
    28. //c、del、预留
    29. mbuttonc=findViewById(R.id.buttonc);
    30. mbuttondel=findViewById(R.id.buttondel);
    31. mbuttonyuliu=findViewById(R.id.buttonyuliu);
    32. //加减乘除、点、等号
    33. mbuttonjia=findViewById(R.id.buttonjia);
    34. mbuttonjian=findViewById(R.id.buttonjian);
    35. mbuttoncheng=findViewById(R.id.buttoncheng);
    36. mbuttonchu=findViewById(R.id.buttonchu);
    37. mbuttondeng=findViewById(R.id.buttondeng);
    38. mbuttondian=findViewById(R.id.buttondian);
    39. //输入输出
    40. edinput=findViewById(R.id.ed_input);
    41. edoutput=findViewById(R.id.ed_output);
    42. //设置按钮监听
    43. //0-9
    44. mbutton0.setOnClickListener(this);
    45. mbutton1.setOnClickListener(this);
    46. mbutton2.setOnClickListener(this);
    47. mbutton3.setOnClickListener(this);
    48. mbutton4.setOnClickListener(this);
    49. mbutton5.setOnClickListener(this);
    50. mbutton6.setOnClickListener(this);
    51. mbutton7.setOnClickListener(this);
    52. mbutton8.setOnClickListener(this);
    53. mbutton9.setOnClickListener(this);
    54. //c、del、预留
    55. mbuttonc.setOnClickListener(this);
    56. mbuttondel.setOnClickListener(this);
    57. mbuttonyuliu.setOnClickListener(this);
    58. //加减乘除、点、等号
    59. mbuttonjia.setOnClickListener(this);
    60. mbuttonjian.setOnClickListener(this);
    61. mbuttoncheng.setOnClickListener(this);
    62. mbuttonchu.setOnClickListener(this);
    63. mbuttondeng.setOnClickListener(this);
    64. mbuttondian.setOnClickListener(this);
    65. }
    66. @Override
    67. public void onClick(View view)
    68. {
    69. String input = edinput.getText().toString();
    70. String output = edoutput.getText().toString();
    71. switch (view.getId()){
    72. //0-9
    73. case R.id.button0:
    74. case R.id.button1:
    75. case R.id.button2:
    76. case R.id.button3:
    77. case R.id.button4:
    78. case R.id.button5:
    79. case R.id.button6:
    80. case R.id.button7:
    81. case R.id.button8:
    82. case R.id.button9:
    83. case R.id.buttondian:
    84. if(deng_flag){
    85. deng_flag=false;
    86. edinput.setText(null);
    87. edinput.setText(((Button) view).getText());
    88. }else {
    89. edinput.setText(input+((Button) view).getText());
    90. }
    91. edinput.setText(input+((Button) view).getText());
    92. break;
    93. //c
    94. case R.id.buttonc:
    95. edinput.setText(null);
    96. edoutput.setText(null);
    97. break;
    98. //del
    99. case R.id.buttondel:
    100. if (deng_flag){
    101. deng_flag=false;
    102. edinput.setText("");
    103. }else if(input !=null&&!input.equals("")){
    104. edinput.setText(input.substring(0,input.length()-1));
    105. }
    106. break;
    107. //预留
    108. case R.id.buttonyuliu:
    109. break;
    110. //加减乘除
    111. case R.id.buttonjia:
    112. case R.id.buttonjian:
    113. case R.id.buttoncheng:
    114. case R.id.buttonchu:
    115. edinput.setText(input+" "+((Button) view).getText()+" ");
    116. break;
    117. //等号
    118. case R.id.buttondeng:
    119. // edinput.setText(input+((Button) view).getText());
    120. // break;
    121. getResult();
    122. }
    123. }
    124. private void getResult() {
    125. try{
    126. String input = edinput.getText().toString();
    127. int iResult=0;
    128. double dResult=0;
    129. String cw="错误";
    130. String s1,s2,op;//数字,数字,操作符 s1"4" op"*" s2"5"
    131. s1=input.substring(0,input.indexOf(" "));
    132. op=input.substring(input.indexOf(" ")+1,input.indexOf(" ")+2);
    133. s2=input.substring(input.indexOf(" ")+3);
    134. double d1,d2;
    135. d1=Double.parseDouble(s1);
    136. d2=Double.parseDouble(s2);
    137. if(op.equals("+")){//
    138. dResult=d1+d2;
    139. // edoutput.setText(dResult+"");
    140. }else if(op.equals("-")){//
    141. dResult=d1-d2;
    142. } else if (op.equals("*")){//
    143. dResult=d1*d2;
    144. } else if (op.equals("/")) {//
    145. if(d2==0){
    146. edoutput.setText(cw+"");
    147. } else if (d1==0) {
    148. dResult=0;
    149. } else {
    150. dResult=d1/d2;
    151. }
    152. }
    153. if(!input.equals(".")&&!input.equals("/")){
    154. iResult=(int)dResult;
    155. edoutput.setText(iResult+"");
    156. }
    157. edoutput.setText(dResult+"");
    158. }catch (Exception e){
    159. System.out.println(e);
    160. }
    161. }
    162. }

    运行结果如下:

    输入计算值,得出结果

     

  • 相关阅读:
    (51单片机)第三章-数码管显示原理及应用实现-中断
    SpringBoot——全局异常处理
    使用C++界面框架ImGUI开发一个简单程序
    js中的this关键字
    Singleton Pattern 单例模式简介与 C# 示例【创建型】【设计模式来了】
    SpringCloud Gateway 自定义Filter用代码怎么写?别再说我只会配置基本Filter了,来学学如何定制Filter过滤器
    设计模式之单例和原型
    RabbitMQ——02
    利用无线技术实现分散传感器信号远程集中控制
    jvm双亲委派机制详解
  • 原文地址:https://blog.csdn.net/m0_73490019/article/details/139697581