Android自定義View時添加自己的監聽器

監聽器在Java中非常常用,在自定義控件時可能根據自己的需要去監聽一些數據的改變,這時就需要我們自己去寫監聽器,Java中的監聽器實際上就是C++中的回調函數,在初始化時設置了這個函數,由某個事件觸發這個函數被調用,兩個類之間的數據通信也可以通過監聽器來實現。要定義監聽器就要先定義一個接口,具體功能由設置監聽器的類去實現

關鍵代碼實現

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package com.example.listviewitem.widgets;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8.   
  9. /** 
  10.  * 在自定義的View中定義三個監聽器 
  11.  */  
  12. public class MyView extends View {  
  13.   
  14.     private OnDownActionListener mDown = null;  
  15.     private OnMoveActionListener mMove = null;  
  16.     private OnUpActionListener mUp = null;  
  17.   
  18.     public MyView(Context context) {  
  19.         super(context);  
  20.     }  
  21.   
  22.     public MyView(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.         // TODO Auto-generated constructor stub  
  25.     }  
  26.   
  27.     @Override  
  28.     protected void onDraw(Canvas canvas) {  
  29.         // TODO Auto-generated method stub  
  30.         super.onDraw(canvas);  
  31.     }  
  32.   
  33.     @Override  
  34.     public boolean onTouchEvent(MotionEvent event) {  
  35.         // TODO Auto-generated method stub  
  36.         int x, y;  
  37.         if (event.getAction() == MotionEvent.ACTION_DOWN) {  
  38.             x = (int) event.getX();  
  39.             y = (int) event.getY();  
  40.             if (mDown != null) {  
  41.                 mDown.OnDown(x, y);  
  42.             }  
  43.             return true// 只有返回true這個控件的move和up纔會響應  
  44.         } else if (event.getAction() == MotionEvent.ACTION_MOVE) {  
  45.             x = (int) event.getX();  
  46.             y = (int) event.getY();  
  47.             if (mMove != null) {  
  48.                 mMove.OnMove(x, y);  
  49.             }  
  50.         } else if (event.getAction() == MotionEvent.ACTION_UP) {  
  51.             x = (int) event.getX();  
  52.             y = (int) event.getY();  
  53.             if (mUp != null) {  
  54.                 mUp.OnUp(x, y);  
  55.             }  
  56.         }  
  57.         return super.onTouchEvent(event);  
  58.     }  
  59.   
  60.     // 爲每個接口設置監聽器  
  61.     public void setOnDownActionListener(OnDownActionListener down) {  
  62.         mDown = down;  
  63.     }  
  64.   
  65.     public void setOnMoveActionListener(OnMoveActionListener move) {  
  66.         mMove = move;  
  67.     }  
  68.   
  69.     public void setOnUpActionListener(OnUpActionListener up) {  
  70.         mUp = up;  
  71.     }  
  72.   
  73.     // 定義三個接口  
  74.     public interface OnDownActionListener {  
  75.         public void OnDown(int x, int y);  
  76.     }  
  77.   
  78.     public interface OnMoveActionListener {  
  79.         public void OnMove(int x, int y);  
  80.     }  
  81.   
  82.     public interface OnUpActionListener {  
  83.         public void OnUp(int x, int y);  
  84.     }  
  85. }  
自定義View在xml中的定義

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <com.example.listviewitem.widgets.MyView  
  8.         android:id="@+id/my_view"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:background="@drawable/area_point_bg" />  
  12.   
  13. </LinearLayout>  
Activity中設置監聽器

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. package com.example.listviewitem;  
  2.   
  3. import com.example.listviewitem.widgets.MyView;  
  4. import com.example.listviewitem.widgets.MyView.OnDownActionListener;  
  5. import com.example.listviewitem.widgets.MyView.OnMoveActionListener;  
  6. import com.example.listviewitem.widgets.MyView.OnUpActionListener;  
  7.   
  8. import android.app.Activity;  
  9. import android.os.Bundle;  
  10.   
  11. public class TestListener extends Activity {  
  12.   
  13.     private MyView view;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.listener);  
  20.         view = (MyView) findViewById(R.id.my_view);  
  21.         view.setOnDownActionListener(new OnDownActionListener() {  
  22.   
  23.             @Override  
  24.             public void OnDown(int x, int y) {  
  25.                 // TODO Auto-generated method stub  
  26.                 System.out.println("down x = " + x + " y = " + y);  
  27.             }  
  28.         });  
  29.   
  30.         view.setOnMoveActionListener(new OnMoveActionListener() {  
  31.   
  32.             @Override  
  33.             public void OnMove(int x, int y) {  
  34.                 // TODO Auto-generated method stub  
  35.                 System.out.println("move x = " + x + " y = " + y);  
  36.             }  
  37.         });  
  38.   
  39.         view.setOnUpActionListener(new OnUpActionListener() {  
  40.   
  41.             @Override  
  42.             public void OnUp(int x, int y) {  
  43.                 // TODO Auto-generated method stub  
  44.                 System.out.println("up x = " + x + " y = " + y);  
  45.             }  
  46.         });  
  47.     }  
  48. }  
打印消息

說明我們自定義的監聽器已經起作用了。


原帖地址:

http://blog.csdn.net/deng0zhaotai/article/details/21456791?utm_source=tuicool


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章