Android 计算器

本文介绍了如何在Android Studio环境下开发一个基本的计算器应用。功能包括进行加减乘除运算。设计了6行4列的网格布局,第一行用于显示计算结果,剩余行分配数字和运算符按钮。通过activity_main.xml定义布局,MainActivity.java处理用户交互和计算逻辑。提供了源代码片段,并展示了运行结果。
摘要由CSDN通过智能技术生成

1 项目名称

   计算器

2 开发环境

    Android Studio

3 功能设定

   进行简单的加减乘除运算

4 具体设计

    在界面设计区域中设置一个6行4列的网格布局,第一行为显示数据的文本标签,第二行为清除数据的按钮,第三至六行均划为四列,共安排十六个按钮,分别代表数字0、1、2、······、9及加、减、乘、除、等号等符号。

5 设计步骤

(1)设计网格布局的布局文件acivity_main.xml并声明TextView和各个Button

(2)设计控制文件MainActivity.java,建立程序中组件与用户界面程序组件的关联,并编写按钮的事件处理代码

6 源代码

   6.1 acivity_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"
    android:rowCount="6"
    android:columnCount="4"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_marginLeft="4dp"
        android:gravity="left"
        android:text=" "
        android:textSize="50dip"/>

    <Button
        android:id="@+id/btnclear"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textSize="26sp"/>

    <Button android:id="@+id/btn1" android:text="1" android:textSize="26sp"/>
    <Button android:id="@+id/btn2" android:text="2" android:textSize="26sp"/>
    <Button android:id="@+id/btn3" android:text="3" android:textSize="26sp"/>
    <Button android:id="@+id/btnadd" android:text="+" android:textSize="26sp"/>
    <Button android:id="@+id/btn4" android:text="4" android:textSize="26sp"/>
    <Button android:id="@+id/btn5" android:text="5" android:textSize="26sp"/>
    <Button android:id="@+id/btn6" android:text="6" android:textSize="26sp"/>
    <Button android:id="@+id/btnsub" android:text="-" android:textSize="26sp"/>
    <Button android:id="@+id/btn7" android:text="7" 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值