当前位置:   article > 正文

Android——计算器(Calculator)_android计算器

android计算器

Android——计算器(Calculator)

简述

步骤:
1:输入一个数储存在变量num1中
2:选择 + - * /任意一个运算符
3:输入另一个数储存在变量num2中
4:并把运算结果保存到变量result中并显示
//如果被除数为0则结果默认显示为0
//在把String类型转化成double时有可能出现NumberFormatException异常,tyr,catch一下就可以了
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

效果图

activity_main.xml

布局代码

<?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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:orientation="vertical">
    <TextView
            android:id="@+id/Show"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:textSize="50sp"
            android:hint="0"
           />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
        android:id="@+id/Clear"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:text="Reset"
        android:textAllCaps="false"
        android:textSize="20sp"/>
        <Button
                android:id="@+id/Number7"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="7"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Clear"/>
        <Button
                android:id="@+id/Number8"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="8"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Clear"
                android:layout_marginLeft="100dp"/>
        <Button
                android:id="@+id/Number9"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="9"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Clear"
                android:layout_marginLeft="200dp"/>
        <Button
                android:id="@+id/Add"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="+"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Clear"
                android:layout_marginLeft="300dp"/>
        <Button
                android:id="@+id/Number4"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="4"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number7"/>
        <Button
                android:id="@+id/Number5"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="5"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number8"
                android:layout_marginLeft="100dp"/>
        <Button
                android:id="@+id/Number6"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="6"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number9"
                android:layout_marginLeft="200dp"/>
        <Button
                android:id="@+id/Subtract"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="-"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Add"
                android:layout_marginLeft="300dp"/>
        <Button
                android:id="@+id/Number1"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="1"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number5"/>
        <Button
                android:id="@+id/Number2"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="2"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number6"
                android:layout_marginLeft="100dp"/>
        <Button
                android:id="@+id/Number3"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="3"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number6"
                android:layout_marginLeft="200dp"/>
        <Button
                android:id="@+id/Multiply"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="*"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Subtract"
                android:layout_marginLeft="300dp"/>
        <Button
                android:id="@+id/Point"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="."
                android:textAllCaps="false"
                android:textSize="30sp"
                android:layout_below="@+id/Number1"/>
        <Button
                android:id="@+id/Number0"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="0"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number2"
                android:layout_marginLeft="100dp"/>
        <Button
                android:id="@+id/Equal"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="="
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Number3"
                android:layout_marginLeft="200dp"/>
        <Button
                android:id="@+id/Divide"
                android:layout_width="100dp"
                android:layout_height="70dp"
                android:text="/"
                android:textAllCaps="false"
                android:textSize="20sp"
                android:layout_below="@+id/Multiply"
                android:layout_marginLeft="300dp"/>

    </RelativeLayout>

</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

功能代码

public class MainActivity extends AppCompatActivity {
    private TextView DisplayBox;
    private Button Number0, Number1,Number2,Number3,Number4,Number5,Number6,Number7,Number8,Number9;
    private Button Add,Subtract,Multiply,Divide,Equal;// + - * / =
    private Button Reset ,Point;
    double num1 = 0,num2 = 0;//第一次输入的数和第二次输入的数
    double result = 0;//存放运算结果
    int Operator;//用于判断是那个运算符

    //private String str1,str2;

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

        DisplayBox = (TextView) findViewById(R.id.Show);

        Number0 = (Button) findViewById(R.id.Number0);
        Number1 = (Button) findViewById(R.id.Number1);
        Number2 = (Button) findViewById(R.id.Number2);
        Number3 = (Button) findViewById(R.id.Number3);
        Number4 = (Button) findViewById(R.id.Number4);
        Number5 = (Button) findViewById(R.id.Number5);
        Number6 = (Button) findViewById(R.id.Number6);
        Number7 = (Button) findViewById(R.id.Number7);
        Number8 = (Button) findViewById(R.id.Number8);
        Number9 = (Button) findViewById(R.id.Number9);

        Add = (Button) findViewById(R.id.Add);
        Subtract = (Button) findViewById(R.id.Subtract);
        Multiply = (Button) findViewById(R.id.Multiply);
        Divide = (Button) findViewById(R.id.Divide);
        Equal = (Button) findViewById(R.id.Equal);

       Reset = (Button) findViewById(R.id.Clear);
       Point = (Button) findViewById(R.id.Point);

        SetListen();

    }
    public void SetListen(){
        OnClick onClick = new OnClick();
        Number0.setOnClickListener(onClick);
        Number1.setOnClickListener(onClick);
        Number2.setOnClickListener(onClick);
        Number3.setOnClickListener(onClick);
        Number4.setOnClickListener(onClick);
        Number5.setOnClickListener(onClick);
        Number6.setOnClickListener(onClick);
        Number7.setOnClickListener(onClick);
        Number8.setOnClickListener(onClick);
        Number9.setOnClickListener(onClick);
        Point.setOnClickListener(onClick);

        Add.setOnClickListener(onClick);
        Subtract.setOnClickListener(onClick);
        Multiply.setOnClickListener(onClick);
        Divide.setOnClickListener(onClick);

        Equal.setOnClickListener(onClick);
        Reset.setOnClickListener(onClick);

    }
    class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
           switch (v.getId())
           {
                   case R.id.Clear:
                       DisplayBox.setText(null);
                       break;
                   case R.id.Number0:
                       String str0 = DisplayBox.getText().toString().trim();
                       str0 += "0";
                       DisplayBox.setText(str0);
                       break;
                   case R.id.Number1:
                       String str1 = DisplayBox.getText().toString().trim();
                       str1 += "1";
                       DisplayBox.setText(str1);
                       break;
                   case R.id.Number2:
                       String str2 = DisplayBox.getText().toString().trim();
                       str2 += "2";
                       DisplayBox.setText(str2);
                       break;
                   case R.id.Number3:
                       String str3 = DisplayBox.getText().toString().trim();
                       str3 += "3";
                       DisplayBox.setText(str3);
                       break;
                   case R.id.Number4:
                       String str4 = DisplayBox.getText().toString().trim();
                       str4 += "4";
                       DisplayBox.setText(str4);
                       break;
                   case R.id.Number5:
                       String str5 = DisplayBox.getText().toString().trim();
                       str5 += "5";
                       DisplayBox.setText(str5);
                       break;
                   case R.id.Number6:
                       String str6 = DisplayBox.getText().toString().trim();
                       str6 += "6";
                       DisplayBox.setText(str6);
                       break;
                   case R.id.Number7:
                       String str7 = DisplayBox.getText().toString().trim();
                       str7 += "7";
                       DisplayBox.setText(str7);
                       break;
                   case R.id.Number8:
                       String str8 = DisplayBox.getText().toString().trim();
                       str8 += "8";
                       DisplayBox.setText(str8);
                       break;
                   case R.id.Number9:
                       String str9 = DisplayBox.getText().toString().trim();
                       str9 += "9";
                       DisplayBox.setText(str9);
                       break;
                   case R.id.Point:
                       String point = DisplayBox.getText().toString().trim();
                       point += ".";
                       DisplayBox.setText(point);
                       break;

                    case R.id.Add:
                       String add = DisplayBox.getText().toString().trim();
                       if (add.equals(null))
                      {
                       return;
                      }
                        try {
                            num1 = Double.valueOf(add);
                        }catch (NumberFormatException e)
                        {
                            e.printStackTrace();
                        }
                      add += "+";
                      DisplayBox.setText(null);
                      Operator = 1;
                      break;
               case R.id.Subtract:
                   String subtract = DisplayBox.getText().toString().trim();
                   if (subtract.equals(null))
                   {
                       return;
                   }
                   try {
                       num1 = Double.valueOf(subtract);
                   }catch (NumberFormatException e)
                   {
                       e.printStackTrace();
                   }
                   subtract += "-";
                   DisplayBox.setText(null);
                   Operator = 2;
                   break;
               case R.id.Multiply:
                   String multiply = DisplayBox.getText().toString().trim();
                   if (multiply.equals(null))
                   {
                       return;
                   }
                   try {
                       num1 = Double.valueOf(multiply);
                   }catch (NumberFormatException e)
                   {
                       e.printStackTrace();
                   }
                   multiply += "*";
                   DisplayBox.setText(null);
                   Operator = 3;
                   break;
               case R.id.Divide:
                   String divide = DisplayBox.getText().toString().trim();
                   if (divide.equals(null))
                   {
                       return;
                   }
                   try {
                       num1 = Double.valueOf(divide);
                   }catch (NumberFormatException e)
                   {
                       e.printStackTrace();
                   }
                   divide += "/";
                   DisplayBox.setText(null);
                   Operator = 4;
                   break;
               case R.id.Equal:
                   String equal = DisplayBox.getText().toString().trim();
                   try {
                       num2 = Double.valueOf(equal);
                   }catch (NumberFormatException e)
                   {
                       e.printStackTrace();
                   }
                   DisplayBox.setText(equal);
                   switch (Operator)
                   {
                       case 1:
                           result = num1 + num2;
                           DisplayBox.setText(num1 + "+" + num2 + "=" +result);
                           break;
                       case 2:
                           result = num1 - num2;
                           DisplayBox.setText(num1 + "-" + num2 + "=" +result);
                           break;
                       case 3:
                           result = num1 * num2;
                           DisplayBox.setText(num1 + "*" + num2 + "=" +result);
                           break;
                       case 4:
                           if (num2 == 0)
                           {
                               DisplayBox.setText("0");
                           }
                           else {
                               result = num1 / num2;
                               DisplayBox.setText(num1 + "/" + num2 + "=" + result);
                           }
                           break;

                   }
                   break;
               default:
                   result = 0;
                   break;
           }
        }
    }
  • 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
  • 231
  • 232
  • 233
  • 234
  • 235

测例效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

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

闽ICP备14008679号