一款簡單的Android計算器源代碼
聲明:本文代碼是在潔子博客http://blog.sina.com.cn/tiankong29源代碼的基礎上修改過來的。由於他的代碼在我的機器上出現很多錯誤,所以略作修改。使之更加完善。在這裏感謝潔子的無私奉獻!
效果圖:
源代碼如下:
MainActivity.java:
public class MainActivity extends ActionBarActivity {
private Button[] btnNum = new Button[11];// 數值按鈕
private Button[] btnCommand = new Button[5];// 符號按鈕
private EditText editText = null;// 顯示區域
private Button btnClear = null; // clear按鈕
private static String lastCommand; // 用於保存運算符
private boolean clearFlag; // 用於判斷是否清空顯示區域的值,true需要,false不需要
private boolean firstFlag; // 用於判斷是否是首次輸入,true首次,false不是首次
private double result; // 計算結果
public MainActivity() {
// 初始化各項值
result = 0; // x的值
firstFlag = true; // 是首次運算
clearFlag = false; // 不需要清空
lastCommand = "="; // 運算符
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 獲取運算符
btnCommand[0] = (Button) findViewById(R.id.add);
btnCommand[1] = (Button) findViewById(R.id.subtract);
btnCommand[2] = (Button) findViewById(R.id.multiply);
btnCommand[3] = (Button) findViewById(R.id.divide);
btnCommand[4] = (Button) findViewById(R.id.equal);
// 獲取數字
btnNum[0] = (Button) findViewById(R.id.num0);
btnNum[1] = (Button) findViewById(R.id.num1);
btnNum[2] = (Button) findViewById(R.id.num2);
btnNum[3] = (Button) findViewById(R.id.num3);
btnNum[4] = (Button) findViewById(R.id.num4);
btnNum[5] = (Button) findViewById(R.id.num5);
btnNum[6] = (Button) findViewById(R.id.num6);
btnNum[7] = (Button) findViewById(R.id.num7);
btnNum[8] = (Button) findViewById(R.id.num8);
btnNum[9] = (Button) findViewById(R.id.num9);
btnNum[10] = (Button) findViewById(R.id.point);
// 初始化顯示結果區域
editText=(EditText) findViewById(R.id.Result);
editText.setText("0.0");
// 實例化監聽器對象
NumberAction na = new NumberAction();
CommandAction ca = new CommandAction();
for (Button bc : btnCommand) {
bc.setOnClickListener((OnClickListener) ca);
}
for (Button bc : btnNum) {
bc.setOnClickListener((OnClickListener) na);
}
// clear按鈕的動作
btnClear = (Button) findViewById(R.id.clear);
btnClear.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
editText.setText("0.0");
// 初始化各項值
result = 0; // x的值
firstFlag = true; // 是首次運算
clearFlag = false; // 不需要清空
lastCommand = "="; // 運算符
}
});
}
//數字按鈕監聽器
private class NumberAction implements OnClickListener{
@Override
public void onClick(View view) {
Button btn = (Button) view;
String input = btn.getText().toString();
if (firstFlag) { // 首次輸入
// 一上就".",就什麼也不做
if (input.equals(".")) {
return;
}
// 如果是"0.0"的話,就清空
if (editText.getText().toString().equals("0.0")) {
editText.setText("");
}
firstFlag = false;// 改變是否首次輸入的標記值
} else {
String editTextStr = editText.getText().toString();
// 判斷顯示區域的值裏面是否已經有".",如果有,輸入的又是".",就什麼都不做
if (editTextStr.indexOf(".") != -1 && input.equals(".")) {
return;
}
// 判斷顯示區域的值裏面只有"-",輸入的又是".",就什麼都不做
if (editTextStr.equals("-") && input.equals(".")) {
return;
}
// 判斷顯示區域的值如果是"0",輸入的不是".",就什麼也不做
if (editTextStr.equals("0") && !input.equals(".")) {
return;
}
}
// 如果我點擊了運算符以後,再輸入數字的話,就要清空顯示區域的值
if (clearFlag) {
editText.setText("");
clearFlag = false;// 還原初始值,不需要清空
}
editText.setText(editText.getText().toString() + input);// 設置顯示區域的值
}
}
//符號按鈕監聽器
private class CommandAction implements OnClickListener{
@Override
public void onClick(View view) {
Button btn = (Button) view;
String inputCommand = (String) btn.getText();
if (firstFlag) {// 首次輸入"-"的情況
if (inputCommand.equals("-")) {
editText.setText("-");// 顯示區域的內容設置爲"-"
firstFlag = false;// 改變首次輸入的標記
}
} else {
if (!clearFlag) {// 如果flag=false不需要清空顯示區的值,就調用方法計算
calculate(Double.parseDouble(editText.getText().toString()));// 保存顯示區域的值,並計算
}
// 保存你點擊的運算符
lastCommand = inputCommand;
clearFlag = true;// 因爲我這裏已經輸入過運算符,
}
}
}
// 計算用的方法
public void calculate(double x) {
//
Object lastCommand = null;
if (lastCommand.equals("+")) {
result += x;
} else if (lastCommand.equals("-")) {
result -= x;
} else if (lastCommand.equals("*")) {
result *= x;
} else if (lastCommand.equals("/")) {
result /= x;
} else if (lastCommand.equals("=")) {
result = x;
}
editText.setText("" + result);
}
}
activity_main.xml文件:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
tools:context="it.heima.cal.MainActivity"
android:stretchColumns="1"
android:weightSum="6"
android:orientation="vertical"
>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<EditText
android:id="@+id/Result"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_span="4"
android:gravity="right|bottom"
android:cursorVisible="false"
android:inputType="none"
android:singleLine="true"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button
android:id="@+id/num7"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="7"
android:layout_weight="1"
/>
<Button
android:id="@+id/num8"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="8"
android:layout_weight="1"
/>
<Button
android:id="@+id/num9"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="9"
android:layout_weight="1"
/>
<Button
android:id="@+id/divide"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="/"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/num4"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="4"
android:layout_weight="1"
/>
<Button
android:id="@+id/num5"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
/>
<Button
android:id="@+id/num6"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="6"
android:layout_weight="1"
/>
<Button
android:id="@+id/multiply"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="*"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<Button android:id="@+id/num1"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="1"
android:layout_weight="1"
/>
<Button
android:id="@+id/num2"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="2"
android:layout_weight="1"
/>
<Button
android:id="@+id/num3"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="3"
android:layout_weight="1"
/>
<Button
android:id="@+id/subtract"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="-"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/num0"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="0"
android:layout_weight="1"
/>
<Button
android:id="@+id/point"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="."
android:layout_weight="1"
/>
<Button
android:id="@+id/add"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="+"
android:layout_weight="1"
/>
<Button
android:id="@+id/equal"
android:layout_width="0px"
android:layout_height="fill_parent"
android:text="="
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:id="@+id/clear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="clear"
android:layout_span="4"
android:gravity="center_vertical|center_horizontal"
/>
</TableRow>
</TableLayout>
由於初學,通過這個小例子的學習我掌握了很多小技巧,特別是對佈局有了更加清晰的認識!感謝網上無私奉獻的大牛們!