Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 基礎複習:三種寫法實作撥號功能

編輯:關於 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 自己實作介面

讓 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);
    }
}

寫法三:XML 直接綁定方法名稱

最省事的一種,在配置檔的 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. 下一篇文章:
Copyright © Android教程網 All Rights Reserved