AndroidStudio编写简易计算器

本文介绍了如何使用Android Studio开发一个简单的计算器应用。展示了界面设计、项目目录结构,并提供了布局文件activity_main.xml及MainActivity的代码片段。完整源码已上传至优快云,供读者下载学习。

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

最近想着用AS做一个简单好看的计算器拿来玩玩
界面如下:
在这里插入图片描述

在这里插入图片描述
Android项目目录结构如下图所示:
在这里插入图片描述
布局文件activity_main.xml示例图:
在这里插入图片描述

布局文件activity_main.xml完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#cacaca"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--标题及结果框-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.23"
        android:orientation="vertical"
        android:background="@drawable/frame"
        android:weightSum="1">
        <TextView
            android:id="@+id/id_input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"
            android:textSize="31sp"
            android:textColor="#2E2E2E"
            android:gravity="right" />

        <TextView
            android:id="@+id/id_result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.50"

            android:textSize="31sp"
            android:textColor="#2E2E2E"
            android:gravity="right" />
    </LinearLayout>
    <!--按键-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3">
        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TableRow android:layout_weight="1">
                <Button
                    android:id="@+id/btn_clear"
                    android:onClick="xml_OnClick"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:background="@drawable/selector_1"
                    android:text="AC"
                    android:textColor="#C106FF"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_delete"
                    android:layout_width="0dp"
                    android:onClick="xml_OnClick"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:background="@drawable/selector_1"
                    android:text="Del"
                    android:textAllCaps="false"
                    android:textColor="#6f7480"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_percent"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:background="@drawable/selector_1"
                    android:text="%"
                    android:textColor="#6f7480"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_divide"
                    android:layout_width="0dp"
                    android:onClick="xml_OnClick"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:background="@drawable/selector_1"
                    android:text="÷"
                    android:textColor="#6f7480"
                    android:textSize="30sp" />
            </TableRow>
            <TableRow android:layout_weight="1">
                <Button
                    android:id="@+id/btn_7"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_gravity="center_horizontal"
                    android:layout_weight="1"
                    android:background="@drawable/selector_1"
                    android:text="7"
                    android:onClick="xml_OnClick"
                    android:textColor="#6f7480"
                    android:textSize="30sp" />
                <Button
                    android:id="@+id/btn_8"
                    android:onClick="xml_OnClick"
                    android:layout_width="0dp"
                    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值