android计算器开源小项目代码(附安装包.apk)

最近在学习安卓开发,做了个小计算器作为实践。发现网上的计算器教程代码的健壮性不够好,只能够容忍正确的输入。于是我花了几天时间写了个完整的程序。可能是我水平有限,其中条件控制逻辑设计的比较复杂,但我受开源运动影响比较深,我学习了别人的代码,就应该把自己的代码公布出来,即使写的不好,多多少少能为一些人带来一点点帮助吧,同时记录自己的学习历程。

先来展示一下成果:

 

 开发工具我选择了android studio, 诚实的说我对xml并不是那么的了解,所以我只能采用所见即所得的办法,不过这个小项目做下来,感觉比刚开始好多了。

再说一下思路吧,计算器上方是个显示器,是一个TextView,显示器上方的东西是个spinner,作用待会再说,显示器下方都是button,通过监听button的摁下,在TextView上显示用户输入来与用户做交互,然后将TextView上的内容提取出来做运算,再显示再屏幕上。其实大部分的工作都是在做人机交互以及规范用户的输入,真正实现功能的代码并不多。

下面开始贴代码了,先贴xml吧,方便与上面的图片比对

 

xml代码部分(AndroidMainfest.xml):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="bottom"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="9"
            android:orientation="vertical">

            <Spinner
                android:id="@+id/spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="start"
                android:textSize="50sp" />

            <TextView
                android:id="@+id/concle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="bottom|end"
                android:maxLines="2"
                android:minLines="1"
                android:singleLine="false"
                android:text="@string/priNum"
                android:textSize="45sp"
                />

        </LinearLayout>

        <TableLayout
            android:id="@+id/table"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:gravity="bottom">

            <TableRow
                android:id="@+id/line1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1">

                <Button
                    android:id="@+id/buttonClean"
                    style="?android:attr/buttonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@android:color/darker_gray"
                    android:text="@string/button_c"
                    android:textColor="@android:color/holo_red_dark"
                    android:textSize="40sp" />

                <Button
                    android:id="@+id/buttonMul"
                    style="?android:attr/buttonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#fa8e12"
                    android:text="@string/button_mul"
                    android:textSize="40sp" />

                <Button
                    android:id="@+id/buttonDiv"
                    style="?android:attr/buttonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#fa8e12"
                    android:text="@string/button_div"
                    android:textSize="40sp" />

                <Button
                    android:id="@+id/buttonDel"
                    style="?android:attr/buttonStyle"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="#b22222"
                    android:text="@string/button_del"
                    android:textSize&
  • 11
    点赞
  • 80
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值