当前位置:   article > 正文

Android Studio的计算器简单实现加减乘除_安卓计算器里的加减乘除怎么触发

安卓计算器里的加减乘除怎么触发

学习目标:

简单计算器的加减乘除


学习内容:

提示:这里可以添加要学的内容

例如:

  1. 搭建 Java 开发环境
    Android Studio jdk1.8
    在这里插入图片描述

  2. 掌握 Java 基本语法
    Switch选择语句

  3. 掌握条件语句

switch (op){
                    case "+":
                        result=num1+num2;
                        break;
                    case "-":
                        result=num1-num2;
                        break;
                    case "*":
                        result=num1*num2;
                        break;
                    case "/":
                        if (num2==0){
                            break;
                        }
                        result=num1/num2;
                        break;
                    default:
                        result=0.0;
                        break;
                }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

学习时间:

  • 周六下午 6点-晚上7点

学习产出:

XML文件
/导入背景图片/
android:background=“@drawable/girl”

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

   	android:background="@drawable/girl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

  <LinearLayout

      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:layout_weight="2.5"
      android:orientation="horizontal"
      >
      <EditText
          android:id="@+id/result"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:hint="0"
          android:gravity="right"
          android:layout_gravity="bottom"
          android:textStyle="bold"
          android:textSize="50sp"
          ></EditText>
  </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="7"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>
            <Button
                android:id="@+id/btn_8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="8"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>

            <Button
                android:id="@+id/btn_9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="9"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>

            <Button
                android:id="@+id/btn_sum"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button

                android:id="@+id/btn_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="4"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>
            <Button
                android:id="@+id/btn_5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="5"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>

            <Button
                android:id="@+id/btn_6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="6"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>

            <Button
                android:id="@+id/btn_sub"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="-"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="1"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>
            <Button
                android:id="@+id/btn_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="2"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>

            <Button
                android:id="@+id/btn_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="3"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>

            <Button
                android:id="@+id/btn_mult"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="*"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>


        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button
                android:id="@+id/btn_0"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="0"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>
            <Button
                android:id="@+id/btn_c"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="C"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>

            <Button
                android:id="@+id/btn_dot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="."
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>
            <Button
                android:id="@+id/btn_div"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="/"
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>
            <Button
                android:id="@+id/btn_equ"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="="
                android:textSize="35sp"
                android:layout_weight="1"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"/>


        </LinearLayout>
    </LinearLayout>

</LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227

主活动

package com.example.calculator;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btn_0,btn_1,btn_2,btn_3,btn_4,btn_5,btn_6,btn_7,btn_8,btn_9;
    private Button btn_dot,btn_sum,btn_sub,btn_mult,btn_div,btn_clr,btn_equ;
    private TextView cal_result;
    private boolean isClickequ=false;
    private String strnum1,strnum2="";
    private double num1,num2=0.0;
    private String op="";//操作符,标记
    private double result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        cal_result=findViewById(R.id.result);
        btn_0=findViewById(R.id.btn_0);
        btn_1=findViewById(R.id.btn_1);
        btn_2=findViewById(R.id.btn_2);
        btn_3=findViewById(R.id.btn_3);
        btn_4=findViewById(R.id.btn_4);
        btn_5=findViewById(R.id.btn_5);
        btn_6=findViewById(R.id.btn_6);
        btn_7=findViewById(R.id.btn_7);
        btn_8=findViewById(R.id.btn_8);
        btn_9=findViewById(R.id.btn_9);
        btn_dot=findViewById(R.id.btn_dot);
        btn_sum=findViewById(R.id.btn_sum);
        btn_sub=findViewById(R.id.btn_sub);
        btn_mult=findViewById(R.id.btn_mult);
        btn_div=findViewById(R.id.btn_div);
        btn_clr=findViewById(R.id.btn_c);
        btn_equ=findViewById(R.id.btn_equ);


            btn_0.setOnClickListener(this);
            btn_1.setOnClickListener(this);
            btn_2.setOnClickListener(this);
            btn_3.setOnClickListener(this);
            btn_4.setOnClickListener(this);
            btn_5.setOnClickListener(this);
            btn_6.setOnClickListener(this);
            btn_7.setOnClickListener(this);
            btn_8.setOnClickListener(this);
            btn_9.setOnClickListener(this);
            btn_dot.setOnClickListener(this);
            btn_sum.setOnClickListener(this);
            btn_sub.setOnClickListener(this);
            btn_mult.setOnClickListener(this);
            btn_div.setOnClickListener(this);
            btn_clr.setOnClickListener(this);
            btn_equ.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_0:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"0");
                break;
            case R.id.btn_1:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"1");
                break;
            case R.id.btn_2:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"2");
                break;

            case R.id.btn_3:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"3");
                break;
            case R.id.btn_4:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"4");
                break;
            case R.id.btn_5:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"5");
                break;
            case R.id.btn_6:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"6");
                break;
            case R.id.btn_7:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"7");
                break;
            case R.id.btn_8:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"8");
                break;
            case R.id.btn_9:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+"9");
                break;
            case R.id.btn_c:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText("");
                break;
            case R.id.btn_dot:
                if (isClickequ){
                    cal_result.setText("");
                    isClickequ =false;
                }
                cal_result.setText(cal_result.getText().toString()+".");
                break;
            case R.id.btn_sum:
                strnum1=cal_result.getText().toString();//存数值
                if (strnum1.equals("")){
                    break;
                }
               num1=Double.parseDouble(strnum1);
                cal_result.setText("");//清空刚刚存数值
                op="+";
                isClickequ=false;
                break;
            case R.id.btn_sub:
                strnum1=cal_result.getText().toString();//存数值
                if (strnum1.equals("")){
                    break;
                }
                num1=Double.parseDouble(strnum1);
                cal_result.setText("");//清空刚刚存数值
                op="-";
                isClickequ=false;
                break;
            case R.id.btn_mult:
                strnum1=cal_result.getText().toString();//存数值
                if (strnum1.equals("")){
                    break;
                }
                num1=Double.parseDouble(strnum1);
                cal_result.setText("");//清空刚刚存数值
                op="*";
                isClickequ=false;
                break;
            case R.id.btn_div:
                strnum1=cal_result.getText().toString();//存数值
                if (strnum1.equals("")){
                    break;
                }
                num1=Double.parseDouble(strnum1);
                cal_result.setText("");//清空刚刚存数值
                op="/";
                isClickequ=false;
                break;
            case R.id.btn_equ:
                strnum2=cal_result.getText().toString();//存操作数值2
                if (strnum2.equals("")){
                    break;
                }
                num2=Double.parseDouble(strnum2);
                cal_result.setText("");//清空刚刚存数值
                switch (op){
                    case "+":
                        result=num1+num2;
                        break;
                    case "-":
                        result=num1-num2;
                        break;
                    case "*":
                        result=num1*num2;
                        break;
                    case "/":
                        if (num2==0){
                            break;
                        }
                        result=num1/num2;
                        break;
                    default:
                        result=0.0;
                        break;
                }

                cal_result.setText(result+"");
                op="";
                isClickequ=true;

        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230

运行界面
在这里插入图片描述

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

闽ICP备14008679号