Android计算器的简单实现

Android计算器的简单实现

这个计算器的界面主要分为两个部分:

  1. 文本框——用于显示计算内容和结果;
  2. 按钮——用于输入数据和运算符。
    实现简单的加、减、乘、除四则运算以及开根号(求平方根)的计算。
    成果演示如下:

bilibili成果演示视频

界面设计

界面设计

布局文件如下:
用TextView作为计算内容和结果的显示框,除根号按钮外的数字和运算符为Button控件,因为我们输入的根号(√ )显示不完整,所以这里利用根号截图做了个imageButton。

<?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"
    tools:context=".CalculatorActivity"
    android:gravity="bottom"
    android:padding="5dp">

    <TextView
        android:id="@+id/calculatorname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:gravity="center_horizontal"
        android:text="简单计算器"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="30sp" />

    <TextView
                android:id="@+id/tv_shownumber"
                android:layout_width="match_parent"
                android:layout_height="270dp"
                android:textSize="20sp"
                android:layout_marginBottom="15dp"/>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:orientation="horizontal"
                android:layout_gravity="bottom"
                android:layout_margin="2dp">
                <Button
                    android:id="@+id/btn_CE"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="CE"
                    android:background="@drawable/selector"
                    android:textSize="20sp"/>
                <Button
                    android:id="@+id/btn_divide"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="÷"/>
                <Button
                    android:id="@+id/btn_multiple"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="×"/>
                <Button
                    android:id="@+id/btn_C"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="C"/>
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:orientation="horizontal"
                android:layout_gravity="end"
                android:layout_margin="2dp">
                <Button
                    android:id="@+id/btn_7"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="7"/>
                <Button
                    android:id="@+id/btn_8"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="8"/>
                <Button
                    android:id="@+id/btn_9"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="9"/>
                <Button
                    android:id="@+id/btn_add"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text=""/>
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:orientation="horizontal"
                android:layout_gravity="bottom"
                android:layout_margin="2dp">
                <Button
                    android:id="@+id/btn_4"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="4"/>
                <Button
                    android:id="@+id/btn_5"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="5"/>
                <Button
                    android:id="@+id/btn_6"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="6"/>
                <Button
                    android:id="@+id/btn_reduce"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text=""/>
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:orientation="horizontal"
                android:layout_gravity="bottom"
                android:layout_margin="2dp">
                <Button
                    android:id="@+id/btn_1"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="1"/>
                <Button
                    android:id="@+id/btn_2"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="2"/>
                <Button
                    android:id="@+id/btn_3"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="3"/>
                <ImageButton
                    android:id="@+id/btn_squareroot"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:background="@mipmap/squareroot"
                    />
            </LinearLayout>


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="70dp"
                android:orientation="horizontal"
                android:layout_gravity="bottom"
                android:layout_margin="2dp">
                <Button
                    android:id="@+id/btn_0"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="0"/>
                <Button
                    android:id="@+id/btn_point"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text="."/>
                <Button
                    android:id="@+id/btn_equal"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:textSize="20sp"
                    android:background="@drawable/selector"
                    android:text=""/>
            </LinearLayout>
</LinearLayout>

常规状态下的按钮样式:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <solid android:color="#f1f1f1"/>
    <stroke
        android:width="0.5dp"
        android:color="#c2c2c2"/>
</shape>

按下状态的按钮样式:
按钮为按下状态时加深按钮颜色。

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="5dp"/>
    <solid android:color="#c2c2c2"/>
    <stroke
        android:width="0.5dp"
        android:color="#c2c2c2"/>

</shape>

selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_pressed"/>
    <item android:drawable="@drawable/btn_normal"/>
</selector>

代码实现

package com.example.helloworld;

import androidx.annotation.LongDef;
import androidx.appcompat.app.AppCompatActivity;

import android.icu.math.BigDecimal;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "huahua";
    private TextView tv_shownumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator);
        Button btn_CE = findViewById(R.id.btn_CE);
        Button btn_divide = findViewById(R.id.btn_divide);
        Button btn_multiple = findViewById(R.id.btn_multiple);
        Button btn_C = findViewById(R.id.btn_C);
        Button btn_9 = findViewById(R.id.btn_9);
        Button btn_8 = findViewById(R.id.btn_8);
        Button btn_7 = findViewById(R.id.btn_7);
        Button btn_6 = findViewById(R.id.btn_6);
        Button btn_5 = findViewById(R.id.btn_5);
        Button btn_4 = findViewById(R.id.btn_4);
        Button btn_3 = findViewById(R.id.btn_3);
        Button btn_2 = findViewById(R.id.btn_2);
        Button btn_1 = findViewById(R.id.btn_1);
        Button btn_0 = findViewById(R.id.btn_0);
        Button btn_add = findViewById(R.id.btn_add);
        Button btn_reduce = findViewById(R.id.btn_reduce);
        ImageButton btn_squareroot = findViewById(R.id.btn_squareroot);
        Button btn_point = findViewById(R.id.btn_point);
        Button btn_equal = findViewById(R.id.btn_equal);
        tv_shownumber = findViewById(R.id.tv_shownumber);
        btn_CE.setOnClickListener(this);
        btn_divide.setOnClickListener(this);
        btn_multiple.setOnClickListener(this);
        btn_C.setOnClickListener(this);
        btn_9.setOnClickListener(this);
        btn_8.setOnClickListener(this);
        btn_7.setOnClickListener(this);
        btn_6.setOnClickListener(this);
        btn_5.setOnClickListener(this);
        btn_4.setOnClickListener(this);
        btn_3.setOnClickListener(this);
        btn_2.setOnClickListener(this);
        btn_1.setOnClickListener(this);
        btn_0.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        btn_reduce.setOnClickListener(this);
        btn_squareroot.setOnClickListener(this);
        btn_point.setOnClickListener(this);
        btn_equal.setOnClickListener(this);

        tv_shownumber.setMovementMethod(new ScrollingMovementMethod());
        tv_shownumber.setGravity(Gravity.RIGHT|Gravity.BOTTOM);

    }

    private String operator = "";
    private String firstNum = "";
    private String nextNum = "";
    private String result = "";
    private String showText = "";


    private boolean calculate(){
        if(operator.equals("+")){
            result = String.valueOf(BigDecimalUtil.add(firstNum,nextNum));
        }else if(operator.equals("-")){
            result = String.valueOf(BigDecimalUtil.sub(firstNum,nextNum));
        }else if(operator.equals("×")){
            result = String.valueOf(BigDecimalUtil.mul(firstNum,nextNum));
        }else if(operator.equals("÷")){
            if(nextNum.equals("0")){
                Toast.makeText(this,"被除数不能为0!",Toast.LENGTH_SHORT).show();
                return false;
            }else{
                result = String.valueOf(BigDecimalUtil.div(firstNum,nextNum));
            }

        }
        firstNum = result;
        nextNum = "";
        return true;
    }

    private void clear(String text){
        showText = text;
        tv_shownumber.setText(showText);
        operator = "";
        firstNum = "";
        nextNum = "";
        result = "";
    }


    @Override
    public void onClick(View v) {
        int resID = v.getId();
        String inputText;
        if(resID == R.id.btn_squareroot){
            inputText = "√";
        }else {
            inputText = ((TextView)v).getText().toString();
        }
        Log.d(TAG, "resID" + resID + "inputText" + inputText);


        if(resID == R.id.btn_CE){
            clear("") ;
        }else if(resID == R.id.btn_C){
            if(operator.equals("")) {
                if (firstNum.length() == 1) {
                    firstNum = "0";                                          
                } else if (firstNum.length() > 1) {
                    firstNum = firstNum.substring(0, firstNum.length() - 1);
                } else {
                    Toast.makeText(this, "没有可以取消的数字了", Toast.LENGTH_SHORT).show();
                }
                showText = firstNum;
                tv_shownumber.setText(showText);
            }else if(!operator.equals("") ){
                if(nextNum.length() == 1){
                    nextNum = "";                                            
                }else if(nextNum.length() > 1){
                    nextNum = nextNum.substring(0,nextNum.length()-1);
                }else if(nextNum.length() <= 0){
                    Toast.makeText(this,"没有可以取消的数字了",Toast.LENGTH_SHORT);
                }
                showText = showText.substring(0,showText.length()-1);
                tv_shownumber.setText(showText);
            }
        }else if(resID == R.id.btn_equal){
            if(operator.length() == 0 || operator.equals("=")){
                Toast.makeText(this,"请输入运算符",Toast.LENGTH_SHORT).show();
            }else if(nextNum.length() <= 0){
                Toast.makeText(this,"请输入数字",Toast.LENGTH_SHORT).show();
            }
            if(calculate()){
                firstNum = result;
                operator = inputText;
                showText = showText + "=" +result;
                tv_shownumber.setText(showText);
            }
        }else if(resID == R.id.btn_add || resID == R.id.btn_reduce || resID == R.id.btn_multiple || resID == R.id.btn_divide){

            if(firstNum.length() <= 0){
                Toast.makeText(this,"请输入数字",Toast.LENGTH_SHORT).show();
            }
            if(operator.length() == 0 || operator.equals("=") || operator.equals("√")){                                      
                operator = inputText;
                showText = showText + operator;
                tv_shownumber.setText(showText);
            }else {
                showText = showText + operator;
                tv_shownumber.setText(showText);
            }

        }else if(resID == R.id.btn_squareroot){
            if(firstNum.length() <= 0){
                Toast.makeText(this,"请输入数字",Toast.LENGTH_SHORT).show();
            }else if(Double.parseDouble(firstNum) < 0){
                Toast.makeText(this,"开根号的数值不能小于0",Toast.LENGTH_SHORT).show();
            }
            result = String.valueOf(Math.sqrt(Double.parseDouble(firstNum)));
            firstNum = result;
            nextNum = "";
            operator = inputText;
            showText = showText + "√=" +result;
            tv_shownumber.setText(showText);
            Log.d(TAG, "result="+result+",firstNum="+firstNum+",operator="+operator);
        }else{
            if(operator.equals("=")){
                operator = "";
                firstNum = "";
                showText = "";
            }
            if(resID == R.id.btn_point){
                inputText = ".";
            }
            if(operator.equals("")){
                firstNum = firstNum + inputText;
            }else {
                nextNum = nextNum +inputText;
            }
            showText = showText + inputText;
            tv_shownumber.setText(showText);
        }
    }
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值