Android 简单计算机实现

这篇博客介绍了如何在Android Studio中创建一个简单的计算机应用。首先,详细讲述了新建工程的步骤,包括如何避免创建默认layout文件。接着,针对新建工程后可能出现的build.gradle版本不匹配问题提供了解决方案。然后,讲解了如何创建布局代码,包括在res/layout下创建新的布局文件。最后,重点解析了功能代码的实现,特别是布局代码和涉及运算的逻辑部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android 简单计算机的实现

新建工程

首先是创建一个新的工程
在这里插入图片描述
新建步骤如下:
在这里插入图片描述
点击next后
在这里插入图片描述
点击Next
在这里插入图片描述
选择Empty Activity选项
在这里插入图片描述
将第一个默认勾选的选项取消勾选,此选项是创建一个默认layout文件,因为我们要使用自己的layout文件作为布局文件,所以我们把此选项改为不勾选,最后点击finish完成创建。

新建文件错误解决

在这里插入图片描述
新建完成后文件报错,报错原因是build.grade文件中的版本不符所造成的问题,进行如下修改:
打开build.grade的文件:
在这里插入图片描述
将其中的版本号30改为26
在这里插入图片描述
如上图所示,修改完成后点击Try again重新加载,错误即可解决。
在这里插入图片描述

代码实现

我们要实现代码首先需要一个写代码的文件,首先创建一个布局代码的容器即在res文件夹下新建一个directory文件并将其命名为layout
在这里插入图片描述
接着在layout文件夹下新建一个布局文档(Layout resource file)用于存放布局代码,示例:将文件名命名为main
在这里插入图片描述
在这里插入图片描述

布局代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textResult" //命名id是为后面计算器功能的实现做准备
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:gravity="right" //设置文本对齐方式为左对齐
        android:text="" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"> //表示该布局水平对齐

        <Button  //按钮
            android:id="@+id/btnCLs" //为设置监听做准备
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" //android:layout_weight用于给一个线性布局中的诸多视图的重要度赋值
            android:text="C"/>
        <Button
            android:id="@+id/btnDel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:text="Del"/>
        <Button
            android:id="@+id/btnChu"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="÷"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnOne"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1"/>
        <Button
            android:id="@+id/btnTwo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2"/>
        <Button
            android:id="@+id/btnThree"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3"/>
        <Button
            android:id="@+id/btnAdd"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btnFour"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值