国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程自定義View時添加自己的監聽器示例

2019-10-22 18:16:50
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程自定義View時添加自己的監聽器。分享給大家供大家參考,具體如下:

監聽器在Java中非常常用,在自定義控件時可能根據自己的需要去監聽一些數據的改變,這時就需要我們自己去寫監聽器,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);    // TODO Auto-generated constructor stub  }  @Override  protected void onDraw(Canvas canvas) {    // TODO Auto-generated method stub    super.onDraw(canvas);  }  @Override  public boolean onTouchEvent(MotionEvent event) {    // TODO Auto-generated method stub    int x, y;    if (event.getAction() == MotionEvent.ACTION_DOWN) {      x = (int) event.getX();      y = (int) event.getY();      if (mDown != null) {        mDown.OnDown(x, y);      }      return true; // 只有返回true這個控件的move和up才會響應    } else if (event.getAction() == MotionEvent.ACTION_MOVE) {      x = (int) event.getX();      y = (int) event.getY();      if (mMove != null) {        mMove.OnMove(x, y);      }    } else if (event.getAction() == MotionEvent.ACTION_UP) {      x = (int) event.getX();      y = (int) event.getY();      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 {    public void OnDown(int x, int y);  }  public interface OnMoveActionListener {    public void OnMove(int x, int y);  }  public interface OnUpActionListener {    public void OnUp(int x, int y);  }}

自定義View在xml中的定義

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://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中設置監聽器

package com.example.listviewitem;import com.example.listviewitem.widgets.MyView;import com.example.listviewitem.widgets.MyView.OnDownActionListener;import com.example.listviewitem.widgets.MyView.OnMoveActionListener;import com.example.listviewitem.widgets.MyView.OnUpActionListener;import android.app.Activity;import android.os.Bundle;public class TestListener extends Activity {  private MyView view;  @Override  protected void onCreate(Bundle savedInstanceState) {    // TODO Auto-generated method stub    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) {        // TODO Auto-generated method stub        System.out.println("down x = " + x + " y = " + y);      }    });    view.setOnMoveActionListener(new OnMoveActionListener() {      @Override      public void OnMove(int x, int y) {        // TODO Auto-generated method stub        System.out.println("move x = " + x + " y = " + y);      }    });    view.setOnUpActionListener(new OnUpActionListener() {      @Override      public void OnUp(int x, int y) {        // TODO Auto-generated method stub        System.out.println("up x = " + x + " y = " + y);      }    });  }}

打印消息

Android,自定義View,監聽器

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

希望本文所述對大家Android程序設計有所幫助。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 尉犁县| 吉木乃县| 曲沃县| 石渠县| 财经| 华亭县| 榕江县| 平邑县| 绥德县| 邵武市| 泸西县| 武夷山市| 虞城县| 石屏县| 错那县| 聂拉木县| 壶关县| 忻州市| 玛多县| 安平县| 綦江县| 民勤县| 玉门市| 龙州县| 雅安市| 即墨市| 彰化市| 本溪市| 双桥区| 忻州市| 开化县| 蕉岭县| 阳高县| 南靖县| 抚松县| 项城市| 呼图壁县| 长沙市| 汤原县| 荆门市| 广东省|