大佬教程收集整理的这篇文章主要介绍了Android编程自定义View时添加自己的监听器示例,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
本文实例讲述了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 @R_262_8487@an 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://scheR_525_11845@as.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); } }); } }
打印消息
@H_674_27@
说明我们自定义的监听器已经起作用了。
更多关于AndroID相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android开发入门与进阶教程》、《Android视图View技巧总结》、《Android编程之activity操作技巧总结》、《Android数据库操作技巧总结》及《Android资源操作技巧汇总》
希望本文所述对大家AndroID程序设计有所帮助。
以上是大佬教程为你收集整理的Android编程自定义View时添加自己的监听器示例全部内容,希望文章能够帮你解决Android编程自定义View时添加自己的监听器示例所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。