Android Studio实现一个计算器

Java

package com.example.ex2_9;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.material.bottomappbar.BottomAppBar;

import java.util.zip.DeflaterOutputStream;

public class MainActivity extends AppCompatActivity {
    TextView txtView;
    Button btnClear;
    Button btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9;
    Button btnDot,btnAdd,btnSub,btnMul,btnDiv,btnEq;
    //只能有一个小数点
    boolean hasDot = false;
    //标志位
    boolean rlt = false;

    double LV = 0, RV;
    String opt = "+";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtView = findViewById(R.id.txtView);
        btnClear = findViewById(R.id.btnClear);
        //        加事件
        btnClear.setOnClickListener(new mBtnClear());
        //给1-9添加对象
        btn0 = findViewById(R.id.btn0);
        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        btn3 = findViewById(R.id.btn3);
        btn4 = findViewById(R.id.btn4);
        btn5 = findViewById(R.id.btn5);
        btn6 = findViewById(R.id.btn6);
        btn7 = findViewById(R.id.btn7);
        btn8 = findViewById(R.id.btn8);
        btn9 = findViewById(R.id.btn9);
        //添加事件监听器
        btn0.setOnClickListener(new mBtnNum());
        btn1.setOnClickListener(new mBtnNum());
        btn2.setOnClickListener(new mBtnNum());
        btn3.setOnClickListener(new mBtnNum());
        btn4.setOnClickListener(new mBtnNum());
        btn5.setOnClickListener(new mBtnNum());
        btn6.setOnClickListener(new mBtnNum());
        btn7.setOnClickListener(new mBtnNum());
        btn8.setOnClickListener(new mBtnNum());
        btn9.setOnClickListener(new mBtnNum());

        btnDot = findViewById(R.id.btnDot);
        btnDot.setOnClickListener(new mBtnDot());
        hasDot = false;

        btnAdd = findViewById(R.id.btnAdd);
        btnSub = findViewById(R.id.btnSub);
        btnMul = findViewById(R.id.btnMul);
        btnDiv = findViewById(R.id.btndiv);
        btnEq = findViewById(R.id.btnEq);

        btnAdd.setOnClickListener(new mBtnOpt());
        btnDiv.setOnClickListener(new mBtnOpt());
        btnSub.setOnClickListener(new mBtnOpt());
        btnMul.setOnClickListener(new mBtnOpt());
        btnEq.setOnClickListener(new mBtnOpt());
    }
        //添加内联类
    private class mBtnClear implements View.OnClickListener {

        @Override
        public void onClick(View view) {
            txtView.setText("0");
            hasDot = false;
            LV=0;
            opt = "+";
            rlt = false;
        }
    }
    //显示框部分显示数字
    private class mBtnNum implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            if(rlt){
                txtView.setText("0");
                rlt = false;
            }
            //因为v没有getText()方法
            Button btn = (Button)v;
            if(txtView.getText().toString().equals("0")){
                txtView.setText(btn.getText().toString());
            }else{
                txtView.setText(txtView.getText().toString() + btn.getText().toString());
            }
            //txtView.setText(txtView.getText().toString() + btn.getText());

        }
    }
    //判断只有一个小数点
    private class mBtnDot implements View.OnClickListener {
        @Override
        public void onClick(View view) {
            if(rlt){
                txtView.setText("0");
                rlt = false;
            }//?????????

            if(!hasDot){
                //第一个可能就是小数点
                txtView.setText(txtView.getText().toString() + ".");
                hasDot = true;
            }
        }
    }

        //加法
    private class mBtnOpt implements View.OnClickListener {
            @Override
            public void onClick(View v) {
                //所有输入的都是RV
                RV = Double.parseDouble(txtView.getText().toString());
                if(opt.equals("+")){
                    LV = LV + RV;
                }else if (opt.equals("-")){
                    LV = LV - RV;
                }else if (opt.equals("*")){
                    LV = LV * RV;
                }else if (opt.equals("/")){
                    if(RV == 0){
                        LV = 0;
                    }
                    else LV = LV / RV;
                }
                else if(opt.equals("=")){
                    LV = LV + RV;
                }
                //转字符串
                txtView.setText("" + LV);

                //覆盖opt
                opt = ((Button)v).getText().toString();

                if(opt.equals("="))opt = "+";
                RV = 0;//?

                rlt = true;
            }
        }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:columnCount="4"
    android:rowCount="6"
    tools:context=".MainActivity"
    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="24sp"
        android:gravity="left"
        android:layout_columnSpan="4" />
    <Button
        android:id="@+id/btnClear"
        android:text="清除"
        android:textSize="24sp"
        android:layout_columnSpan="4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/btn1"
        android:text="1"
        android:textSize="24sp"
        android:layout_row="2"
        android:layout_column="0"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn2"
        android:text="2"
        android:textSize="24sp"
        android:layout_row="2"
        android:layout_column="1"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn3"
        android:text="3"
        android:textSize="24sp"
        android:layout_row="2"
        android:layout_column="2"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn4"
        android:text="4"
        android:textSize="24sp"
        android:layout_row="3"
        android:layout_column="0"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btnADD"
        android:text="+"
        android:textSize="24sp"
        android:layout_row="2"
        android:layout_column="3"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn5"
        android:text="5"
        android:textSize="24sp"
        android:layout_row="3"
        android:layout_column="1"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn6"
        android:text="6"
        android:textSize="24sp"
        android:layout_row="3"
        android:layout_column="2"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btnSub"
        android:text="-"
        android:textSize="24sp"
        android:layout_row="3"
        android:layout_column="3"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn7"
        android:text="7"
        android:textSize="24sp"
        android:layout_row="4"
        android:layout_column="0"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn8"
        android:text="8"
        android:textSize="24sp"
        android:layout_row="4"
        android:layout_column="1"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn9"
        android:text="9"
        android:textSize="24sp"
        android:layout_row="4"
        android:layout_column="2"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btnMul"
        android:text="*"
        android:textSize="24sp"
        android:layout_row="4"
        android:layout_column="3"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btn0"
        android:text="0"
        android:textSize="24sp"
        android:layout_row="5"
        android:layout_column="0"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btnDot"
        android:text="."
        android:textSize="24sp"
        android:layout_row="5"
        android:layout_column="1"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btnEq"
        android:text="="
        android:textSize="24sp"
        android:layout_row="5"
        android:layout_column="2"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/btnDiv"
        android:text="/"
        android:textSize="24sp"
        android:layout_row="5"
        android:layout_column="3"
        android:width="90sp"
        android:layout_height="wrap_content"
        />
</GridLayout>
posted @ 2022-04-02 13:34  晚觉入海  阅读(504)  评论(0)    收藏  举报
Document