Android简易计算器

Android简易计算器功能实现

1.项目简介

代码方面:进行两个运算数字之间简单的加减乘除运算。
布局方面:采取LinearLayout线性布局和RelativeLayout相对布局。
美化方面:使用了两个GIF动图美化界面,设计了点击button按钮伸缩、震动效果。

2.界面展示

界面展示

3.代码展示

完整源码链接: github地址
百度网盘:https://pan.baidu.com/s/1W8xg7X25CUTf0e3c7BVOGw?pwd=xgu0
提取码:xgu0

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:gravity="bottom"
    tools:context=".MainActivity">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="240dp">
        <ImageView
            android:id="@+id/gif_epinel"
            android:layout_width="170dp"
            android:layout_height="240dp"
            android:src="@drawable/epinel"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textSize="25sp"
            android:gravity="right|bottom"
            android:id="@+id/tv_result"/>
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:text="÷"
            android:textSize="30sp"
            android:background="@drawable/round_button_asmd"
            android:id="@+id/button_divide"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:text="×"
            android:textSize="30sp"
            android:background="@drawable/round_button_asmd"
            android:id="@+id/button_multiply"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:text="-"
            android:textSize="30sp"
            android:background="@drawable/round_button_asmd"
            android:id="@+id/button_subtract"/>
        <Button
            android:layout_width="70dp"
            android:layout_height="match_parent"
            android:text="+"
            android:textSize="30sp"
            android:background="@drawable/round_button_asmd"
            android:id="@+id/button_add"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="DEL"
            android:textSize="20sp"
            android:textColor="#FF0000"
            android:background="@drawable/round_button_del"
            android:id="@+id/button_del"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_7"
                    android:id="@+id/button_7"/>
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_8"
                    android:id="@+id/button_8"/>
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_9"
                    android:id="@+id/button_9"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_4"
                    android:id="@+id/button_4"/>
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_5"
                    android:id="@+id/button_5"/>
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_6"
                    android:id="@+id/button_6"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_1"
                    android:id="@+id/button_1"/>
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_2"
                    android:id="@+id/button_2"/>
                <Button
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@drawable/button_3"
                    android:id="@+id/button_3"/>
            </LinearLayout>
        </LinearLayout>
        <ImageView
            android:id="@+id/gif_alice"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/alice"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="@drawable/button_0"
            android:id="@+id/button_0"/>
        <Button
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:text="."
            android:textSize="35sp"
            android:background="@drawable/round_button_point"
            android:id="@+id/button_point"/>
        <ImageButton
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/sqrt"
            android:scaleType="fitCenter"
            android:id="@+id/button_sqrt"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="="
            android:textSize="35sp"
            android:background="#FFA500"
            android:id="@+id/button_equal"/>
    </LinearLayout>

</LinearLayout>

MainActivity.java
读取button按钮点击的功能函数onClick()

@RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onClick(View v)
    {
        String inputText;
        if(v.getId()==R.id.button_0)
        {
            inputText="0";
        }
        else if (v.getId()==R.id.button_1)
        {
            inputText="1";
        }
        else if (v.getId()==R.id.button_2)
        {
            inputText="2";
        }
        else if (v.getId()==R.id.button_3)
        {
            inputText="3";
        }
        else if (v.getId()==R.id.button_4)
        {
            inputText="4";
        }
        else if (v.getId()==R.id.button_5)
        {
            inputText="5";
        }
        else if (v.getId()==R.id.button_6)
        {
            inputText="6";
        }
        else if (v.getId()==R.id.button_7)
        {
            inputText="7";
        }
        else if (v.getId()==R.id.button_8)
        {
            inputText="8";
        }
        else if (v.getId()==R.id.button_9)
        {
            inputText="9";
        }
        else if (v.getId()==R.id.button_add)
        {
            inputText="+";
            if(first_num.equals(""))
            {
                //若输入加号时前面无运算数字则弹出提示窗口
                Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_SHORT).show();
                inputText="";
            }
            if(!operator.equals(""))
            {
                //若已经输过运算符,则弹出提示窗口
                Toast.makeText(MainActivity.this,"已有运算符",Toast.LENGTH_SHORT).show();
                String now_result="";
                now_result=tv_result.getText().toString();
                now_result=now_result.substring(0,now_result.length()-1);
                refreshResult(now_result);
                refreshText(now_result);
            }
        }
        else if (v.getId()==R.id.button_subtract)
        {
            inputText="-";
            if(first_num.equals(""))
            {
                Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_SHORT).show();
                inputText="";
            }
            if(!operator.equals(""))
            {
                Toast.makeText(MainActivity.this,"已有运算符",Toast.LENGTH_SHORT).show();
                String now_result="";
                now_result=tv_result.getText().toString();
                now_result=now_result.substring(0,now_result.length()-1);
                refreshResult(now_result);
                refreshText(now_result);
            }
        }
        else if (v.getId()==R.id.button_multiply)
        {
            inputText="×";
            if(first_num.equals(""))
            {
                Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_SHORT).show();
                inputText="";
            }
            if(!operator.equals(""))
            {
                Toast.makeText(MainActivity.this,"已有运算符",Toast.LENGTH_SHORT).show();
                String now_result="";
                now_result=tv_result.getText().toString();
                now_result=now_result.substring(0,now_result.length()-1);
                refreshResult(now_result);
                refreshText(now_result);
            }
        }
        else if (v.getId()==R.id.button_divide)
        {
            inputText="÷";
            if(first_num.equals(""))
            {
                Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_SHORT).show();
                inputText="";
            }
            if(!operator.equals(""))
            {
                Toast.makeText(MainActivity.this,"已有运算符",Toast.LENGTH_SHORT).show();
                String now_result="";
                now_result=tv_result.getText().toString();
                now_result=now_result.substring(0,now_result.length()-1);
                refreshResult(now_result);
                refreshText(now_result);
            }
        }
        else if(v.getId()==R.id.button_point)
        {
            inputText=".";
            if(operator.equals("")&&first_num.contains("."))
            {
                //若已经输过小数点则弹出提示窗口(两个小数点重复)
                Toast.makeText(MainActivity.this,"小数点已存在",Toast.LENGTH_SHORT).show();
                inputText="";
            }
            if(!operator.equals("")&&next_num.contains("."))
            {
                //若已经输过小数点则弹出提示窗口(两个小数点重复)
                Toast.makeText(MainActivity.this,"小数点已存在",Toast.LENGTH_SHORT).show();
                inputText="";
            }
        }
        else if(v.getId()==R.id.button_sqrt)
        {
            inputText="";
            if(first_num.equals(""))
            {
                //开根号时缺少底数弹出提示窗口
                Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_SHORT).show();
                return;
            }
        }
        else if (v.getId()==R.id.button_equal)
        {
            inputText="";
            if(operator.equals(""))
            {
                //若无运算符
                Toast.makeText(MainActivity.this,"请输入运算符",Toast.LENGTH_SHORT).show();
            }
            if (next_num.equals(""))
            {
                //若无第二个运算数字
                Toast.makeText(MainActivity.this,"请输入数字",Toast.LENGTH_SHORT).show();
                return;
            }
        }
        else
        {
            inputText="";
        }
        switch (v.getId())
        {
            case R.id.button_del:
                String now_result="";
                now_result=tv_result.getText().toString();
                if(now_result.equals(""))
                {
                    Toast.makeText(MainActivity.this,"已无可删除内容",Toast.LENGTH_SHORT).show();
                }
                else
                {
                    if(now_result.length()==1)
                    {
                        now_result="";
                    }
                    else
                    {
                        now_result=now_result.substring(0,now_result.length()-1);
                    }
                    refreshResult(now_result);
                    refreshText(now_result);
                }
                break;
            case R.id.button_add:
            case R.id.button_subtract:
            case R.id.button_multiply:
            case R.id.button_divide:
                operator=inputText;
                refreshText(showText+operator);
                break;
            case R.id.button_equal:
                double fourOperation_result=fourOperation();
                refreshResult(String.valueOf(fourOperation_result));
                refreshText(result);
                break;
            case R.id.button_sqrt:
                double sqrt_result=Math.sqrt(Double.parseDouble(first_num));
                refreshResult(String.valueOf(sqrt_result));
                refreshText(result);
                break;
            default:
                if(result.length()>0&&operator.equals(""))
                {
                    refreshResult("");
                    refreshText("");
                }
                if(operator.equals(""))
                {
                    first_num=first_num+inputText;
                }
                else
                {
                    next_num=next_num+inputText;
                }
                if(showText.equals("0")&&!inputText.equals("."))
                {
                    //整数前面不需要0
                    refreshText(inputText);
                }
                else
                {
                    refreshText(showText+inputText);
                }
        }
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YuJianWeiY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值