Android简单计算器实现

Android简单计算器实现

前言

最近刚开始学习安卓,只是简单的看到布局那块,就开始上手写了,从布局那里就开始各种乱,还到处拖按钮,一开始的第一版只能实现两个数的运算,比较简单,就是简单的上了个手,在第二版里实现了多项式的运算(没加括号的),最难的是里面的逻辑代码算法那块。
这里写图片描述
这个是我第一版的,,,可能我是有毒吧,,这样的计算器,不被人打死就好
这里写图片描述
这个是我第二版的,,实现了许多计算优先级的问题,可以记住上次计算的结果,错误输入的时候会提示

在UI那块

我采用的是嵌套的布局结构,在最外层是用的是LinearLayout的线性竖线布局,内层是LinearLayout的线性横向布局,每个内层里横向布局,设置权重,让按钮很整体很好布局。外层的竖线线性布局,可以让按钮紧贴最下面,方便,调整Testview和每个横行的比例关系,下面上代码:
在app/src/main/res/layout/中的activity_main.xml中的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="40sp"
            android:id="@+id/btn_clear"
            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_divide"
            android:layout_weight="1"
            android:background="#ffffff"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="X"
            android:textSize="40sp"
            android:id="@+id/btn_multiply"
            android:layout_weight="1"
            android:background="#ffffff"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="T"
            android:textSize="40sp"
            android:id="@+id/btn_del"
            android:layout_weight="1"
            android:background="#ffffff"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:weightSum="4"
        >
        <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="#ffffcc"/>
        <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="#ffffcc"/>
        <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="#ffffcc"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="-"
            android:textSize="50sp"
            android:id="@+id/btn_minus"
            android:layout_weight="1"
            android:background="#ffffff"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:weightSum="4"
        >
         <Button
             android:layout_width="0dp"
             android:layout_height="match_parent"
            android:text="4"
            android:textSize="30sp"
            android:id="@+id/btn_4"
             
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值