Android 回调机制
来源:互联网 发布:淘宝网创立时间 编辑:程序博客网 时间:2024/06/06 18:48
package com.tz.onclick;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Toast;import com.tz.onclick.listener.OnTextViewClickListener;public class MainActivity extends Activity implements OnTextViewClickListener {private MyTextView tv;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tv = (MyTextView) findViewById(R.id.tv);tv.setOnTextViewClickListener(this);}// 定会回调函数public void onTextViewClick(View v) {// TODO Auto-generated method stubToast.makeText(this, "ontextViewClick", 1000).show();}// //定义回调函数// public void onClick(View v) {// // TODO Auto-generated method stub// Toast.makeText(this, "onClick", 1000).show();// }}
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.tz.onclick.MyTextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="fill_parent" > </com.tz.onclick.MyTextView></LinearLayout>
package com.tz.onclick;import com.tz.onclick.listener.OnTextViewClickListener;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;public class MyTextView extends View {private float touchX, touchY;private OnTextViewClickListener onTextViewClickListener;public OnTextViewClickListener getOnTextViewClickListener() {return onTextViewClickListener;}public void setOnTextViewClickListener(OnTextViewClickListener onTextViewClickListener) {this.onTextViewClickListener = onTextViewClickListener;}public MyTextView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}// 绘制控件视图@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubPaint paint = new Paint();paint.setTextSize(28);paint.setColor(Color.RED);canvas.drawText("这是我们的控件", 200, 200, paint);super.onDraw(canvas);}@Overridepublic boolean onTouchEvent(MotionEvent event) {// TODO Auto-generated method stubint action = event.getAction();switch (action) {case MotionEvent.ACTION_DOWN:touchX = event.getX();touchY = event.getY();break;case MotionEvent.ACTION_UP:float x = event.getX();float y = event.getY();Log.i("INFO", "DownX:" + touchX + "----upX:" + x);if (touchX == x && touchY == y) {// 产生了onclick事件onTextViewClickListener.onTextViewClick(this);}break;default:break;}return true;}}
package com.tz.onclick.listener;import android.view.View;public interface OnTextViewClickListener {void onTextViewClick(View v);}
0 0
- android回调机制
- Android回调机制
- Android回调机制
- android 回调机制
- Android回调机制
- Android回调机制
- Android回调机制
- Android回调机制
- Android回调机制
- Android回调机制
- Android回调机制
- Android 回调机制
- Android回调机制
- android 回调机制
- Android回调机制
- Android回调机制
- Android回调机制
- Android回调机制
- BootStrap入门教程 (四)
- 决策树
- 一步一步在Windows下用MinGW编译x264(x264-snapshot-20160119-2245)
- 数据结构实现之队列
- Codeforce 445E
- Android 回调机制
- jq与dom对象相互转换
- Django传递数据给JS
- Spring4.0学习笔记008——AOP的配置(基于注解)
- 【费用流模型】BZOJ2668 UVA1317 UVA1486 UVA1104
- 关于数组与指针的一个小问题
- iOS面试常见问题总结
- 【C语言】编写函数判断当前的机器大小端模式
- Codeforce 445(A,B,C,D,E)