Android编程自定义View时添加自己的 * 示例
作者:dztai 发布时间:2023-08-20 11:58:27
标签: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 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程序设计有所帮助。
来源:http://blog.csdn.net/deng0zhaotai/article/details/21456791


猜你喜欢
- 一、问题描述有时候,我们会遇到在遍历List集合的过程中删除数据的情况。看着自己写的代码,感觉完全没有问题,但就是达不到预期的效果,这是为什
- 在日常工作中,我们有时会需要修改字体的颜色来突出文本重点,让读者更容易抓住文章要点。在今天这篇文章中,我将为大家介绍如何以编程方式,在Wor
- 关于UIToolbarToolBar工具栏是视图View的属性,可以在工具栏上添加工具栏按钮Bar Button Item(可以是自定义的C
- 近期项目中需要使用到一种类似手机电池充电进度的动画效果,以前没学属性动画的时候,是用图片+定时器的方式来完成的,最近一直在学习动画这一块,再
- 现在很多app的支付、输入密码功能,都已经开始使用自定义数字键盘,不仅更加方便、其效果着实精致。下面带着大家学习下,如何 * 微信的数字键盘,
- 本文实例为大家分享了DrawerLayout和触摸事件分发实现抽屉侧滑效果的具体代码,供大家参考,具体内容如下效果展示 还是看代码实在,直接
- 最近一门课要求编写一个上位机串口通信工具,我基于Java编写了一个带有图形界面的简单串口通信工具,下面详述一下过程,供大家参考 ^_^一:首
- 微信朋友圈上面的图片封面,QQ空间说说上面的图片封面都有下拉反弹的效果,这些都是使用滚动条实现的。下拉,当松开时候,反弹至原来的位置。下拉时
- 使用通配符增强泛型1.题目泛型是JAVA重要的特性,使用泛型编程,可以使代码复用率提高。实现:在泛型方法中使用通配符2.解题思路创建一个类:
- Spring-boot JMS 发送消息慢的问题解决1、在《ActiveMQ 基于zookeeper的主从(levelDB Master/S
- 文章来源:互联网 作者:skywoo/CSDNWindows2000+Apache2.0.48+resin2.1.6 &nbs
- 本文为大家分享了C#导入导出Excel数据的具体代码,供大家参考,具体内容如 * :对于实体类对象最好新建一个并且继承原有实体类,这样可以将类
- 1. 需要事先将jar包 放在kettle 的 libext 目录,kettle 在启动时会自动加载libext 目录下的所有 jar 包。
- Activity非异常情况下的生命周期是指,用户正常参与UI交互的情况下,Activity所经过的生命周期的改变;一般情况下,Activit
- 一、前言前一篇文章已经详细介绍了如何使用Xposed框架编写第一个微信插件:摇骰子和猜拳 * 本文继续来介绍如何使用Xposed框架编写第
- 一、概述IDEA自带的注释模板一般都很简单,然而我们在写代码的时候喜欢把类注释和文档注释写在代码里,既方便自己看所有的参数,也便于以后维护代
- 本文实例讲述了Java截取字符串的方法。分享给大家供大家参考。具体实现方法如下:public static void main(String
- 今天在云和学院学了很多,我这次只能先总结一下C#中的虚方法和抽象的运用。理论:虚方法:用virtual修饰的方法叫
- 寻找到application.yml的读取的操作。从spring.factories 中查看到# Application Listeners
- 关于Android的自定义控件,之前也写了两个,一个是简单地继承View,另一个通过继承Layout实现一个省市联动控件。这篇,将通过继承V