編輯:關於 Android 程式設計
撥打電話是 Android 入門階段很值得練習的小功能,因為它同時涵蓋了控制項操作、Intent 意圖、事件監聽與權限宣告這幾個核心觀念。本文用同一個需求,示範三種不同的事件處理寫法,剛好可以拿來對照理解。
畫面上放三個元件:一個提示文字、一個輸入號碼的 EditText、一個「撥打號碼」的 Button。使用者輸入號碼後按下按鈕,就跳到系統撥號介面。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/a" />
<EditText
android:id="@+id/num_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/c" />
<Button
android:id="@+id/call_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b" />
</LinearLayout>
字串資源放在 strings.xml 中:
<resources>
<string name="app_name">PhoneTest</string>
<string name="a">請輸入電話號碼</string>
<string name="b">撥打號碼</string>
<string name="c">請在這裡輸入號碼</string>
</resources>
要在 AndroidManifest.xml 中加上撥號權限,否則程式執行到 startActivity 就會被系統攔下:
<uses-permission android:name="android.permission.CALL_PHONE"/>
補充一個實務上的差異:Intent.ACTION_CALL 會直接撥出,屬於高風險操作,在 Android 6.0 以後還需要執行時授權(runtime permission);若只想把號碼帶到撥號盤由使用者確認,改用 Intent.ACTION_DIAL 就完全不需要這項權限,也更符合使用者預期。
最直覺的方式,在 onCreate 裡 new 一個 OnClickListener 匿名類別:
bt_call.setOnClickListener(new MyOnClickListener());
private class MyOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
String phoneNum = et_num.getText().toString();
// 撥號核心:三步驟
Intent intent = new Intent(); // 1. 建立意圖
intent.setAction(Intent.ACTION_CALL); // 2. 指定動作:撥電話
intent.setData(Uri.parse("tel:" + phoneNum)); // 指定資料:號碼
startActivity(intent); // 3. 送出意圖
}
}
讓 Activity 直接 implements OnClickListener,把 this 傳給按鈕。程式碼看起來更乾淨,缺點是當畫面上有很多按鈕時,所有事件都會擠在同一個 onClick 裡,需要用 view.getId() 分流。
public class MainActivity2 extends Activity implements OnClickListener {
private EditText et_num;
private Button bt_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_num = (EditText) findViewById(R.id.num_et);
bt_call = (Button) findViewById(R.id.call_bt);
bt_call.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String phoneNum = et_num.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNum));
startActivity(intent);
}
}
最省事的一種,在配置檔的 Button 上寫 android:onClick="hello",然後在 Activity 中提供一個接收 View 參數的 public 方法即可,完全不需要寫 setOnClickListener。
<Button
android:id="@+id/call_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/b"
android:onClick="hello" />
public void hello(View v) {
String phoneNum = et_num.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + phoneNum));
startActivity(intent);
}
事件處理邏輯簡單、只被一個元件使用時,用匿名內部類最直觀;同一個 Activity 內事件數量不多時,讓 Activity 實作介面可以少一層縮排;而 XML 綁定的好處是宣告清楚、Activity 更精簡,缺點是方法名寫死在配置檔裡,重構時較難被 IDE 追蹤到。實際專案中可以依團隊習慣混用,重點是同一個專案內保持一致的風格。
微信如何轉發文章?分享給好友與朋友圈教學
在微信閱讀到喜歡的文章時,可透過右上角選單分享給朋友或分享到朋友圈。1. 開啟文章,點擊右上角三點選單。2. 選擇分享到朋友圈或傳送給好友。3. 確認發送後,即完成轉發。
Android 消息處理機制淺析——Looper、Handler 與 Message
題外話 說來有些慚愧,我對這三者的初步認識,居然是在背面試題的時候。那時我接觸 Android 的時間還不長,讀的書也只是比較適合入門的《瘋狂 Android 講義》。書
Android 檔案上傳實作:圖片與檔案進度更新
實作概念 在 Android 中實作檔案上傳,通常需要先取得要上傳的檔案來源,例如系統檔案選擇器、相機、錄影機或錄音機,再將檔案內容以 multipart/form-d
Android Fragment 生命周期詳解:與 Activity 的對應關係
上一篇談了 Fragment 的基礎用法,這次把焦點放在它的生命周期。Fragment 的行為看似複雜,其實只要抓住一條主線就不難理解:Fragment 的命運完全依附於