1.模拟java View类:
package com.test; /** * Created by dandan on 15-2-7. */ public class View { /**设置默认ID**/ public static final int NO_ID = -1; /**设置每个控件的ID用于被子类覆盖**/ public int mID = NO_ID; /**保存点击回调函数**/ public OnClicklistener mOnClicklistener; public interface OnClicklistener { void onClick(View view); }; void setOnClicklistener(OnClicklistener onClicklistener) { this.mOnClicklistener = onClicklistener; }; public int getId(){ return mID; }; }<span style="color:#ff0000;"> </span>
2.模拟android控件继承View
package com.test; /** * Created by dandan on 15-2-7. */ public class AnimationButton extends View{ /** * 按钮子类中模拟用户点击的方法 * * **/ public void moniClick(){ this.mID = 20; OnClicklistener clicklistener = this.mOnClicklistener; clicklistener.onClick(this); } }
package com.test; /** * Created by dandan on 15-2-7. */ public class MainActivity { public static void main(String[] args) { AnimationButton animationButton = new AnimationButton(); animationButton.setOnClicklistener(new View.OnClicklistener() { @Override public void onClick(View view) { System.out.print(view.getId()); } });
<span style="white-space:pre"> </span>//模拟用户点击按钮 animationButton.moniClick(); } }
android setOnClickListener 回调函数理解
原文地址:http://blog.csdn.net/taolong1/article/details/43602299