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

相关推荐
红尘伴蝶舞3 分钟前
从秒开优化到连环崩溃:深入剖析 ViewPager2 在常驻 DialogFragment 中的复用陷阱与自愈方案
android·kotlin
巫山老妖5 小时前
Android 应用架构演进:MVC / MVP / MVVM / MVI (2026)
android·android jetpack
木头左9 小时前
【架构设计】教育类 App 服务端化改造:AI 上移、题库分层与个人题库归属
android·架构设计·系统设计·后端开发·数据库设计
墨狂之逸才9 小时前
USB 的 HOST 和 DEVICE 到底有什么区别?ADB 调试应该选哪个?
android
奶油话梅糖10 小时前
苹果的内核跑上了安卓手机:2,631 Star 的 HakiOS 让 XNU 在 Adreno GPU 上画出 118 帧
android·macos·智能手机
恋猫de小郭10 小时前
Flutter THLC 的 WebView 滚动终于从 70 FPS 提升到 120 FPS
android·前端·flutter
好的佩奇21 小时前
Dart 之扩展类型
android·flutter·dart
alexhilton1 天前
2027 Android 开发者路线图:学习、面试与 AI
android·kotlin·android jetpack
灿飞屋1 天前
Kotlin与Java虚拟机三:控制流
android·java·开发语言·后端·python·kotlin·安卓
疯华整猫1 天前
我用 Trae 把 Android 内存账重算了一遍
android·性能优化·源码