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

自定義 View 時如何加上自己的監聽器

編輯:關於 Android 程式設計

監聽器在 Java 裡無所不在。寫自定義控制項時,往往需要對外通報某些狀態變化,這時就得自己定義監聽器。它與 C++ 的回呼函式概念相通:初始化時把函式交出去,事件發生時由控制項回呼;兩個類別之間的資料溝通,也常靠這條路徑完成。

定義監聽器的第一步永遠是宣告一個介面,具體行為則由設定監聽器的那一方實作。

關鍵程式碼

package com.example.listviewitem.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/** 自定義 View:對外提供三個監聽器 */
public class MyView extends View {

    private OnDownActionListener mDown = null;
    private OnMoveActionListener mMove = null;
    private OnUpActionListener mUp = null;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (mDown != null) {
                mDown.OnDown(x, y);
            }
            return true; // 傳回 true,後續的 move 與 up 才會送到這裡
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (mMove != null) {
                mMove.OnMove(x, y);
            }
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            if (mUp != null) {
                mUp.OnUp(x, y);
            }
        }
        return super.onTouchEvent(event);
    }

    public void setOnDownActionListener(OnDownActionListener down) {
        mDown = down;
    }

    public void setOnMoveActionListener(OnMoveActionListener move) {
        mMove = move;
    }

    public void setOnUpActionListener(OnUpActionListener up) {
        mUp = up;
    }

    public interface OnDownActionListener {
        void OnDown(int x, int y);
    }

    public interface OnMoveActionListener {
        void OnMove(int x, int y);
    }

    public interface OnUpActionListener {
        void OnUp(int x, int y);
    }
}

在版面檔中引用

<?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" >

    <com.example.listviewitem.widgets.MyView
        android:id="@+id/my_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/area_point_bg" />
</LinearLayout>

在 Activity 中設定監聽器

public class TestListener extends Activity {

    private MyView view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listener);
        view = (MyView) findViewById(R.id.my_view);

        view.setOnDownActionListener(new OnDownActionListener() {
            @Override
            public void OnDown(int x, int y) {
                System.out.println("down x = " + x + " y = " + y);
            }
        });

        view.setOnMoveActionListener(new OnMoveActionListener() {
            @Override
            public void OnMove(int x, int y) {
                System.out.println("move x = " + x + " y = " + y);
            }
        });

        view.setOnUpActionListener(new OnUpActionListener() {
            @Override
            public void OnUp(int x, int y) {
                System.out.println("up x = " + x + " y = " + y);
            }
        });
    }
}

執行後在控制台觀察輸出:

三組座標都按順序印了出來,代表自定義的監聽器已經正常運作。這裡唯一要留意的是 ACTION_DOWN 必須回傳 true,否則後續的 move 與 up 事件不會再送進來。

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