我制作了一个计算器应用程序,其代码如下:
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;
}
});
}
}
我遇到了一个问题,如果其中一个EditTexts没有文本,它会得到一个stackoverflow。 我是一个初学者,一个中学生,我一直在寻找信息,但我找不到它。请不要从我这里扣除积分。
如何在中间停止应用程序并在EditText edit1中发送一条消息,说明“需要更多变量”?请在下面的sqrt中举例说明。数字来自edit1,也应该输出到edit1。谢谢。
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;
}
});
答案 0 :(得分:0)
您可以尝试setOnTouchListener
而不是使用setOnClickListener
,这样就可以避免多次调用计算过程。
此外,您可以检查一个editText
是否为空。
尝试TextUtils.isEmpty(string)
答案 1 :(得分:0)
在继续计算之前检查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;
}
干杯。