Android实现简单计算器

本文实例为大家分享了Android实现简单计算器的具体代码,供大家参考,具体内容如下

功能

1、加减乘除四则运算
2、归0
3、回退
4、即时运算

配置

在build.gradle(app) 中加入下面的代码

buildFeatures {
        viewBinding = true
    }

加入位置如下所示

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
buildFeatures {
    viewBinding = true
}

布局

Android实现简单计算器_第1张图片



    
    
    
        
            

代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String tv="0";
    private ActivityMainBinding inflate;
    private String count="";
    private boolean isMax=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflate = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(inflate.getRoot());
        inflate.zero.setOnClickListener(this);
        inflate.yi.setOnClickListener(this);
        inflate.er.setOnClickListener(this);
        inflate.san.setOnClickListener(this);
        inflate.si.setOnClickListener(this);
        inflate.wu.setOnClickListener(this);
        inflate.liu.setOnClickListener(this);
        inflate.qi.setOnClickListener(this);
        inflate.ba.setOnClickListener(this);
        inflate.jiu.setOnClickListener(this);
        inflate.add.setOnClickListener(this);
        inflate.sub.setOnClickListener(this);
        inflate.mul.setOnClickListener(this);
        inflate.div.setOnClickListener(this);
        inflate.point.setOnClickListener(this);
        inflate.equal.setOnClickListener(this);
        inflate.dec.setOnClickListener(this);
        inflate.ac.setOnClickListener(this);
    }
    private void jisuanqi(){
        List strs=new ArrayList<>();
        StringBuilder f= new StringBuilder();
        for(int i=0;i n,int v) {
        double con = 0;
        int j=0;
        con=Double.valueOf(n.get(0));
        if(v==0) {
            for(int i=0;ii+1) {
                        j=i+1;
                    }
                }
                if(n.get(j).equals("x")){
                    n.set(i, String.valueOf(con*Double.valueOf(n.get(i+2))));
                    n.remove(i+1);
                    n.remove(i+1);
                    return number(n,0);
                }else if(n.get(j).equals("/")){
                    if(Double.valueOf(n.get(i+2))==0){
                        isMax=true;
                        return 10;
                    }
                    n.set(i, String.valueOf(con/Double.valueOf(n.get(i+2))));
                    n.remove(i+1);
                    n.remove(i+1);
                    return number(n,0);
                }
            }
        }
        for(int i=0;ii+1) {
                    j=i+1;
                }
            }
            if(n.get(j).equals("+")){
                n.set(i, String.valueOf(con+Double.valueOf(n.get(i+2))));
                n.remove(i+1);
                n.remove(i+1);
                return number(n,1);

            }else if(n.get(j).equals("-")){
                n.set(i, String.valueOf(con-Double.valueOf(n.get(i+2))));
                n.remove(i+1);
                n.remove(i+1);
                return number(n,1);
            }

        }
        return con;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.point:
                if(String.valueOf(tv.charAt(tv.length()-1)).matches("\\d+")){
                    tv+=".";
                    inflate.tv.setText(tv);
                }
                break;
            case R.id.zero:
            case R.id.yi:
            case R.id.er:
            case R.id.san:
            case R.id.si:
            case R.id.wu:
            case R.id.liu:
            case R.id.qi:
            case R.id.ba:
            case R.id.jiu:
                if(tv.equals("0")){
                    tv="";
                }
                Button bt_digit=findViewById(v.getId());
                tv+=bt_digit.getText();
                inflate.tv.setText(tv);
                count=tv;
                jisuanqi();
                break;
            case R.id.sub:
                if(String.valueOf(tv.charAt(0)).equals("0")){
                    tv="-";
                }else {
                    if(String.valueOf(tv.charAt(tv.length()-1)).equals(".")){
                        tv += "0";
                    }
                    tv+="-";
                }
                showText();
                break;
            case R.id.add:
            case R.id.mul:
            case R.id.div:
                Button bt_operator=findViewById(v.getId());
                String laststr=String.valueOf(tv.charAt(tv.length()-1));
                if(!laststr.matches("(\\d+)") ) {
                    tv = tv.substring(0, tv.length() - 1) + bt_operator.getText();
                }else {
                    if(laststr.equals(".")) {
                        tv += "0";
                    }
                    tv+=bt_operator.getText();
                }

                showText();
                break;
            case R.id.ac:
                tv="0";
                inflate.tv.setText(tv);
                break;
            case R.id.dec:
                if(tv.length()>1) {
                    tv = tv.substring(0, tv.length() - 1);
                }else {
                    tv="0";
                }
                jisuanqi();
                break;
            case R.id.equal:
                if(!String.valueOf(tv.charAt(tv.length()-1)).matches("(\\d+)")){
                    tv = tv.substring(0, tv.length() - 1);
                }
                count=tv;
                jisuanqi();
                tv=count;
                inflate.tv.setText(tv);
                break;
        }
    }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android实现简单计算器)