1,View是所有控件的父类
2,View可以关联很多个listenner。就像一个女孩有很多个男人对她好一样。有为她卖苦力的穷叼丝,也有她拿得出手的高副帅。当女孩发生需要搬宿舍的事件的时候,这个时候穷叼丝监听到了该事件的发生,于是帮搬宿舍,当她需要去看电影和开房的时候,高副帅男友监听到了该事件,于是就跟她完成了一个很美好的夜晚。
View 的事件监听,举个例子
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);
}
}
然后在layout的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">
</com.example.listviewitem.widgets.myview></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、Canvas、bitmap
Android开发学习笔记(整理),整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、...
android 菜单栏 仿微信 自定义View初阶学习 记录下 方便以后再次使用
Android开发者学习笔记-View、Canvas、bitmap.pdf
来自http://blog.csdn.net/meiyoumimang的笔记代码
根据鸿洋大神博客所写。自己的学习笔记。自定义View实现验证码。
Android学习笔记 > 十年生死两茫茫,不思量,自难忘,华年短暂,陈辞岁月悠悠伤, > 满腔热血已芜荒,展未来,后生强,战战兢兢,如履薄冰心彷徨, > 青丝化雪、鬓角成霜,已是英雄迟暮,人生怎慷慨激昂? 目录 ...
目录,整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、apdapter等几乎全部内容...
Android Training学习笔记——Navigation 参考源码,文章地址:http://blog.csdn.net/xroocky/article/details/50767844
整理论坛android学习笔记,较完整的讲解了android的内容。包括:view、activity、service、intent、广播机制、http连接、数据sqllite存储、后台线程、各种layout、偏好、本地文件操作、apdapter等几乎全部内容。非常...
总结了android viewpager 学习过程中的经验
黑马程序员之android学习笔记--用户界面 View(一).docx
如果不是从学习Android SDK中提供的那些样例代码开始,可能没有更好的方法来掌握在Android这个框架上开发。由Android的核心开发团队提供了15个优秀的示例项目,包含了游戏、图像处理、时间显示、开始菜单快捷方式...
所以activity类创建了一个窗口,开发人员可以通过setContentView(View)接口把UI放到activity创建的窗口上,当activity指向全屏窗口时,也可以用其他方式实现:作为漂浮窗口(通过windowIsFloating的主题集合),或者...
主要为大家详细介绍了Android手势密码view的学习笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
如果不是从学习Android SDK中提供的那些样例代码开始,可能没有更好的方法来掌握在Android这个框架上开发。由Android的核心开发团队提供了15个优秀的示例项目,包含了游戏、图像处理、时间显示、开始菜单快捷方式...
我们还是接着我们上一篇博客中的内容往下讲哈,上一节 Android手势密码view笔记(一)我们已经实现了我们的IndicatorView指示器view了: 下面我们来实现下我们的手势密码view: 实现思路: 1、我们照样需要拿到...