Android小程序-简易计算器的实现

目标效果:

 

通过编写代码,可以实现整数和小数的加减乘除运算,以及删除和清空的功能。


1.页面中Button使用的是线性布局,最外边一个是父布局,第一行C,DEL,/,*为第一个子布局,第二行7,8,9,-为第二个子布局,第三行4,5,6,+为第三个子布局,第四五行为第四个子布局,第四个子布局中还有两个相当于是孙布局的级别,1,2,3为第一个孙布局,0和.为第二个孙布局,=在两个孙布局之外第四个子布局以内。因为计算器的水平竖直排列十分鲜明,所以可以用线性布局,当然也可以用表格布局来进行排布。


2.activity_main.xml页面用于存放所有控件。

activity_main.xml页面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_marginTop="40dp"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/etInput"
    	android:layout_width="310dp"
    	android:layout_height="60dip"
    	android:editable="false"         //代表不能进行键盘输入
    	android:gravity="right"          //文字靠右边
    	android:layout_gravity="center"
    	android:background="@drawable/white_bg"/>   <!-- 设置输入框的背景,为一个xml文件 -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >          <!-- 代表水平排布 -->
        
        <Button
        android:id="@+id/btClear"
        android:background="@drawable/white_select"   //设置按钮的背景,为一个xml文件
        android:layout_width="75dp"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:text="C" />
        <Button
        android:id="@+id/btDel"
        android:background="@drawable/white_select"
        android:layout_marginLeft="5dp"
        android:layout_width="75dp"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:text="DEL" />
        <Button
        android:id="@+id/btDivide"
        android:background="@drawable/white_select"
        android:layout_marginLeft="5dp"
        android:layout_width="75dp"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:text="/" />
        <Button
        android:id="@+id/btMul"
        android:background="@drawable/white_select"
        android:layout_marginLeft="5dp"
        android:layout_width="75dp"
        android:layout_height="60dp"
        android:textSize="20sp"
        android:text="*" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal" >
        
        <Button
        android:id="@+id/btSeven"
        android:background="@drawable/white_select"
        android:layout_width="75dp"
        android:layout_height="60dp"
        android:textSize="20
  • 10
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
好的,我可以为您介绍一下面向对象程序设计中简单工厂模式的应用。 简单工厂模式是一种创建型设计模式,它提供了一种创建对象的最佳方式。该模式允许客户端通过传递类型参数来请求工厂创建所需类型的对象。 在一个简易计算器中,我们可以使用简单工厂模式来创建不同类型的运算对象。首先,我们需要定义一个运算接口,例如: ```java public interface Operation { double getResult(double numberA, double numberB); } ``` 然后,我们可以定义不同类型的运算类,例如: ```java public class AddOperation implements Operation { public double getResult(double numberA, double numberB) { return numberA + numberB; } } public class SubtractOperation implements Operation { public double getResult(double numberA, double numberB) { return numberA - numberB; } } public class MultiplyOperation implements Operation { public double getResult(double numberA, double numberB) { return numberA * numberB; } } public class DivideOperation implements Operation { public double getResult(double numberA, double numberB) { if (numberB == 0) { throw new IllegalArgumentException("除数不能为0"); } return numberA / numberB; } } ``` 最后,我们可以定义一个简单工厂类,根据传入的运算类型参数来创建相应的对象: ```java public class OperationFactory { public static Operation createOperation(String operationType) { Operation operation = null; switch (operationType) { case "+": operation = new AddOperation(); break; case "-": operation = new SubtractOperation(); break; case "*": operation = new MultiplyOperation(); break; case "/": operation = new DivideOperation(); break; default: throw new IllegalArgumentException("不支持的运算类型"); } return operation; } } ``` 使用简单工厂模式,我们可以在客户端代码中通过传递运算类型参数来获取相应的运算对象,例如: ```java public static void main(String[] args) { Operation operation = OperationFactory.createOperation("+"); double result = operation.getResult(3, 4); System.out.println(result); // 输出 7.0 } ``` 这样,我们就可以灵活地创建不同类型的运算对象,实现了代码的可扩展性和可维护性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值