自定义监听接口
来源:互联网 发布:程序员文本编辑器 编辑:程序博客网 时间:2024/04/25 00:44
本文章主要实现 自定义view 进行自定义监听接口 实现点击,移动,抬起 是 触摸的坐标
1:首先自定义view
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class Myview extends View {
private OnDownActionListener down=null;
private OnMoveActionListener move=null;
private OnUpActionListener up=null;
public Myview(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public Myview(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public Myview(Context context) {
super(context);
// 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) {
int x,y;
if (event.getAction()==MotionEvent.ACTION_DOWN) {
x=(int) event.getX();
y=(int) event.getY();
if (down!=null) {
down.OnDown(x, y);
}
return true;
}else if (event.getAction()==MotionEvent.ACTION_MOVE) {
x=(int) event.getX();
y=(int) event.getY();
if (move!=null) {
move.OnMove(x, y);
}
}else if (event.getAction()==MotionEvent.ACTION_UP) {
x=(int) event.getX();
y=(int) event.getY();
if (up!=null) {
up.OnUp(x, y);
}
}
return super.onTouchEvent(event);
}
public OnDownActionListener getDown() {
return down;
}
public void setDown(OnDownActionListener down) {
this.down = down;
}
public OnMoveActionListener getMove() {
return move;
}
public void setMove(OnMoveActionListener move) {
this.move = move;
}
public OnUpActionListener getUp() {
return up;
}
public void setUp(OnUpActionListener up) {
this.up = up;
}
public interface OnDownActionListener{
public void OnDown(int x,int y);
}
public interface OnUpActionListener{
public void OnUp(int x,int y);
}
public interface OnMoveActionListener{
public void OnMove(int x,int y);
}
}
2:定义XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.example.demo7interf.Myview
android:id="@+id/my_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_action_search" >
</com.example.demo7interf.Myview>
</LinearLayout>
3:在activity中使用view以及监听
package com.example.demo7interf;
import com.example.demo7interf.Myview.OnDownActionListener;
import com.example.demo7interf.Myview.OnMoveActionListener;
import com.example.demo7interf.Myview.OnUpActionListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity {
private Myview myview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myview=(Myview) findViewById(R.id.my_view);
myview.setDown(new OnDownActionListener() {
@Override
public void OnDown(int x, int y) {
Toast.makeText(MainActivity.this, "你点击的是down事件:坐标为:x"+x+"y:"+y, 1).show();
}
});
myview.setMove(new OnMoveActionListener() {
@Override
public void OnMove(int x, int y) {
Toast.makeText(MainActivity.this, "你点击的是MOVE事件:坐标为:x"+x+"y:"+y, 1).show();
}
});
myview.setUp(new OnUpActionListener() {
@Override
public void OnUp(int x, int y) {
Toast.makeText(MainActivity.this, "你点击的是up事件:坐标为:x"+x+"y:"+y, 1).show();
}
});
}
}
ok这样就实现了
- 自定义监听接口
- Android 自定义Dialog,自定义接口监听事件。
- 利用接口实现自定义监听事件以及观察者模式
- 扩展 Rxbinding,打造自定义监听多控件接口
- 自定义监听
- 监听原理----自定义监听--示例
- 安卓中RecyclerView的简单使用和自定义RecyclerView的接口点击监听事件
- HttpSession相关监听接口
- OnKeyListener键盘监听接口
- 接口与监听机制
- java 自定义监听事件
- Ext自定义事件监听
- java 自定义监听事件
- 自定义java事件监听
- java 自定义监听事件
- spring自定义事件监听
- 自定义UILable 监听事件
- android中自定义监听
- centos 开放端口
- 第二套三
- 分享组件
- Unity3D 4.0新功能:Mecanim动画系统基础教程
- java基本数据结构
- 自定义监听接口
- android px、sp、dp之间的互转
- Android Service完全解析,关于服务你所需知道的一切(上)
- Jquery.autocomplete详解
- ScrollView的属性(纵向的用HorizontalScrollView)
- 在zend编辑器中快捷键的使用
- Android Service完全解析,关于服务你所需知道的一切(下)
- 开学第一课——从“心”开始 心理健康教育推广公益活动
- SQL执行效率,执行计划