栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

制作一个简单的计算器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

制作一个简单的计算器

xml样式


    
    
        

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;
        }}
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340047.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号