简单的计算器编写 基于Android 2.2
package com.jiajia.calculate; import java.io.IOException; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class CalculateActivity extends Activity { /** Called when the activity is first created. */ //定义能显示的最大个数 public static final int Maxindex=14; public static final double MAX=999999999999.9; private Button button1,button2,button3,button4,button5, button6,button7,button8,button9,button0,button00,buttonadd, buttonsub,buttonmul,buttondiv,buttonM_add,buttonM_sub, buttonMR,buttonMC,buttonclear,buttondot,buttonequale,buttonBack; private TextView Text1; private TextView Text21; private TextView Text22; private TextView Text31; private TextView Text32; private String factor1="0"; private String factor2="0"; private String memory=""; private String result=""; private String M1=""; private String M2=""; private String M3=""; private String op=""; private String mop=""; private double d_result=0.0; private double d_M1=0.0; private double d_M2=0.0; private double d_M3=0.0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button0=(Button)findViewById(R.id.button0); button0.setOnClickListener(new listener()); button00=(Button)findViewById(R.id.button00); button00.setOnClickListener(new listener()); buttondot=(Button)findViewById(R.id.buttondot); buttondot.setOnClickListener(new listener()); buttonequale=(Button)findViewById(R.id.buttonequale); buttonequale.setOnClickListener(new listener()); button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new listener()); button2=(Button)findViewById(R.id.button2); button2.setOnClickListener(new listener()); button3=(Button)findViewById(R.id.button3); button3.setOnClickListener(new listener()); button4=(Button)findViewById(R.id.button4); button4.setOnClickListener(new listener()); button5=(Button)findViewById(R.id.button5); button5.setOnClickListener(new listener()); button6=(Button)findViewById(R.id.button6); button6.setOnClickListener(new listener()); button7=(Button)findViewById(R.id.button7); button7.setOnClickListener(new listener()); button8=(Button)findViewById(R.id.button8); button8.setOnClickListener(new listener()); button9=(Button)findViewById(R.id.button9); button9.setOnClickListener(new listener()); buttonadd=(Button)findViewById(R.id.buttonadd); buttonadd.setOnClickListener(new listener()); buttonsub=(Button)findViewById(R.id.buttonsub); buttonsub.setOnClickListener(new listener()); buttonmul=(Button)findViewById(R.id.buttonmul); buttonmul.setOnClickListener(new listener()); buttondiv=(Button)findViewById(R.id.buttondiv); buttondiv.setOnClickListener(new listener()); buttonM_add=(Button)findViewById(R.id.buttonM_add); buttonM_add.setOnClickListener(new listener()); buttonM_sub=(Button)findViewById(R.id.buttonM_sub); buttonM_sub.setOnClickListener(new listener()); buttonMR=(Button)findViewById(R.id.buttonMR); buttonMR.setOnClickListener(new listener()); buttonMC=(Button)findViewById(R.id.buttonMC); buttonMC.setOnClickListener(new listener()); buttonclear=(Button)findViewById(R.id.buttonclear); buttonclear.setOnClickListener(new listener()); buttonBack=(Button)findViewById(R.id.buttonback); buttonBack.setOnClickListener(new buttonBack()); Text1=(TextView)findViewById(R.id.TextView1); Text21=(TextView)findViewById(R.id.TextView21); Text22=(TextView)findViewById(R.id.TextView22); Text31=(TextView)findViewById(R.id.TextView31); Text32=(TextView)findViewById(R.id.TextView32); Text1.setText("0"); Text21.setText(""); Text22.setText(""); Text31.setText(""); Text32.setText("0"); } class buttonBack implements OnClickListener{ public void onClick(View arg0) { //onKeyDown(KeyEvent.KEYCODE_BACK, null); AlertDialog.Builder builder=new Builder(CalculateActivity.this); builder.setTitle(R.string.Dia_Title); builder.setMessage(R.string.Dia_msg); builder.setPositiveButton(R.string.Dia_Yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); CalculateActivity.this.finish(); } }); builder.setNegativeButton(R.string.Dia_Cancle, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.dismiss(); } }); builder.create().show(); } } /* public void clearWallpaper(){ try { super. clearWallpaper(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ class listener implements android.view.View.OnClickListener{ //获取操作符计算结果 public void operator(String op,double d1,double d2){ if(op.compareTo("+")==0){d_result=d1+d2;} if(op.compareTo("-")==0){d_result=d1-d2;} if(op.compareTo("x")==0){d_result=d1*d2;} if(op.compareTo("/")==0&&d2!=0){d_result=d1/d2;} if(op.compareTo("/")==0&&d2==0){this.errorOp(); } } //获取代表M的各个View的值 public void getViewM(){ M1=Text1.getText().toString(); M2=Text22.getText().toString(); if(M2.equals("")||M2.equals(" ")||M2.equals(null)){M2="0";} M3=Text32.getText().toString(); op=Text31.getText().toString(); d_M1=Double.parseDouble(M1); d_M2=Double.parseDouble(M2); d_M3=Double.parseDouble(M3); } //出错处理 public void errorOp(){ d_result=0; factor1=""; factor2="0"; result="0"; Text21.setText("E"); Text32.setText( factor2); } //判断result是否符合计算结果 public void judgeRes(){ if((result.endsWith(".0"))){ result= result.substring(0, result.lastIndexOf(".")); } if(result.length()>Maxindex&& d_result<=MAX){ result=result.substring(0,Maxindex); } if(d_result>MAX){ this.errorOp(); } } //相当于等号操作的个操作符运算(包括等号) public void equale(){ this.getViewM(); this.operator(op, d_M2, d_M3); result=String.valueOf(d_result); this.judgeRes(); Text32.setText(result); System.out.println(" result = "+ result); Text31.setText(op); // result=factor1; factor1="0"; factor2=""; } //连续的操作符运算 public void CouOP(String op) { M3=Text32.getText().toString(); Text31.setText(op); factor1=M3; Text22.setText(factor1); factor1="0"; factor2="0"; Text32.setText(factor2); } //M操作的运算 public void MOperator(double d_M2,double d_M3,String mop){ this.operator(op, d_M2, d_M3); if(mop.equals("+")){d_result+=d_M1; } if(mop.equals("-")){d_result=d_M1-d_result;} result=String.valueOf(d_result); this.judgeRes(); Text32.setText(result); Text1.setText(result); Text22.setText(""); factor1=""; factor2=""; } //点击事件 public void onClick(View v) { switch(v.getId()){ case R.id.button0: Text21.setText(""); if(factor1.equals("0")){ factor1="0"; Text32.setText(factor1); }else{ factor1+="0"; Text32.setText(factor1); } break; case R.id.button1: Text21.setText(""); if(factor1.equals("0")){ factor1="1"; Text32.setText(factor1); }else{ factor1+="1"; Text32.setText(factor1); } result=factor1; this.judgeRes(); break; case R.id.button2: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="2"; Text32.setText(factor1); }else{ factor1+="2"; Text32.setText(factor1); } result=factor1; this.judgeRes(); break; case R.id.button3: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="3"; Text32.setText(factor1); }else{ factor1+="3"; Text32.setText(factor1); } result=factor1; this.judgeRes(); break; case R.id.button4: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="4"; Text32.setText(factor1); }else{ factor1+="4"; Text32.setText(factor1); } break; case R.id.button5: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="5"; Text32.setText(factor1); }else{ factor1+="5"; Text32.setText(factor1); } break; case R.id.button6: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="6"; Text32.setText(factor1); }else{ factor1+="6"; Text32.setText(factor1); } break; case R.id.button7: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="7"; Text32.setText(factor1); }else{ factor1+="7"; Text32.setText(factor1); } break; case R.id.button8: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="8"; Text32.setText(factor1); }else{ factor1+="8"; Text32.setText(factor1); } break; case R.id.button9: Text21.setText(""); if(factor1.compareTo("0")==0){ factor1="9"; Text32.setText(factor1); }else{ factor1+="9"; Text32.setText(factor1); } break; //00不能出现在最前面 case R.id.button00: Text21.setText(""); if(factor1.equals("0")||factor1.equals("")){ System.out.println("factor1 = "+factor1); factor1="0"; Text32.setText(factor1); }else{ factor1+="00"; Text32.setText(factor1); } break; //. case R.id.buttondot: Text21.setText(""); if(!factor1.contains(".")){ factor1+="."; Text32.setText(factor1);} // else if((factor1.startsWith("0", 0))){ // factor1="0."; // } else{ Text32.setText(factor1); } break; case R.id.buttonadd: Text21.setText(""); M2=Text22.getText().toString(); if(M2.equals("")){ op="+"; this.CouOP(op); } else{ this.equale(); Text22.setText(result); Text32.setText(factor1); op="+"; Text31.setText(op); } break; case R.id.buttonsub: Text21.setText(""); M2=Text22.getText().toString(); if(M2.equals("")){ op="-"; this.CouOP(op); } else { this.equale(); Text22.setText(result); Text32.setText(factor1); op="-"; Text31.setText(op); } break; case R.id.buttonmul: M2=Text22.getText().toString(); if(M2.equals("")){ op="x"; this.CouOP(op); }else { this.equale(); Text22.setText(result); Text32.setText(factor1); op="x"; Text31.setText(op); } break; case R.id.buttondiv: M2=Text22.getText().toString(); if(M2.equals("")){ op="/"; this.CouOP(op); }else { this.equale(); Text22.setText(result); Text32.setText(factor1); op="/"; Text31.setText(op); } break; //M+ //当作M2=“ ”||“”||null的时候 出现了问题。。因为直接调用的getview //所以里面带有将M2转化为d_M2的时候就出问题了,,那是不能之间转化的 case R.id.buttonM_add: mop="+"; this.getViewM(); this.MOperator(d_M2,d_M3,mop); break; //M- case R.id.buttonM_sub: mop="-"; this.getViewM(); this.MOperator(d_M2,d_M3,mop); break; //MR :将View1中的数据显示在View3中 case R.id.buttonMR: this.getViewM(); Text1.setText(M1); d_result=d_M1; result=String.valueOf(d_result); this.judgeRes(); Text32.setText(result); case R.id.buttonMC: // factor1=Text1.getText().toString(); factor1="0"; factor2="0"; d_result=0; Text1.setText(factor1); break; //清除view3中数据 case R.id.buttonclear: factor1="0"; op=""; factor2="0"; Text21.setText(""); Text32.setText("0"); Text22.setText(""); Text31.setText(op); break; case R.id.buttonequale: this.equale(); Text22.setText(factor2); op="="; Text31.setText(op); break; } } } }简单的计算器编写 基于Android 2.2
内容版权声明:除非注明,否则皆为本站原创文章。