制作一个简单的计算器

xml样式
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:textSize="40dp"
        android:id="@+id/tv_show"
        android:textColor="@color/teal_200"
        android:textStyle="bold"
        android:text="0"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="c" />
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="DELETE" />
        <Button
            android:id="@+id/button3"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="/" />
    </LinearLayout>



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button4"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="7"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button5"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="8"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button6"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="9"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button7"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="X" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button8"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="4"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button9"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="5"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button10"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="6"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button11"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="+" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button12"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="1"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button13"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="2"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button14"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="3"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button15"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="-" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/button16"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="."
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button17"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="0"
            android:onClick="MyOnClick"/>
        <Button
            android:id="@+id/button18"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="=" />

    </LinearLayout>
</LinearLayout>
java代码
package com.example.myapplication4;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

    private Button button4,button5,button6,button8,button9,button10,
            button12,button13,button14,button17;
    private TextView tv_show;
    private Button button_qinchu,button_shanchu,button_chu,button_jia,button_jian,button_cheng,
            button_dian,button_den;
    private boolean isChar=false;
    private StringBuffer strA=new StringBuffer(),strB=new StringBuffer();
    char operator;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }
    public  void  init(){
        button_qinchu=this.findViewById(R.id.button1);
        button_shanchu=this.findViewById(R.id.button2);
        button_chu=this.findViewById(R.id.button3);
        button4=this.findViewById(R.id.button4);
        button5=this.findViewById(R.id.button5);
        button6=this.findViewById(R.id.button6);
        button_cheng=this.findViewById(R.id.button7);
        button8=this.findViewById(R.id.button8);
        button9=this.findViewById(R.id.button9);
        button10=this.findViewById(R.id.button10);
        button_jia=this.findViewById(R.id.button11);
        button12=this.findViewById(R.id.button12);
        button13=this.findViewById(R.id.button13);
        button14=this.findViewById(R.id.button14);
        button_jian=this.findViewById(R.id.button15);
        button_dian=this.findViewById(R.id.button16);
        button17=this.findViewById(R.id.button17);
        button_den=this.findViewById(R.id.button18);

        tv_show=this.findViewById(R.id.tv_show);

        button_qinchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isChar==false){
                    strA=new StringBuffer();
                }else {
                    strB=new StringBuffer();
                }
                tv_show.setText("0");
            }
        });
        button_shanchu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isChar==false) {
                    if(strA.length()==0){
                        tv_show.setText("0");
                    }else{
                    strA.deleteCharAt(strA.length()-1);
                    tv_show.setText(strA.toString());}
                    if(strA.length()==0){
                        tv_show.setText("0");
                    }
                }
                else{
                    if(strB.length()==0){
                        tv_show.setText("0");
                    }
                    else{
                        strB.deleteCharAt(strB.length()-1);
                        tv_show.setText(strB.toString());}
                    if(strB.length()==0){
                        tv_show.setText("0");
                    }
                }
            }




        });

        button_jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_show.setText("+");
                isChar=true;
                operator=0;
            }
        });
        button_jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_show.setText("-");
                operator = 0;
                isChar=true;
                operator=1;
            }
        });
        button_cheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_show.setText("乘");
                operator = 0;
                isChar=true;
                operator=2;
            }
        });
        button_chu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_show.setText("除");
                operator = 0;
                isChar=true;
                operator=3;
            }
        });
        button_den.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (strA.length()==0&&strB.length()==0){
                    strA.append(0);
                    strB.append(0);
                }else if(strA.length()==0){
                    strA.append(0);
                }else if (strB.length()==0){
                    strB.append(0);
                }

                double one=Double.parseDouble(strA.toString());
                double two=Double.parseDouble(strB.toString());
                double sum=0;
                switch (operator){
                    case 0:
                        isChar=false;
                        sum=one+two;
                        break;
                    case 1:
                        isChar=false;
                        sum=one-two;
                        break;
                    case 2:
                        isChar=false;
                        sum=one*two;
                        break;
                    case 3:
                        isChar=false;
                        sum=one/two;
                        break;
                }

                tv_show.setText(sum+"");
                strA = new StringBuffer();
                strB = new StringBuffer();
//                表示从第二轮计算开始,又是从第一个(digA)数开始赋值
                isChar = false;
            }
        });
    }

    public void MyOnClick(View v) {
        if (isChar==false){switch (v.getId()) {
            case R.id.button4:

                strA.append(7);
                tv_show.setText(strA);
                break;
            case R.id.button5:
                strA.append(8);
                tv_show.setText(strA);
                break;
            case R.id.button6:
                strA.append(9);
                tv_show.setText(strA);
                break;
            case R.id.button8:
                strA.append(4);
                tv_show.setText(strA);
                break;
            case R.id.button9:
                strA.append(5);
                tv_show.setText(strA);
                break;
            case R.id.button10:
                strA.append(6);
                tv_show.setText(strA);
                break;
            case R.id.button12:
                strA.append(1);
                tv_show.setText(strA);
                break;
            case R.id.button13:
                strA.append(2);
                tv_show.setText(strA);
                break;
            case R.id.button14:
                strA.append(3);
                tv_show.setText(strA);
                break;
            case R.id.button16:
                strA.append(".");
                tv_show.setText(strA);
                break;
            case R.id.button17:
                strA.append(0);
                tv_show.setText(strA);
                break;
        }}else {switch (v.getId()) {
            case R.id.button4:
                strB.append(7);
                tv_show.setText(strB);
                break;
            case R.id.button5:
                strB.append(8);
                tv_show.setText(strB);
                break;
            case R.id.button6:
                strB.append(9);
                tv_show.setText(strB);
                break;
            case R.id.button8:
                strB.append(4);
                tv_show.setText(strB);
                break;
            case R.id.button9:
                strB.append(5);
                tv_show.setText(strB);
                break;
            case R.id.button10:
                strB.append(6);
                tv_show.setText(strB);
                break;
            case R.id.button12:
                strB.append(1);
                tv_show.setText(strB);
                break;
            case R.id.button13:
                strB.append(2);
                tv_show.setText(strB);
                break;
            case R.id.button14:
                strB.append(3);
                tv_show.setText(strB);
                break;
            case R.id.button16:
                strA.append(".");
                tv_show.setText(strB);
                break;
            case R.id.button17:
                strB.append(0);
                tv_show.setText(strB);
                break;
        }}
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值