如何用 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 计算器的核心概念。根据需求可以进一步扩展功能和完善用户体验。

相关推荐
00后程序员张4 分钟前
App 上架全流程指南,iOS 应用发布步骤、ipa 文件上传工具、TestFlight 分发与 App Store 审核经验分享
android·ios·小程序·https·uni-app·iphone·webview
2501_916013749 分钟前
iOS App 上架流程详解,苹果应用发布步骤、App Store 审核规则、ipa 文件上传与测试分发实战经验
android·ios·小程序·https·uni-app·iphone·webview
周杰伦的稻香2 小时前
MySQL中的空间碎片率计算分析
android·数据库·mysql
用户093 小时前
MVI架构如何改变Android开发模式
android·面试·kotlin
梦终剧4 小时前
【Android之路】.sp和界面层次结构
android
2501_916008894 小时前
iOS 26 软件性能测试全流程,启动渲染资源压力对比与优化策略
android·macos·ios·小程序·uni-app·cocoa·iphone
zh_xuan4 小时前
Android Handler源码阅读
android
雪饼android之路4 小时前
【Android】 android suspend/resume总结(3)
android·linux
00后程序员张4 小时前
iOS 26 兼容测试实战,机型兼容、SwiftUI 兼容性改动
android·ios·小程序·uni-app·swiftui·cocoa·iphone
molong9315 小时前
Android 应用配置跳转微信小程序
android·微信小程序·小程序