安卓开发编写计算器(工具 android studio)

本文详述了如何使用Android Studio创建一个简单的计算器应用。通过网格布局设计UI,并实现加、减、乘、除四种基本运算。展示了activity_main.xml的界面代码以及MainActivity.java中的关键操作,最终给出了不同运算的结果截图。

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

问题:编写一个计算器。

用网格布局编写界面:

activity_main.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:rowCount="6"
    android:columnCount="4" >
    <!--一个6行4列的布局-->
<!--文本标签显示输入的等式-->
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_marginLeft="4dp"
        android:gravity="left"
        android:id="@+id/input"
        android:textSize="30dp"/>
    <!--计算器按钮0-9,+-*/和删除-->
    <Button
        android:id="@+id/button_delete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textSize="26sp"
        />
    <Button android:id="@+id/button1" android:text="1" android:textSize="26sp"/>
    <Button android:id="@+id/button2" android:text="2" android:textSize="26sp"/>
    <Button android:id="@+id/button3" android:text="3" android:textSize="26sp"/>
    <Button android:id="@+id/button_add" android:text="+" android:textSize="26sp"/>
    <Button android:id="@+id/button4" android:text="4" android:textSize="26sp"/>
    <Button android:id="@+id/button5" android:text="5" android:textSize="26sp"/>
    <Button android:id="@+id/button6
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值