package com.fang.mobileqq; import com.fang.mobileqq.R; import com.fang.mobileqq.R.id; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class cal extends Activity implements OnClickListener{ EditText t1; Button bt1,bt2,bt3,bt4,bt5,bt6,bt7,bt8,bt9,bt0; Button btplus,btminus,btmultiply,btdivide,btpoint,btclear,btdel,btequal; boolean clear_flag;//清空标识 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt0=(Button) findViewById(R.id.button16); bt1=(Button) findViewById(R.id.button13); bt2=(Button) findViewById(R.id.button14); bt3=(Button) findViewById(R.id.button15); bt4=(Button) findViewById(R.id.button9); bt5=(Button) findViewById(R.id.button10); bt6=(Button) findViewById(R.id.button11); bt7=(Button) findViewById(R.id.button5); bt8=(Button) findViewById(R.id.button6); bt9=(Button) findViewById(R.id.button7); btplus=(Button) findViewById(R.id.button12); btminus=(Button) findViewById(R.id.button8); btmultiply=(Button) findViewById(R.id.button4); btdivide=(Button) findViewById(R.id.button3); btpoint=(Button) findViewById(R.id.button17); btclear=(Button) findViewById(R.id.button1); btdel=(Button) findViewById(R.id.button2); btequal=(Button) findViewById(R.id.button18); t1=(EditText) findViewById(R.id.editText1); bt0.setOnClickListener(this); bt1.setOnClickListener(this); bt2.setOnClickListener(this); bt3.setOnClickListener(this); bt4.setOnClickListener(this); bt5.setOnClickListener(this); bt6.setOnClickListener(this); bt7.setOnClickListener(this); bt8.setOnClickListener(this); bt9.setOnClickListener(this); btplus.setOnClickListener(this); btminus.setOnClickListener(this); btmultiply.setOnClickListener(this); btdivide.setOnClickListener(this); btpoint.setOnClickListener(this); btclear.setOnClickListener(this); btdel.setOnClickListener(this); btequal.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub String str=t1.getText().toString(); switch(v.getId()){ case R.id.button16: case R.id.button13: case R.id.button14: case R.id.button15: case R.id.button9: case R.id.button10: case R.id.button11: case R.id.button5: case R.id.button6: case R.id.button7: case R.id.button17: if(clear_flag){ clear_flag=false; str=""; t1.setText(""); } t1.setText(str+((Button)v).getText()); break; case R.id.button12: case R.id.button8: case R.id.button4: case R.id.button3: if(clear_flag){ clear_flag=false; str=""; t1.setText(""); } t1.setText(str+" "+((Button)v).getText()+" "); break; case R.id.button2: if(clear_flag){ clear_flag=false; str=""; t1.setText(""); } else if(str!=null&&!str.equals("")){ t1.setText(str.substring(0,str.length()-1)); } break; case R.id.button1: clear_flag=false; str=""; t1.setText(" "); break; case R.id.button18: getResult(); break; } } private void getResult(){ String exp=t1.getText().toString(); if(exp==null||exp.equals(" ")){ return; } if(!exp.contains(" ")){ return; } if(clear_flag){ clear_flag=false; return; } clear_flag=true; double result=0; String s1=exp.substring(0,exp.indexOf("")); String op=exp.substring(exp.indexOf("")+1,exp.indexOf("")+2); String s2=exp.substring(exp.indexOf("")+3); if(!s1.equals("")&&!s2.equals("")){ double d1=Double.parseDouble(s1); double d2=Double.parseDouble(s2); if(op.equals("+")){ result=d1+d2; }else if(op.equals("-")){ result=d1-d2; }else if(op.equals("*")){ result=d1*d2; }else if(op.equals("/")){ if(d2==0){ result=0;}else{ result=d1/d2; } } if(!s1.contains(".")&&!s2.contains(".")&&!op.equals('/')){ int r=(int)result; t1.setText(r+""); }else{ t1.setText(result+"");} }else if(!s1.equals("")&&s2.equals("")){ t1.setText(exp); }else if(s1.equals("")&&!s2.equals("")){ double d2=Double.parseDouble(s2); if(op.equals("+")){ result=0+d2; }else if(op.equals("-")){ result=0-d2; }else if(op.equals("*")){ result=0; }else if(op.equals("/")){ result=0; } if(!s2.contains(".")){ int r=(int)result; t1.setText(r+" "); }else{ t1.setText(result+" "); } }else{ t1.setText(""); } } }
Listener呆
相关分类