Java 计算器应用程序在溢出前停止并显示消息

Java 计算器应用程序在溢出前停止并显示消息,java,android,android-sdk-tools,Java,Android,Android Sdk Tools,我制作了一个计算器应用程序,其代码如下: package com.example.calc; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivi

我制作了一个计算器应用程序,其代码如下:

package com.example.calc;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;



public class MainActivity extends Activity {
    EditText edit1, edit2;
    Button btnAdd, btnSub, btnMul, btnDiv, btnSin, btnCos, btnTan, btnSqrt, btnSqr, btnFact, btnPi, btnE, btnLog, btnLn, btnMod, btnInt, btnAsin, btnAcos, btnAtan, btnSinh, btnCosh, btnTanh, btn3root, btnNroot, btnSin1, btnCos1, btnTan1, btnSinh1, btnCosh1, btnTanh1;
    Double result;
    String num1, num2;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle("Easy Scientific Calculator");
        edit1=(EditText)findViewById(R.id.Edit1);
        edit2=(EditText)findViewById(R.id.Edit2);
        btnAdd=(Button)findViewById(R.id.BtnAdd);
        btnSub=(Button)findViewById(R.id.BtnSub);
        btnMul=(Button)findViewById(R.id.BtnMul);
        btnDiv=(Button)findViewById(R.id.BtnDiv);
        btnSin=(Button)findViewById(R.id.BtnSin);
        btnCos=(Button)findViewById(R.id.BtnCos);
        btnTan=(Button)findViewById(R.id.BtnTan);
        btnSqrt=(Button)findViewById(R.id.BtnSqrt);
        btnSqr=(Button)findViewById(R.id.BtnSqr);
        btnFact=(Button)findViewById(R.id.BtnFact);
        btnPi=(Button)findViewById(R.id.BtnPi);
        btnE=(Button)findViewById(R.id.BtnE);
        btnLog=(Button)findViewById(R.id.BtnLog);
        btnLn=(Button)findViewById(R.id.BtnLn);
        btnMod=(Button)findViewById(R.id.BtnMod);
        btnInt=(Button)findViewById(R.id.BtnInt);
        btnAsin=(Button)findViewById(R.id.BtnAsin);
        btnAcos=(Button)findViewById(R.id.BtnAcos);
        btnAtan=(Button)findViewById(R.id.BtnAtan);
        btnSinh=(Button)findViewById(R.id.BtnSinh);
        btnCosh=(Button)findViewById(R.id.BtnCosh);
        btnTanh=(Button)findViewById(R.id.BtnTanh);
        btn3root=(Button)findViewById(R.id.Btn3root);
        btnNroot=(Button)findViewById(R.id.BtnNroot);
        btnSin1=(Button)findViewById(R.id.BtnSin1);
        btnCos1=(Button)findViewById(R.id.BtnCos1);
        btnTan1=(Button)findViewById(R.id.BtnTan1);
        btnSinh1=(Button)findViewById(R.id.BtnSinh1);
        btnCosh1=(Button)findViewById(R.id.BtnCosh1);
        btnTanh1=(Button)findViewById(R.id.BtnTanh1);

        btnAdd.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=Double.parseDouble(num1)+Double.parseDouble(num2);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
             }
        });
        btnSub.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=Double.parseDouble(num1)-Double.parseDouble(num2);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnMul.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=Double.parseDouble(num1)*Double.parseDouble(num2);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnDiv.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=Double.parseDouble(num1)/Double.parseDouble(num2);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnSin.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.sin((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnCos.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.cos((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnTan.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.tan((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnSqrt.setOnTouchListener(new View.OnTouchListener(){
             public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                num1=edit1.getText().toString();
                    result=Math.sqrt(Double.parseDouble(num1));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
             }
        });
        btnSqr.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=Math.pow(Double.parseDouble(num1), Double.parseDouble(num2));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnFact.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    Double r=1.0;
                    Double v=Double.parseDouble(num1); 
                    for (int i = 1; i <= v; i++) 
                    r *= i; 
                    result=r;
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnPi.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    result=Math.PI;
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnE.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    result=Math.E;
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnLog.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=Math.log10(Double.parseDouble(num1))/Math.log10(Double.parseDouble(num2));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnLn.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.log(Double.parseDouble(num1));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnMod.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=(Double.parseDouble(num1))%(Double.parseDouble(num2));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnInt.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.floor(Double.parseDouble(num1));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnAsin.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=57.29577951308232087679815481410517033240547246657*Math.asin(Double.parseDouble(num1));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnAcos.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=57.29577951308232087679815481410517033240547246657*Math.acos(Double.parseDouble(num1));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnAtan.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=57.29577951308232087679815481410517033240547246657*Math.atan(Double.parseDouble(num1));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnSinh.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.sinh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnCosh.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.cosh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnTanh.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.tanh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btn3root.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=Math.pow((Double.parseDouble(num1)), 1.0/3);
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnNroot.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    num2=edit2.getText().toString();
                    result=Math.pow((Double.parseDouble(num1)), 1.0/(Double.parseDouble(num2)));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnSin1.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=1/(Math.sin((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnCos1.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=1/(Math.cos((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnTan1.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=1/(Math.tan((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
        });
        btnSinh1.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=1/(Math.sinh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnTanh.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=1/(Math.cosh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
        btnTanh.setOnTouchListener(new View.OnTouchListener(){
            public boolean onTouch(View arg0, MotionEvent arg1){
                int action = arg1.getActionMasked();
                if(action==MotionEvent.ACTION_UP)
                {
                    num1=edit1.getText().toString();
                    result=1/(Math.tanh((Double.parseDouble(num1))/57.29577951308232087679815481410517033240547246657));
                    edit1.setText(Double.toString(result));
                    return false;
                }
                else
                    return false;
            }
    });
    }
}

您可以尝试使用
setOnClickListener
而不是使用
setOnTouchListener,这样可以避免多次调用计算过程

您还可以检查一个
editText
是否为空


在继续计算之前,请尝试
TextUtils.isEmpty(string)

检查EditText是否为空:

num1 = edit1.getText().toString();
if(num1 == null || num1.isEmpty()) {
   // Give your error message that this EditText is empty
   return false;
}
此外,最好检查是否给出了有效的Double,以避免出现NumberFormatException

try { 
   result = Double.parseDouble(num1) - Double.parseDouble(num2);
} catch (NumberFormatException nfe) {
   // Give your error messages
   return false;
}

干杯。

谢谢!我最终通过使用TextUtils.isEmpty(字符串)修复了它。
try { 
   result = Double.parseDouble(num1) - Double.parseDouble(num2);
} catch (NumberFormatException nfe) {
   // Give your error messages
   return false;
}