package supperRabbit.simpleCalculator;
import java.math.BigDecimal;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class SimpleCalculator extends Activity {
/** Called when the activity is first created. */
private TextView outputForm;
private Button numberZero;
private Button numberOne;
private Button numberTwo;
private Button numberThree;
private Button numberFour;
private Button numberFive;
private Button numberSix;
private Button numberSeven;
private Button numberEight;
private Button numberNine;
private Button pointButton;
private Button clearButton;
private Button deleteButton;
private Button plusButton;
private Button equalButton;
private Button substractButton;
private Button multiplyButton;
private Button divisionButton;
double plusNumber = 0;
int symble = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//获取各个控件
outputForm = (TextView)findViewById(R.id.outputForm);
numberZero = (Button) findViewById(R.id.numberZero);
numberOne = (Button) findViewById(R.id.numberOne);
numberTwo = (Button) findViewById(R.id.numberTwo);
numberThree = (Button) findViewById(R.id.numberThree);
numberFour = (Button) findViewById(R.id.numberFour);
numberFive = (Button) findViewById(R.id.numberFive);
numberSix = (Button) findViewById(R.id.numberSix);
numberSeven = (Button) findViewById(R.id.numberSeven);
numberEight = (Button) findViewById(R.id.numberEight);
numberNine = (Button) findViewById(R.id.numberNine);
pointButton = (Button) findViewById(R.id.pointButton);
clearButton = (Button) findViewById(R.id.clearButton);
deleteButton = (Button)findViewById(R.id.deleteButton);
plusButton = (Button) findViewById(R.id.plusButton);
equalButton = (Button) findViewById(R.id.equalButton);
substractButton = (Button) findViewById(R.id.substractButton);
multiplyButton = (Button) findViewById(R.id.multiplyButton);
divisionButton = (Button) findViewById(R.id.divisionButton);
outputForm.setText("0");
numberZero.setOnClickListener(new MyButtonListener());
numberOne.setOnClickListener(new MyButtonListener());
numberTwo.setOnClickListener(new MyButtonListener());
numberThree.setOnClickListener(new MyButtonListener());
numberFour.setOnClickListener(new MyButtonListener());
numberFive.setOnClickListener(new MyButtonListener());
numberSix.setOnClickListener(new MyButtonListener());
numberSeven.setOnClickListener(new MyButtonListener());
numberEight.setOnClickListener(new MyButtonListener());
numberNine.setOnClickListener(new MyButtonListener());
clearButton.setOnClickListener(new MyButtonListener());
pointButton.setOnClickListener(new MyButtonListener());
deleteButton.setOnClickListener(new MyButtonListener());
plusButton.setOnClickListener(new MyButtonListener());
equalButton.setOnClickListener(new MyButtonListener());
substractButton.setOnClickListener(new MyButtonListener());
multiplyButton.setOnClickListener(new MyButtonListener());
divisionButton.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case(R.id.numberZero):
numberZeroClick();
break;
case(R.id.numberOne):
numberOneToNineClick("1");
break;
case(R.id.numberTwo):
numberOneToNineClick("2");
break;
case(R.id.numberThree):
numberOneToNineClick("3");
break;
case(R.id.numberFour):
numberOneToNineClick("4");
break;
case(R.id.numberFive):
numberOneToNineClick("5");
break;
case(R.id.numberSix):
numberOneToNineClick("6");
break;
case(R.id.numberSeven):
numberOneToNineClick("7");
break;
case(R.id.numberEight):
numberOneToNineClick("8");
break;
case(R.id.numberNine):
numberOneToNineClick("9");
break;
case(R.id.clearButton):
clearButtonClick();
break;
case(R.id.deleteButton):
deleteButtonClick();
break;
case(R.id.pointButton):
pointButtonClick();
break;
case(R.id.plusButton):
plusButtonClick();
break;
case(R.id.substractButton):
substractButtonClick();
break;
case(R.id.multiplyButton):
multiplyButtonClick();
break;
case(R.id.divisionButton):
divisionButtonClick();
break;
case(R.id.equalButton):
equalButtonClick();
break;
}
}
private void equalButtonClick() {
// TODO Auto-generated method stub
String existNumber = outputForm.getText().toString();
double result = 0;
String theResult = "";
double number2 = Double.parseDouble(existNumber);
switch(symble) {
case(1):
if(plusNumber == 315 && number2 == 518) {
theResult = "我爱你!";
} else {
result = plusNumber + number2;
}
break;
case(2):result = plusNumber - number2;
//Log.v("aaa","sdf");
break;
case(3):result = plusNumber * number2;
break;
case(4):result = plusNumber / number2;
break;
}
symble = 0;
plusNumber = 0;
if(theResult == "我爱你!") {
outputForm.setText(theResult);
} else {
//解决textView中输出的double类型结果中小数点的问题 ,例如3*5 输出了15.0
BigDecimal result1 = new BigDecimal(result);
if(result1.compareTo(result1.setScale(0,BigDecimal.ROUND_FLOOR)) == 0) {
theResult = result1.setScale(0,BigDecimal.ROUND_FLOOR).toString();
} else {
theResult = result1.toString();
}
//String theResult = Double.toString(result);
outputForm.setText(theResult);
}
}
private void divisionButtonClick() {
// TODO Auto-generated method stub
String existNumber = outputForm.getText().toString();
double aaa = Double.parseDouble(existNumber);
if(plusNumber == 0) {
plusNumber = aaa;
} else {
plusNumber /= aaa;
}
outputForm.setText("0");
symble = 4;
}
private void multiplyButtonClick() {
// TODO Auto-generated method stub
String existNumber = outputForm.getText().toString();
double aaa = Double.parseDouble(existNumber);
if(plusNumber == 0) {
plusNumber = aaa;
} else {
plusNumber *= aaa;
}
outputForm.setText("0");
symble = 3;
}
private void substractButtonClick() {
// TODO Auto-generated method stub
String existNumber = outputForm.getText().toString();
double aaa = Double.parseDouble(existNumber);
if(plusNumber == 0) {
plusNumber = aaa;
} else {
plusNumber -= aaa;
}
outputForm.setText("0");
symble = 2;
}
private void plusButtonClick() {
// TODO Auto-generated method stub
String existNumber = outputForm.getText().toString();
double aaa = Double.parseDouble(existNumber);
plusNumber += aaa;
outputForm.setText("0");
symble = 1;
}
private void pointButtonClick() {
// TODO Auto-generated method stub
String existNumber = outputForm.getText().toString();
if(existNumber.equals("0")) {
outputForm.setText("0.");
} else if(existNumber.indexOf(".") < 0) {
outputForm.setText(existNumber + ".");
}
}
private void deleteButtonClick() {
// TODO Auto-generated method stub
String existNumber = outputForm.getText().toString();
if(!existNumber.equals("0")) {
String cutNumber = existNumber.substring(0, existNumber.length()-1).toString();
if(cutNumber.equals("")) {
outputForm.setText("0");
} else {
outputForm.setText(cutNumber);
}
}
}
private void clearButtonClick() {
// TODO Auto-generated method stub
outputForm.setText("0");
}
评论2
最新资源