編輯:關於 Android 程式設計
動手做一個計算器,我把它拆成兩大部分:界面設計與功能實作。兩者其實緊密相連——界面怎麼設計,功能就得怎麼配合。

(成品效果圖)
元件:界面共 19 個按鈕,包含數字 1-9、小數點、加減乘除、清空與回退、百分比、等號,另外還有一個用來顯示內容的文字方塊。做法很直觀,直接拖進 XML 即可。
布局:剛拖進來的元件排列凌亂,需要重新調整。本範例採用
LinearLayout進行布局。樣式:包含背景色、邊框顏色與圓角。
先來看樣式檔。

幾個常用標籤的含義:gradient 對應漸層,startColor、endColor 決定起訖色,android:angle 決定漸層起始角度;solid 是純色填充;stroke 是描邊;corners 是圓角;padding 定義內容與邊界的距離,等同 android:padding_left 這類屬性。
先看顯示內容的文字方塊樣式 white_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 圓角 -->
<corners android:radius="5dp" />
<!-- 邊框線 -->
<stroke android:color="#000" android:width="1dp" />
<!-- 填充 -->
<solid android:color="#FFF" />
</shape>
接著是白色按鈕的樣式 white_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/white_bg" />
<item android:drawable="@drawable/gray_bg" android:state_pressed="true" />
</selector>
第一個 item 引用 white_bg.xml,作為預設狀態的樣式;第二個 item 引用 gray_bg.xml,並透過 android:state_pressed="true" 指定為按下時的樣式。gray_bg.xml 的內容如下:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#ccc" />
</shape>
再來看橘色按鈕的樣式 orange_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ashen" android:state_pressed="true" />
<item android:drawable="@drawable/orange_bg" />
</selector>
第一個 item 引用 ashen.xml,對應按下時的狀態:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#F5F5F5" />
</shape>
第二個 item 引用 orange_bg.xml,作為預設樣式:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<corners android:radius="5dp" />
<solid android:color="#FF6100" />
</shape>
最後在 activity_main.xml 中分別為元件套用 white_bg、white_selector、orange_selector 即可。
寫邏輯之前,先把按鈕分類,關係釐清了程式就好寫:
1、數字 0-9 與小數點
2、加減乘除
3、清除與回退
4、等號
5、百分比
public class MainActivity extends Activity implements View.OnClickListener {
Button btn_0, btn_1, btn_2, btn_3, btn_4, btn_5, btn_6, btn_7, btn_8, btn_9; // 數字 0-9
Button btn_dian; // 小數點
Button btn_C, btn_del; // 清除與回退
Button btn_yu; // 取餘
Button btn_chu; // 除法
Button btn_cheng; // 乘法
Button btn_jia; // 加法
Button btn_jian; // 減法
Button btn_deng; // 等號
EditText input; // 顯示內容的文字方塊
boolean clear_ed; // 是否需要清空文字方塊
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_C = (Button) findViewById(R.id.btn_C);
btn_del = (Button) findViewById(R.id.btn_del);
btn_yu = (Button) findViewById(R.id.btn_yu);
btn_chu = (Button) findViewById(R.id.btn_chu);
btn_cheng = (Button) findViewById(R.id.btn_cheng);
btn_jia = (Button) findViewById(R.id.btn_jia);
btn_jian = (Button) findViewById(R.id.btn_jian);
btn_deng = (Button) findViewById(R.id.btn_deng);
btn_dian = (Button) findViewById(R.id.btn_dian);
btn_9 = (Button) findViewById(R.id.btn_9);
btn_8 = (Button) findViewById(R.id.btn_8);
btn_7 = (Button) findViewById(R.id.btn_7);
btn_6 = (Button) findViewById(R.id.btn_6);
btn_5 = (Button) findViewById(R.id.btn_5);
btn_4 = (Button) findViewById(R.id.btn_4);
btn_3 = (Button) findViewById(R.id.btn_3);
btn_2 = (Button) findViewById(R.id.btn_2);
btn_1 = (Button) findViewById(R.id.btn_1);
btn_0 = (Button) findViewById(R.id.btn_0);
input = (EditText) findViewById(R.id.input);
btn_0.setOnClickListener(this);
btn_1.setOnClickListener(this);
btn_2.setOnClickListener(this);
btn_3.setOnClickListener(this);
btn_4.setOnClickListener(this);
btn_5.setOnClickListener(this);
btn_6.setOnClickListener(this);
btn_7.setOnClickListener(this);
btn_8.setOnClickListener(this);
btn_9.setOnClickListener(this);
btn_yu.setOnClickListener(this);
btn_chu.setOnClickListener(this);
btn_cheng.setOnClickListener(this);
btn_jia.setOnClickListener(this);
btn_jian.setOnClickListener(this);
btn_deng.setOnClickListener(this);
btn_dian.setOnClickListener(this);
btn_C.setOnClickListener(this);
btn_del.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String str = input.getText().toString();
if (str.equals("0")) {
str = "";
}
switch (v.getId()) {
case R.id.btn_0:
case R.id.btn_1:
case R.id.btn_2:
case R.id.btn_3:
case R.id.btn_4:
case R.id.btn_5:
case R.id.btn_6:
case R.id.btn_7:
case R.id.btn_8:
case R.id.btn_9:
case R.id.btn_dian:
if (clear_ed) {
clear_ed = false;
str = "";
input.setText("0"); // 重置為 0
}
input.setText(str + ((Button) v).getText()); // 依按鈕文字追加內容
break;
case R.id.btn_jia:
case R.id.btn_jian:
case R.id.btn_cheng:
case R.id.btn_chu:
// 運算子前後刻意留空格,方便後續解析
input.setText(str + " " + ((Button) v).getText() + " ");
break;
case R.id.btn_C:
input.setText("0"); // 重置為 0
break;
case R.id.btn_del:
if (input.length() <= 1 || input.getText().toString().equals("")) {
input.setText("0");
} else {
// 刪掉最後一個字元
input.setText(str.substring(0, str.length() - 1));
}
break;
case R.id.btn_deng:
getResult();
break;
case R.id.btn_yu:
Double d3 = Double.parseDouble(str);
Double d4 = d3 * 0.01;
input.setText(d4 + "");
break;
}
}
private void getResult() {
String exp = input.getText().toString();
if (exp == null || exp.equals("")) {
return;
}
// 沒有預留的空格分隔符,代表還不是一個完整算式
if (!exp.contains(" ")) {
return;
}
clear_ed = 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("÷")) {
result = (d2 == 0) ? 0 : d1 / d2; // 分母為 0 時結果給 0
}
// 依是否含小數點決定輸出整數或浮點數
if (!s1.contains(".") && !s2.contains(".")) {
int r = (int) result;
input.setText(r + "");
} else {
input.setText(result + "");
}
} else if (!s1.equals("") && s2.equals("")) {
input.setText(exp); // 右側缺數字,原樣輸出
} else if (s1.equals("") && s2.equals("")) {
input.setText(exp); // 兩側皆空,原樣輸出
} else if (s1.equals("") && !s2.equals("")) {
// 左側缺數字,以 0 代入
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;
input.setText(r + "");
} else {
input.setText(result + "");
}
}
}
}
整體思路並不複雜:把輸入的字串當成一個待解析的算式,用空白作為運算子分隔的依據,按下等號時切分出左右運算元與運算子,計算後再依是否含小數點決定輸出格式。
這只是入門練習,後續還有優化空間,例如改成能處理連續運算、加入運算優先順序,或改用 RxJava、雙向解析等更嚴謹的做法。程式碼中的註解我先補到這個程度,剩下的留給自己慢慢打磨。
Launcher3 壁紙機制解析:從選擇、裁切到動態壁紙
壁紙是系統能力 Launcher 的壁紙設定並非 Launcher 獨有,而是透過系統 WallpaperManager 完成。因此,不只是 Launcher,許多第三方
第八章:Activity 與任務調用棧深度解析
知識點目錄 8.1 Activity 8.1.1 起源 8.1.2 Activity 形態 8.1.3 生命週期 8.2 Activity 任務棧簡介 8.3 Acti
Android熱補丁動態修復實踐全流程解析
前言 好幾個月前,Android App 熱補丁修復火過一陣,起因是 QQ 空間團隊那篇《安卓 App 熱補丁動態修復技術介紹》,隨後各大廠的開源項目層出不窮。本文的實踐
Android 基礎:selector 狀態列表背景用法
在 Android 開發中,selector 常用來為 View 建立不同狀態下的背景效果。例如按鈕在按下、選中、無法使用時,可以顯示不同樣式,讓使用者更清楚目前操作狀態