如何用 Kotlin 在 Android 手机开发一个计算器

使用 Kotlin 开发 Android 计算器

1. 创建新项目 打开 Android Studio,选择新建项目,模板选择 "Empty Activity",语言选择 Kotlin,确保最低 API 级别为 21 或更高。

2. 设计用户界面 在 res/layout/activity_main.xml 中定义计算器的布局。通常包括一个 TextView 显示结果和多个 Button 用于数字和操作符。

XML 复制代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tvResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"/>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:columnCount="4">

        <Button android:text="7" android:onClick="onDigitClick"/>
        <Button android:text="8" android:onClick="onDigitClick"/>
        <Button android:text="9" android:onClick="onDigitClick"/>
        <Button android:text="/" android:onClick="onOperatorClick"/>

        <!-- 继续添加其他按钮 -->
    </GridLayout>
</LinearLayout>

3. 实现逻辑 在 MainActivity.kt 中实现计算逻辑:

kotlin 复制代码
class MainActivity : AppCompatActivity() {
    private var currentInput = ""
    private var storedValue = 0.0
    private var currentOperator = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    fun onDigitClick(view: View) {
        val button = view as Button
        currentInput += button.text
        updateDisplay()
    }

    fun onOperatorClick(view: View) {
        val button = view as Button
        if (currentInput.isNotEmpty()) {
            performCalculation()
            currentOperator = button.text.toString()
            currentInput = ""
        }
    }

    fun onEqualsClick(view: View) {
        if (currentInput.isNotEmpty()) {
            performCalculation()
            currentOperator = ""
        }
    }

    fun onClearClick(view: View) {
        currentInput = ""
        storedValue = 0.0
        currentOperator = ""
        updateDisplay()
    }

    private fun performCalculation() {
        val inputValue = currentInput.toDouble()
        when (currentOperator) {
            "+" -> storedValue += inputValue
            "-" -> storedValue -= inputValue
            "*" -> storedValue *= inputValue
            "/" -> storedValue /= inputValue
            else -> storedValue = inputValue
        }
        currentInput = storedValue.toString()
        updateDisplay()
    }

    private fun updateDisplay() {
        findViewById<TextView>(R.id.tvResult).text = currentInput
    }
}

4. 测试应用 在模拟器或真机上运行应用,测试各种计算场景,确保基本功能正常工作。

5. 添加高级功能 可以考虑添加以下高级功能:

  • 小数点支持
  • 百分比计算
  • 正负号切换
  • 科学计算功能
  • 历史记录功能

6. 优化用户体验

  • 添加按钮点击效果
  • 处理除以零等错误情况
  • 调整布局适应不同屏幕尺寸
  • 添加动画效果

7. 发布准备

  • 添加应用图标
  • 配置应用名称
  • 测试不同 Android 版本兼容性
  • 生成签名 APK 或 App Bundle

这个基本实现展示了用 Kotlin 开发 Android 计算器的核心概念。根据需求可以进一步扩展功能和完善用户体验。

相关推荐
是真的小外套12 小时前
2024年软考中级信息安全工程师|选择题考点+案例分析真题解析(含答案)
android·网络
用户20187928316715 小时前
一文讲透 Android Native Heap 监控原理:一间"只进不出"的仓库,和总部派来的侦探
android
YB137516 小时前
Jetpack Compose 动画animate*AsState
android
高晶16 小时前
Android 通讯录应用开发实战:从零搭建 SQLite 数据持久化完整项目
android·设计模式·前端框架
隐擎fox16 小时前
深入 Android 移动端风控与协议层对抗:内核级虚拟环境检测、网络堆栈特征伪装与住宅 IP 协同实战
android·网络·python·网络协议·tcp/ip
换元不配限18 小时前
从 AsyncTask 到现代并发:Android 异步方案的演进与替代实践
android·asynctask·异步编程·方案对比·asynctask替代方案
用户20187928316718 小时前
你的 App 为什么是「电量刺客」?——从一条 WakeLock 追到 Linux 内核的 Android 省电原理全解
android
mmsx19 小时前
Android 安全校验下沉到原生层:防 patch 与构建期自动化
android·安全·自动化
老牛日常随笔20 小时前
Google把Android测试搬进AI对话:ARTEMIS如何用自然语言操作你的手机
android·人工智能·智能手机