Android編程自定義View時(shí)添加自己的監(jiān)聽器示例
本文實(shí)例講述了Android編程自定義View時(shí)添加自己的監(jiān)聽器。分享給大家供大家參考,具體如下:
監(jiān)聽器在Java中非常常用,在自定義控件時(shí)可能根據(jù)自己的需要去監(jiān)聽一些數(shù)據(jù)的改變,這時(shí)就需要我們自己去寫監(jiān)聽器,Java中的監(jiān)聽器實(shí)際上就是C++中的回調(diào)函數(shù),在初始化時(shí)設(shè)置了這個(gè)函數(shù),由某個(gè)事件觸發(fā)這個(gè)函數(shù)被調(diào)用,兩個(gè)類之間的數(shù)據(jù)通信也可以通過監(jiān)聽器來實(shí)現(xiàn)。要定義監(jiān)聽器就要先定義一個(gè)接口,具體功能由設(shè)置監(jiān)聽器的類去實(shí)現(xiàn)
關(guān)鍵代碼實(shí)現(xiàn)
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中定義三個(gè)監(jiān)聽器 */ 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這個(gè)控件的move和up才會(huì)響應(yīng) } 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); } // 為每個(gè)接口設(shè)置監(jiān)聽器 public void setOnDownActionListener(OnDownActionListener down) { mDown = down; } public void setOnMoveActionListener(OnMoveActionListener move) { mMove = move; } public void setOnUpActionListener(OnUpActionListener up) { mUp = up; } // 定義三個(gè)接口 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中設(shè)置監(jiān)聽器
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); } }); } }
打印消息
說明我們自定義的監(jiān)聽器已經(jīng)起作用了。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android控件用法總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》及《Android資源操作技巧匯總》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)檢測(cè)手機(jī)搖晃的監(jiān)聽器
- Android中ScrollView實(shí)現(xiàn)滑動(dòng)距離監(jiān)聽器的方法
- Android編程之監(jiān)聽器用法實(shí)例分析
- Android編程之監(jiān)聽器的實(shí)現(xiàn)方法
- Android實(shí)現(xiàn)靜態(tài)廣播監(jiān)聽器的方法
- android Animation監(jiān)聽器AnimationListener的使用方法)
- Android控件系列之Button以及Android監(jiān)聽器使用介紹
- android監(jiān)聽器實(shí)例代碼
相關(guān)文章
Android使用xml文件資源定義菜單實(shí)現(xiàn)方法示例
這篇文章主要介紹了Android使用xml文件資源定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android資源文件管理及xml配置自定義菜單相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android自定義View制作動(dòng)態(tài)炫酷按鈕實(shí)例解析
這篇文章主要為大家詳細(xì)解析了Android自定義View制作動(dòng)態(tài)炫酷按鈕實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android開發(fā)之Activity管理工具類完整示例
這篇文章主要介紹了Android開發(fā)之Activity管理工具類,集合完整實(shí)例形式分析了Android操作Activity創(chuàng)建、添加、獲取、移除等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01非常實(shí)用的小功能 Android應(yīng)用版本的更新實(shí)例
這篇文章主要為大家詳細(xì)介紹了一個(gè)非常實(shí)用的小功能,Android應(yīng)用版本的更新實(shí)例,感興趣的小伙伴們可以參考一下2016-08-08Eclipse新建Android項(xiàng)目報(bào)錯(cuò)解決方案詳細(xì)匯總
這篇文章主要介紹了Eclipse新建Android項(xiàng)目報(bào)錯(cuò)解決方案詳細(xì)匯總,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Android Bitmap詳解及Bitmap的內(nèi)存優(yōu)化
這篇文章主要介紹了Android Bitmap詳解及Bitmap的內(nèi)存優(yōu)化的相關(guān)資料,Bitmap是Android系統(tǒng)中的圖像處理的最重要類之一。用它可以獲取圖像文件信息,進(jìn)行圖像剪切、旋轉(zhuǎn)、縮放等操作,并可以指定格式保存圖像文件,需要的朋友可以參考下2017-03-03Android開發(fā)之ListView、GridView 詳解及示例代碼
本文主要介紹Android開發(fā)之ListView、GridView,這里整理了相關(guān)資料及簡(jiǎn)單示例代碼,幫助大家學(xué)習(xí)參考,有需要的小伙伴可以參考下2016-08-08