【Android】简易计算器实现

本文介绍了如何实现一个Android简易计算器,包括界面设计和逻辑实现。通过嵌套布局创建计算器界面,实现了加减乘除功能。实验过程中加深了对Android项目开发的理解,但也意识到在逻辑设计上的不足,计划通过不断练习提升编程能力。
摘要由CSDN通过智能技术生成

一、实验要求:

1、界面设计

2、设计并实现计算器的具体功能,如加减乘除等

3、完成开发日志(代码需有注释)

二、实验过程:

1、界面设计:

①布局:采用嵌套式布局结构,内外层分别使用横向和竖向的LinearLayout设计,使界面更加简洁美观

②源代码:

<?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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <!--横向排列-->
    <!--显示文本框-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="2"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/et_input"
            android:textSize="40sp"
            />
    </LinearLayout>
    <!--各类按键,分作五行-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:weightSum="4"

        >
        <!--清空-->
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="C"
            android:textSize="30sp"
            android:id="@+id/btn_clear"
            android:layout_weight="1"
            android:background="#cccccc"/>
        <!--除-->
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="÷"
            android:textSize="40sp"
            android:id="@+id/btn_divide"
            android:layout_weight="1"
            android:background="#cccccc"/>
        <!--乘-->
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="×"
            android:textSize="40sp"
            android:id="@+id/btn_multiply"
            android:layout_weight="1"
            android:background="#cccccc"/>
        <!--删除-->
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="DEL"
            android:textSize="30sp"
            android:id="@+id/btn_del"
            android:layout_weight="1"
            android:background="#cccccc"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:weightSum="4"
        >
        <!--7~9-->
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="7"
            android:textSize="30sp"
            android:id="@+id/btn_7"
            android:layout_weight="1"
            android:background="#ffffff"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="8"
            android:textSize="30sp"
            android:id="@+id/btn_8"
            android:layout_weight="1"
            android:background="#ffffff"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="9"
            android:textSize="30sp"
            android:id="@+id/btn_9"
            android:layout_weight="1"
            android:background="#ffffff"/>
        <!--减号-->
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="-"
            android:textSize="40sp"
            android:id="@+id/btn_minus"
            android:layout_weight="1"
            android:background="#cccccc"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="4"
        >
        <!--4~6-->
        <Button
            android:layout_width="0dp"
            android:layo
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值