核心思想:
監聽按鈕的onClick()
:
- 獲取按下的按鈕上的
String
- 然後對數字、小數點、運算符、根號、等號、C、CE、做出不同的響應
- 每次運算都要進行刷新操作數和運算符,設置計算框的操作
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv_result; // 結果框
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_result = findViewById(R.id.tv_result);
// 文本移動方式爲滾動
tv_result.setMovementMethod(new ScrollingMovementMethod());
......
}
@Override
public void onClick(View v) {
String inputText;
int resid = v.getId();
// 根號
if (resid == R.id.ib_sqrt) {
inputText = "√";
} else {
inputText = ((TextView) v).getText().toString();
}
Log.d(TAG, "resid = " + resid + " , inputText=" + inputText);
if (resid == R.id.btn_clear) {
// C
clear("");
Toast.makeText(this, "歸零", Toast.LENGTH_SHORT).show();
} else if (resid == R.id.btn_cancel) {
// CE
if (operator.equals("")) {
// 沒有操作符,逐位取消前一個操作數
if (firstNum.length() == 1) {
firstNum = "0";
} else if (firstNum.length() > 0) {
firstNum = firstNum.substring(0, firstNum.length() - 1);
} else {
Toast.makeText(this, "沒有可以取消的數字", Toast.LENGTH_SHORT).show();
return;
}
showText = firstNum;
} else {
// 有操作符,逐位取消後一個數字
if (nextNum.length() == 1) {
nextNum = "";
} else if (nextNum.length() > 0) {
nextNum = nextNum.substring(0, nextNum.length() - 1);
} else {
Toast.makeText(this, "運算符不可取消", Toast.LENGTH_SHORT).show();
return;
}
showText = showText.substring(0, showText.length() - 1);
}
tv_result.setText(showText);
} else if (resid == R.id.btn_equal) {
// =
if (operator.length() == 0 || operator.equals("=")) {
Toast.makeText(this, "請輸入運算符", Toast.LENGTH_SHORT).show();
return;
} else if (nextNum.length() <= 0) {
Toast.makeText(this, "要輸入第二個數才能運算", Toast.LENGTH_SHORT).show();
return;
}
if (calculate()) {
operator = inputText;
showText = showText + "=" + result;
tv_result.setText(showText);
} else {
return;
}
} else if (resid == R.id.btn_plus || resid == R.id.btn_minus || resid == R.id.btn_multiply || resid == R.id.btn_divide) {
// +-*/
if (firstNum.length() <= 0) {
Toast.makeText(this, "先輸入數字再輸入運算符", Toast.LENGTH_SHORT).show();
return;
}
if (operator.length() == 0 || operator.equals("=") || operator.equals("√")) {
operator = inputText;
showText += operator;
tv_result.setText(showText);
} else {
Toast.makeText(this, "不支持連續運算,請輸入數字", Toast.LENGTH_SHORT).show();
return;
}
} else if (resid == R.id.ib_sqrt) {
// √
if (firstNum.length() <= 0) {
Toast.makeText(this, "先輸入數字才能開根號", Toast.LENGTH_SHORT).show();
return;
}
if (Double.parseDouble(firstNum) < 0) {
Toast.makeText(this, "開根號的數值不能爲負數", Toast.LENGTH_SHORT).show();
return;
}
result = String.valueOf(Math.sqrt(Double.parseDouble(firstNum)));
firstNum = result;
nextNum = "";
operator = inputText;
showText = showText + "√=" + result;
tv_result.setText(showText);
Log.d(TAG, "result = " + result + " ,firstNum = " + firstNum + " ,operator = " + operator);
} else {
// 數字和小數點
if (operator.equals("=")) {
operator = "";
firstNum = "";
showText = "";
}
if (resid == R.id.btn_dot) {
inputText = ".";
}
if (operator.equals("")){
if(firstNum.contains(".") && inputText.equals(".")){
return;
}
firstNum += inputText;
}else {
if(nextNum.contains(".") && inputText.equals(".")){
return;
}
nextNum += inputText;
}
showText += inputText;
tv_result.setText(showText);
}
return;
}
private boolean calculate() {
switch (operator) {
case "+":
result = Arith.add(firstNum, nextNum);
break;
case "-":
result = Arith.sub(firstNum, nextNum);
break;
case "×":
result = Arith.mul(firstNum, nextNum);
break;
case "÷":
if (Double.parseDouble(nextNum) == 0) {
Toast.makeText(this, "被除數不能爲0", Toast.LENGTH_SHORT).show();
return false;
} else {
result = Arith.div(firstNum, nextNum);
}
break;
}
Log.d(TAG, "result = " + result);
firstNum = result;
nextNum = "";
return true;
}
private String showText = ""; // 顯示的文本內容
private String firstNum = ""; // 前一個操作數
private String nextNum = ""; // 後一個操作數
private String result = ""; // 當前計算結果
private String operator = ""; // 操作符
private void clear(String text) {
showText = text;
tv_result.setText(showText);
operator = "";
firstNum = "";
nextNum = "";
result = "";
}
}
實現效果: