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

Android View 點擊事件的四種寫法

編輯:關於 Android 程式設計

為控制項綁定點擊事件,Android 至少提供了四種做法。它們的效果相同,適用情境卻不太一樣,值得一次弄清楚。

四種寫法

  1. 具名內部類別:另立一個 class 實作 OnClickListener。
  2. 匿名內部類別:在 setOnClickListener 中直接 new 一個,最常見的寫法。
  3. Activity 實作介面:讓 Activity implements OnClickListener,再由 switch 判斷是哪個控制項。
  4. 版面檔綁定:在 XML 上寫 android:onClick=" methodName ",方法簽名必須是 public void xxx(View v)。

前兩者適合單一控制項的簡單情境;第三者在控制項眾多時會讓 onCreate 之外多出一個巨大的 switch;第四者最精簡,缺點是方法名寫死在 XML 裡,改名時容易被漏掉,編譯器不會提醒。

程式碼

package com.example.test;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    Button bt_call;
    private EditText tv_phonenumber;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bt_call = (Button) findViewById(R.id.call);
        tv_phonenumber = (EditText) MainActivity.this.findViewById(R.id.et_phonenumber);

        // 寫法一:具名內部類別
        // bt_call.setOnClickListener(new myListener());

        // 寫法二:匿名內部類別
        // bt_call.setOnClickListener(new OnClickListener() {
        //     @Override
        //     public void onClick(View v) {
        //         callphone();
        //     }
        // });

        // 寫法三:Activity 實作介面
        // bt_call.setOnClickListener(this);
    }

    // 寫法四:由版面檔的 android:onClick 呼叫
    public void callButtonClicked(View view) {
        callphone();
    }

    private class myListener implements OnClickListener {
        @Override
        public void onClick(View v) {
            callphone();
        }
    }

    private void callphone() {
        String number = tv_phonenumber.getText().toString().trim();
        if (TextUtils.isEmpty(number)) {
            Toast.makeText(MainActivity.this, "請輸入手機號碼", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);
        intent.setData(Uri.parse("tel:" + number));
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.call:
                callphone();
                break;
        }
    }
}

版面檔

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_phonenumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:inputType="phone" />

    <Button
        android:id="@+id/call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/et_phonenumber"
        android:layout_marginTop="16dp"
        android:onClick="callButtonClicked"
        android:text="@string/bt_call" />
</RelativeLayout>

這裡有兩個實務上的提醒。第一,Intent.ACTION_CALL 會直接撥出電話,需要 CALL_PHONE 權限,而且屬於敏感權限,在較新的 Android 版本上必須執行期動態申請;若只想跳到期待撥號畫面而不直接撥出,改用 Intent.ACTION_DIAL 就不需要任何權限。第二,輸入號碼後記得 trim() 並檢查空值,否則會帶著空白字元去組 URI。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved