<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:gravity="top|center"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="簡單計算器"
android:textColor="#000000"
android:textSize="22sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/shape_white_with_stroke"
android:orientation="vertical">
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right|bottom"
android:lines="3"
android:maxLines="3"
android:scrollbars="vertical"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancel"
style="@style/btn_cal"
android:text="CE" />
<Button
android:id="@+id/btn_divide"
style="@style/btn_cal"
android:text="÷" />
<Button
android:id="@+id/btn_multiply"
style="@style/btn_cal"
android:text="×" />
<Button
android:id="@+id/btn_clear"
style="@style/btn_cal"
android:text="C" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_seven"
style="@style/btn_cal"
android:text="7" />
<Button
android:id="@+id/btn_eight"
style="@style/btn_cal"
android:text="8" />
<Button
android:id="@+id/btn_nine"
style="@style/btn_cal"
android:text="9" />
<Button
android:id="@+id/btn_plus"
style="@style/btn_cal"
android:text="+" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_four"
style="@style/btn_cal"
android:text="4" />
<Button
android:id="@+id/btn_five"
style="@style/btn_cal"
android:text="5" />
<Button
android:id="@+id/btn_six"
style="@style/btn_cal"
android:text="6" />
<Button
android:id="@+id/btn_minus"
style="@style/btn_cal"
android:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_one"
style="@style/btn_cal"
android:text="1" />
<Button
android:id="@+id/btn_two"
style="@style/btn_cal"
android:text="2" />
<Button
android:id="@+id/btn_three"
style="@style/btn_cal"
android:text="3" />
<ImageButton
android:id="@+id/ib_sqrt"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerInside"
android:src="@drawable/sqrt"
android:background="@drawable/btn_nine_selector" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="75dp"
android:orientation="horizontal">
<Button
android:id="@+id/btn_zero"
style="@style/btn_cal"
android:layout_weight="2"
android:text="0" />
<Button
android:id="@+id/btn_dot"
style="@style/btn_cal"
android:text="." />
<Button
android:id="@+id/btn_equal"
style="@style/btn_cal"
android:text="=" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
package com.example.junior;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.junior.util.Arith;
public class CalculatorActivity extends AppCompatActivity implements View.OnClickListener {
private final static String TAG = "CalculatorActivity";
private TextView tv_result; // 聲明一個文本視圖對象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
// 從佈局文件中獲取名叫tv_result的文本視圖
tv_result = findViewById(R.id.tv_result);
// 設置tv_result內部文本的移動方式爲滾動形式
tv_result.setMovementMethod(new ScrollingMovementMethod());
// 下面給每個按鈕控件都註冊了點擊監聽器
findViewById(R.id.btn_cancel).setOnClickListener(this); // “取消”按鈕
findViewById(R.id.btn_divide).setOnClickListener(this); // “除法”按鈕
findViewById(R.id.btn_multiply).setOnClickListener(this); // “乘法”按鈕
findViewById(R.id.btn_clear).setOnClickListener(this); // “清除”按鈕
findViewById(R.id.btn_seven).setOnClickListener(this); // 數字7
findViewById(R.id.btn_eight).setOnClickListener(this); // 數字8
findViewById(R.id.btn_nine).setOnClickListener(this); // 數字9
findViewById(R.id.btn_plus).setOnClickListener(this); // “加法”按鈕
findViewById(R.id.btn_four).setOnClickListener(this); // 數字4
findViewById(R.id.btn_five).setOnClickListener(this); // 數字5
findViewById(R.id.btn_six).setOnClickListener(this); // 數字6
findViewById(R.id.btn_minus).setOnClickListener(this); // “減法”按鈕
findViewById(R.id.btn_one).setOnClickListener(this); // 數字1
findViewById(R.id.btn_two).setOnClickListener(this); // 數字2
findViewById(R.id.btn_three).setOnClickListener(this); // 數字3
findViewById(R.id.btn_zero).setOnClickListener(this); // 數字0
findViewById(R.id.btn_dot).setOnClickListener(this); // “小數點”按鈕
findViewById(R.id.btn_equal).setOnClickListener(this); // “等號”按鈕
findViewById(R.id.ib_sqrt).setOnClickListener(this); // “開平方”按鈕
}
@Override
public void onClick(View v) {
int resid = v.getId(); // 獲得當前按鈕的編號
String inputText;
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) { // 點擊了清除按鈕
clear("");
} else if (resid == R.id.btn_cancel) { // 點擊了取消按鈕
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;
tv_result.setText(showText);
} 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 (caculate()) { // 計算成功,則顯示計算結果
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 = 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, "開根號的數值不能小於0", 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("")) { // 無操作符,則繼續拼接前一個操作數
firstNum = firstNum + inputText;
} else { // 有操作符,則繼續拼接後一個操作數
nextNum = nextNum + inputText;
}
showText = showText + inputText;
tv_result.setText(showText);
}
return;
}
private String operator = ""; // 操作符
private String firstNum = ""; // 前一個操作數
private String nextNum = ""; // 後一個操作數
private String result = ""; // 當前的計算結果
private String showText = ""; // 顯示的文本內容
// 開始加減乘除四則運算,計算成功則返回true,計算失敗則返回false
private boolean caculate() {
if (operator.equals("+")) { // 當前是相加運算
result = String.valueOf(Arith.add(firstNum, nextNum));
} else if (operator.equals("-")) { // 當前是相減運算
result = String.valueOf(Arith.sub(firstNum, nextNum));
} else if (operator.equals("×")) { // 當前是相乘運算
result = String.valueOf(Arith.mul(firstNum, nextNum));
} else if (operator.equals("÷")) { // 當前是相除運算
if ("0".equals(nextNum)) { // 發現被除數是0
// 被除數爲0,要彈窗提示用戶
Toast.makeText(this, "被除數不能爲零", Toast.LENGTH_SHORT).show();
// 返回false表示運算失敗
return false;
} else { // 被除數非0,則進行正常的除法運算
result = String.valueOf(Arith.div(firstNum, nextNum));
}
}
// 把運算結果打印到日誌中
Log.d(TAG, "result=" + result);
firstNum = result;
nextNum = "";
// 返回true表示運算成功
return true;
}
// 清空並初始化
private void clear(String text) {
showText = text;
tv_result.setText(showText);
operator = "";
firstNum = "";
nextNum = "";
result = "";
}
}