当前位置:   article > 正文

【Android】简易计算器_android开发一个简易的计算器

android开发一个简易的计算器

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:Kotlin版本+Java版本

这学期学习了Android开发,写的第一个小项目就是简易计算器。原本只写了kotlin版本,放假后加了Java版本,在此记录下。

计算器的功能包括【加减乘除、求余】,功能比较简单,所以是简易计算器。


计算器效果如图:

简易计算器

一、Kotlin版本

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

1. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1a1c21"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="167dp" />


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="261dp" />


    <TextView
        android:id="@+id/process_text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:gravity="bottom|end"
        android:letterSpacing="0.1"
        android:text="16x16"
        android:textColor="#f4f0f2"
        android:textSize="25sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/result_text_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"
        android:gravity="end|center_vertical"
        android:letterSpacing="0.1"
        android:text="196"
        android:textColor="@color/white"
        android:textSize="30sp"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline1" />

    <TextView
        android:id="@+id/btn_view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@drawable/top_round_shape"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/result_text_view"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/mod_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="operatorButtonClicked"
        android:text="@string/percent"
        android:textColor="#d74b51"
        android:textSize="25sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/div_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_M"
        app:layout_constraintTop_toBottomOf="@+id/result_text_view" />

    <TextView
        android:id="@+id/view_M"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="mButtonClicked"
        android:text="@string/M"
        android:textColor="#29d1b2"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_9"
        app:layout_constraintEnd_toStartOf="@+id/mod_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/del_view"
        app:layout_constraintTop_toBottomOf="@+id/result_text_view" />

    <TextView
        android:id="@+id/view_7"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_7"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_4"
        app:layout_constraintEnd_toStartOf="@+id/view_8"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ac_view" />

    <TextView
        android:id="@+id/view_8"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_8"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_5"
        app:layout_constraintEnd_toStartOf="@+id/view_9"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_7"
        app:layout_constraintTop_toBottomOf="@+id/del_view" />

    <TextView
        android:id="@+id/view_4"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_4"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_1"
        app:layout_constraintEnd_toStartOf="@+id/view_5"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view_7" />

    <TextView
        android:id="@+id/view_1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_1"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_0"
        app:layout_constraintEnd_toStartOf="@+id/view_2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view_4" />

    <TextView
        android:id="@+id/view_0"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_0"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/dot_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/view_1" />

    <TextView
        android:id="@+id/view_3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_3"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/equal_view"
        app:layout_constraintEnd_toStartOf="@+id/add_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_2"
        app:layout_constraintTop_toBottomOf="@+id/view_6" />

    <TextView
        android:id="@+id/view_5"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_5"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_2"
        app:layout_constraintEnd_toStartOf="@+id/view_6"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_4"
        app:layout_constraintTop_toBottomOf="@+id/view_8" />

    <TextView
        android:id="@+id/equal_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="equalButtonClicked"
        android:text="@string/equal"
        android:textColor="#ffff00"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/sub_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/dot_view"
        app:layout_constraintTop_toBottomOf="@+id/view_3" />

    <TextView
        android:id="@+id/dot_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="dotButtonClicked"
        android:text="@string/dot"
        android:textColor="@color/white"
        android:textSize="30sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/equal_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_0"
        app:layout_constraintTop_toBottomOf="@+id/view_2" />

    <TextView
        android:id="@+id/add_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="operatorButtonClicked"
        android:text="@string/add"
        android:textColor="#d74b51"
        android:textSize="35sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/sub_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_3"
        app:layout_constraintTop_toBottomOf="@+id/mul_view" />

    <TextView
        android:id="@+id/sub_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="operatorButtonClicked"
        android:text="@string/sub"
        android:textColor="#d74b51"
        android:textSize="35sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/equal_view"
        app:layout_constraintTop_toBottomOf="@+id/add_view" />

    <TextView
        android:id="@+id/view_2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_2"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/dot_view"
        app:layout_constraintEnd_toStartOf="@+id/view_3"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_1"
        app:layout_constraintTop_toBottomOf="@+id/view_5" />

    <TextView
        android:id="@+id/mul_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="operatorButtonClicked"
        android:text="@string/mul"
        android:textColor="#d74b51"
        android:textSize="35sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/add_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_6"
        app:layout_constraintTop_toBottomOf="@+id/div_view" />

    <TextView
        android:id="@+id/view_6"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_6"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_3"
        app:layout_constraintEnd_toStartOf="@+id/mul_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_5"
        app:layout_constraintTop_toBottomOf="@+id/view_9" />

    <TextView
        android:id="@+id/ac_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="clearButtonClicked"
        android:text="@string/AC"
        android:textColor="#29d1b2"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_7"
        app:layout_constraintEnd_toStartOf="@+id/del_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/result_text_view" />

    <TextView
        android:id="@+id/view_9"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="numberButtonClicked"
        android:text="@string/num_9"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_6"
        app:layout_constraintEnd_toStartOf="@+id/div_view"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_8"
        app:layout_constraintTop_toBottomOf="@+id/view_M" />

    <TextView
        android:id="@+id/div_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="operatorButtonClicked"
        android:text="@string/div"
        android:textColor="#d74b51"
        android:textSize="35sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/mul_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/view_9"
        app:layout_constraintTop_toBottomOf="@+id/mod_view" />

    <TextView
        android:id="@+id/del_view"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@drawable/round_shape"
        android:gravity="center"
        android:onClick="delButtonClicked"
        android:text="@string/Del"
        android:textColor="#29d1b2"
        android:textSize="22sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/view_8"
        app:layout_constraintEnd_toStartOf="@+id/view_M"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/ac_view"
        app:layout_constraintTop_toBottomOf="@+id/result_text_view" />

    <ImageView
        android:id="@+id/lightMode"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/mode_ic" />
</androidx.constraintlayout.widget.ConstraintLayout>
  • 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
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425

2. MainActivity.kt

package com.example.my_counter

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.my_counter.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private val currentInputNumString = StringBuilder()
    private val numbersList = mutableListOf<Double>()
    private val operatorsList = mutableListOf<String>()
    private val dotsList = mutableListOf(false)
    private var isNumStart = true

    private lateinit var processTextView: TextView
    private lateinit var resultTextView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        processTextView = binding.processTextView
        resultTextView = binding.resultTextView
    }

    fun numberButtonClicked(view: View) {
        val tv = view as TextView
        if (currentInputNumString.toString() == "0") {
            currentInputNumString.clear()
        }
        // 更新text view内容
        currentInputNumString.append(tv.text)
        processTextView.text = currentInputNumString.toString()

        if (isNumStart) {
            isNumStart = false
        }
    }

    fun operatorButtonClicked(view: View) {
        val tv = view as TextView
        val myOperators = listOf('+', '-', '×', '÷', '%')
        val digits = listOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
        // 如果前一个符号为运算符,则更换运算符
        when {
            currentInputNumString[currentInputNumString.length - 1] in myOperators -> {
                currentInputNumString.deleteCharAt(currentInputNumString.length - 1)
                currentInputNumString.append(tv.text)
            }
            currentInputNumString[currentInputNumString.length - 1] in digits -> {
                // 如果前一个符号为数字
                currentInputNumString.append(tv.text.toString())
                processTextView.text = currentInputNumString.toString()
                isNumStart = true
                dotsList.add(false)
            }
            else -> {
                Toast.makeText(this, "此刻不能添加${tv.text}!", Toast.LENGTH_SHORT).show()
            }
        }
        processTextView.text = currentInputNumString.toString()
        Log.v("myTag", "$operatorsList")
    }


    fun clearButtonClicked(view: View) {
        currentInputNumString.clear()
        currentInputNumString.append("0")
        processTextView.text = currentInputNumString.toString()
        // 重置列表、参数
        isNumStart = true
        dotsList.clear()
        dotsList.add(false)
    }

    fun delButtonClicked(view: View) {
        val myOperators = listOf('+', '-', '×', '÷', '%')
        // 若输入串为NULL,则不进行操作
        if (currentInputNumString.isNotEmpty()) {
            if (currentInputNumString[currentInputNumString.length - 1] in myOperators) {
                isNumStart = false
                dotsList.removeLast()
            } else if (currentInputNumString[currentInputNumString.length - 1] == '.') {
                dotsList[dotsList.size - 1] = false
            } else {
                if (currentInputNumString.length >= 2 && currentInputNumString[currentInputNumString.length - 2] in myOperators) {
                    isNumStart = true
                    //dotsList.removeLast()
                } else if(currentInputNumString.length == 1){
                    isNumStart = true
                }
            }
            currentInputNumString.deleteCharAt(currentInputNumString.length - 1)
            processTextView.text = currentInputNumString.toString()
        }
    }

    fun dotButtonClicked(view: View) {
        if (!dotsList[dotsList.size - 1]) {
            val tv = view as TextView
            if (currentInputNumString.isEmpty()) {
                currentInputNumString.append("0")
            }
            currentInputNumString.append(tv.text.toString())
            processTextView.text = currentInputNumString.toString()
            dotsList[dotsList.size - 1] = true
        }
    }

    fun equalButtonClicked(view: View) {
        if (currentInputNumString.isEmpty()) {
            resultTextView.text = "0"
            return
        }
        var resNum = 0.0
        var i = 0
        var temp = 0.0
        val numStr = StringBuilder()
        val firstOperators = listOf("×", "÷", "%")
        val operators = listOf('×', '÷', '%', '+', '-')
        // 构造numbersList、operatorsList
        numbersList.clear()
        operatorsList.clear()
        while (i < currentInputNumString.length) {
            if (currentInputNumString[i] in operators) {
                numbersList.add(numStr.toString().toDouble())
                operatorsList.add(currentInputNumString[i].toString())
                numStr.clear()
            } else {
                numStr.append(currentInputNumString[i])
            }
            i++
        }
        if (numStr.isNotEmpty()) {
            numbersList.add(numStr.toString().toDouble())
        } else {
            operatorsList.removeLast()
        }
        // 进行乘除运算
        i = 0
        while (i < operatorsList.size) {
            if (operatorsList[i] in firstOperators) {
                when (operatorsList[i]) {
                    "×" -> temp = numbersList[i] * numbersList[i + 1]
                    "÷" -> temp = numbersList[i] / numbersList[i + 1]
                    "%" -> temp = numbersList[i] % numbersList[i + 1]
                }
                operatorsList.removeAt(i)
                numbersList[i + 1] = temp
                numbersList.removeAt(i)
            } else {
                i++
            }
        }
        // 进行加减运算
        i = 0
        resNum = numbersList[i]
        numbersList.removeAt(0)
        while (i < operatorsList.size) {
            if (operatorsList[i] == "+") {
                resNum += numbersList[i]
            } else {
                resNum -= numbersList[i]
            }
            i++
        }
        // 显示结果
        val resStr = resNum.toString()
        val ii = resStr.indexOf('.')
        var flagInt = true
        for (a in ii + 1 until resStr.length) {
            if (resStr[a] != '0') {
                flagInt = false
                break
            }
        }
        if (flagInt) {    // 显示整数
            resultTextView.text = resNum.toInt().toString()
        } else {  // 显示浮点数
            resultTextView.text = resNum.toString()
        }
        Log.v("myTag", "$numbersList")
        Log.v("myTag", "$operatorsList")
    }

    fun mButtonClicked(view: View) {
        // 更新显示
        processTextView.text = resultTextView.text
        // 更新字符串
        currentInputNumString.clear()
        currentInputNumString.append(resultTextView.text.toString())
        // 重置dotsList列表
        dotsList.clear()
        dotsList.add(false)
        // 标记是否数字开头
        isNumStart = false
        // 判断是否为小数
        val numStr = resultTextView.text.toString()
        val i = numStr.indexOf('.')
        if (i > 0) {
            dotsList[0] = 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

二、Java版本

1. activity_main.xml

提示:其实两个版本的布局文件都差不多,只是颜色和背景有区别而已。 直接用Kotlin版本的activity_main.xml,再修改下颜色、背景即可。

2. MainActivity.java

代码如下:

package com.example.zhangskin;

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

import com.zhy.changeskin.SkinManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class MainActivity extends com.zhy.changeskin.base.BaseSkinActivity implements View.OnClickListener {

    private int signal = 0;
    private final StringBuilder currentInputNumString = new StringBuilder();
    private final ArrayList<Double> numbersList = new ArrayList<>();
    private final ArrayList<String> operatorsList = new ArrayList<>();
    private final ArrayList<Boolean> dotsList = new ArrayList<>();
    private Boolean isNumStart = true;

    private TextView processTextView;
    private TextView resultTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 换肤第三方库注册,可直接注释掉
        SkinManager.getInstance().init(this);

        dotsList.add(false);

        processTextView = findViewById(R.id.process_text_view);
        resultTextView = findViewById(R.id.result_text_view);

        // 绑定监听事件
        findViewById(R.id.view_0).setOnClickListener(this);
        findViewById(R.id.view_1).setOnClickListener(this);
        findViewById(R.id.view_2).setOnClickListener(this);
        findViewById(R.id.view_3).setOnClickListener(this);
        findViewById(R.id.view_4).setOnClickListener(this);
        findViewById(R.id.view_5).setOnClickListener(this);
        findViewById(R.id.view_6).setOnClickListener(this);
        findViewById(R.id.view_7).setOnClickListener(this);
        findViewById(R.id.view_8).setOnClickListener(this);
        findViewById(R.id.view_9).setOnClickListener(this);
        findViewById(R.id.view_M).setOnClickListener(this);
        findViewById(R.id.ac_view).setOnClickListener(this);
        findViewById(R.id.del_view).setOnClickListener(this);
        findViewById(R.id.add_view).setOnClickListener(this);
        findViewById(R.id.sub_view).setOnClickListener(this);
        findViewById(R.id.mul_view).setOnClickListener(this);
        findViewById(R.id.div_view).setOnClickListener(this);
        findViewById(R.id.mod_view).setOnClickListener(this);
        findViewById(R.id.dot_view).setOnClickListener(this);
        findViewById(R.id.equal_view).setOnClickListener(this);

        // bringToFront()让控件始终在最顶层,不被覆盖                     
        findViewById(R.id.lightMode).bringToFront();
        // 换肤操作,可直接注释掉
        findViewById(R.id.lightMode).setOnClickListener(view -> {
            if(signal == 0){
                signal = 1;
                SkinManager.getInstance().changeSkin("night");
            }else{
                signal = 0;
                SkinManager.getInstance().removeAnySkin();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.view_0:
            case R.id.view_1:
            case R.id.view_2:
            case R.id.view_3:
            case R.id.view_4:
            case R.id.view_5:
            case R.id.view_6:
            case R.id.view_7:
            case R.id.view_8:
            case R.id.view_9:
                numberButtonClicked(view);break;
            case R.id.add_view:
            case R.id.sub_view:
            case R.id.mul_view:
            case R.id.div_view:
            case R.id.mod_view:
                operatorButtonClicked(view);break;
            case R.id.ac_view:
                clearButtonClicked(view);break;
            case R.id.del_view:
                delButtonClicked(view);break;
            case R.id.view_M:
                mButtonClicked(view);break;
            case R.id.dot_view:
                dotButtonClicked(view);break;
            case R.id.equal_view:
                equalButtonClicked(view);break;
            default:break;
        }
    }

    public void numberButtonClicked(View v){
        TextView tv = (TextView) v;
        if (currentInputNumString.toString().equals("0")) {
            currentInputNumString.delete(0,currentInputNumString.length());
        }
        // 更新text view内容
        currentInputNumString.append(tv.getText());
        processTextView.setText(currentInputNumString.toString());
        if (isNumStart) {
            isNumStart = false;
        }
    }
    public void operatorButtonClicked(View view) {
        TextView tv = (TextView) view;
        List<Character> myOperators = Arrays.asList('+', '-', '×', '÷', '%');
        List<Character> digits = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        // 如果前一个符号为运算符,则更换运算符
        if(myOperators.contains(currentInputNumString.charAt(currentInputNumString.length() - 1))) {
                currentInputNumString.deleteCharAt(currentInputNumString.length() - 1);
                currentInputNumString.append(tv.getText());
        }else if(digits.contains(currentInputNumString.charAt(currentInputNumString.length() - 1))){
            // 如果前一个符号为数字
            currentInputNumString.append(tv.getText().toString());
            processTextView.setText(currentInputNumString.toString());
            isNumStart = true;
            dotsList.add(false);
        }else {
            Toast.makeText(this, "此刻不能添加${tv.getText()}!", Toast.LENGTH_SHORT).show();
        }
        processTextView.setText(currentInputNumString.toString());
    }


    public void clearButtonClicked(View view) {
        currentInputNumString.delete(0,currentInputNumString.length());
        currentInputNumString.append("0");
        processTextView.setText(currentInputNumString.toString());
        // 重置列表、参数
        isNumStart = true;
        dotsList.clear();
        dotsList.add(false);
    }

    public void delButtonClicked(View view) {
        List<Character> myOperators = Arrays.asList('+', '-', '×', '÷', '%');
        // 若输入串为NULL,则不进行操作
        if (currentInputNumString.length()>0) {
            char temp = currentInputNumString.charAt(currentInputNumString.length() - 1);
            if (myOperators.contains(temp)) {
                isNumStart = false;
                dotsList.remove(dotsList.size()-1);
                //dotsList.removeLast();
            } else if ('.' == currentInputNumString.charAt(currentInputNumString.length() - 1)) {
                dotsList.set(dotsList.size() - 1, false);
            } else {
                if (currentInputNumString.length() >= 2 &&
                        myOperators.contains(currentInputNumString.charAt(currentInputNumString.length() - 2))) {
                    isNumStart = true;
                } else if(currentInputNumString.length() == 1){
                    isNumStart = true;
                }
            }
            currentInputNumString.deleteCharAt(currentInputNumString.length() - 1);
            processTextView.setText(currentInputNumString.toString());
        }
    }

    public void dotButtonClicked(View view) {
        if (!dotsList.get(dotsList.size() - 1)) {
            TextView tv = (TextView) view;
            if (currentInputNumString.length() == 0) {
                currentInputNumString.append("0");
            }
            currentInputNumString.append(".");
            processTextView.setText(currentInputNumString.toString());
            dotsList.set(dotsList.size() - 1, true);
        }
    }

    public void mButtonClicked(View view) {
        // 更新显示
        processTextView.setText(resultTextView.getText());
        // 更新字符串
        currentInputNumString.delete(0,currentInputNumString.length());
        currentInputNumString.append(resultTextView.getText().toString());
        // 重置dotsList列表
        dotsList.clear();
        dotsList.add(false);
        // 标记是否数字开头
        isNumStart = false;
        // 判断是否为小数
        String numStr = resultTextView.getText().toString();
        int i = numStr.indexOf('.');
        if (i > 0) {
            dotsList.set(0, true);
        }
    }

    public void equalButtonClicked(View view) {
        if (currentInputNumString.length() == 0 ) {
            resultTextView.setText("0");
            return;
        }
        double resNum = 0.0;
        int i = 0;
        double temp = 0.0;
        StringBuilder numStr = new StringBuilder();
        List<String> firstOperators = Arrays.asList("×", "÷", "%");
        List<Character> operators = Arrays.asList('×', '÷', '%', '+', '-');
        // 构造numbersList、operatorsList
        numbersList.clear();
        operatorsList.clear();
        while (i < currentInputNumString.length()) {
            if (operators.contains(currentInputNumString.charAt(i))) {
                numbersList.add(Double.parseDouble(numStr.toString()));
                operatorsList.add(Character.toString(currentInputNumString.charAt(i)));
                numStr.delete(0,numStr.length());
            } else {
                numStr.append(currentInputNumString.charAt(i));
            }
            i++;
        }
        if (numStr.length()>0) {
            numbersList.add(Double.parseDouble(numStr.toString()));
        } else {
            operatorsList.remove(operatorsList.size()-1);
        }
        // 进行乘除运算
        i = 0;
        while (i < operatorsList.size()) {
            if (firstOperators.contains(operatorsList.get(i))){
                switch (operatorsList.get(i)) {
                    case "×":
                        temp = numbersList.get(i) * numbersList.get(i + 1);break;
                    case "÷":
                        temp = numbersList.get(i) / numbersList.get(i + 1);break;
                    case "%":
                        temp = numbersList.get(i) % numbersList.get(i + 1);break;
                }
                operatorsList.remove(i);
                numbersList.set(i+1,temp);
                numbersList.remove(i);
            } else {
                i++;
            }
        }
        // 进行加减运算
        i = 0;
        resNum = numbersList.get(i);
        numbersList.remove(0);
        while (i < operatorsList.size()) {
            if (operatorsList.get(i).equals("+")) {
                resNum += numbersList.get(i);
            } else {
                resNum -= numbersList.get(i);
            }
            i++;
        }
        // 显示结果
        String resStr = Double.toString(resNum);
        int ii = resStr.indexOf('.');
        boolean flagInt = true;
        for (int a = ii + 1;a < resStr.length();a++) {
            if (resStr.charAt(a) != '0') {
                flagInt = false;
                break;
            }
        }
        if (flagInt) {    // 显示整数
            resultTextView.setText(String.valueOf((int)resNum));
        } else {  // 显示浮点数
            resultTextView.setText(String.valueOf(resNum));
        }
    }
}
  • 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
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283

总结

计算器的界面,是我参照B站上一个up主的讲解视频完成的,至于逻辑完成的代码,原本也是照着up主来,后来发现效果不是我想要的,就自己改了。

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

闽ICP备14008679号