Protocol¼òµ¥À´Ëµ¾ÍÊÇһϵÁз½·¨µÄÁÐ±í£¬ÆäÖÐÉùÃ÷µÄ·½·¨¿ÉÒÔ±»ÈκÎÀàʵÏÖ¡£ÕâÖÐģʽһ°ã³ÆΪ´úÀí£¨delegation)ģʽ¡£
ÔÚIOSºÍOS X¿ª·¢ÖУ¬Apple²ÉÓÃÁË´óÁ¿µÄ´úÀíģʽÀ´ÊµÏÖMVCÖÐView(UI¿Ø¼þ£©ºÍController(¿ØÖÆÆ÷£©µÄ½âñî¡£
ÏÂÃæÎÒÃÇÏÈÀ´¿´Ò»ÏÂÎÒÃÇÊìϤµÄAndroidÖеİ´Å¥¼àÌý¹ý³Ì£¬È»ºóÔÙ¶Ô±ÈÀí½âdelegation¡£
Ê×ÏÈÎÒ½¨Á¢Ò»¸öºÜ¼òµ¥µÄAndroid¹¤³Ì£¬ÔÚLayoutÖзÅÖÃÒ»¸ö°´Å¥£¬ÈçÏ£º
package com.example.helloword; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnLongClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.mybutton); button.setOnClickListener(new MyOnClickListener()); } class MyOnClickListener implements OnClickListener{ @Override public void onClick(View arg0) { Toast.makeText(MainActivity.this, "µã»÷ÁË°´Å¥", Toast.LENGTH_SHORT).show(); } } class MyonLongClickListener implements OnLongClickListener{ @Override public boolean onLongClick(View arg0) { Toast.makeText(MainActivity.this, "³¤°´ÁË°´Å¥", Toast.LENGTH_SHORT).show(); return false; } } }OnClickListenerÊÇViewµÄÒ»¸öÄÚ²¿À࣬ÊÇView¶¨ÒåµÄÒ»¸ö½Ó¿Ú£¬ÎÒÃÇ´ò¿ªOnClickListenerÔ´ÂëÈçÏ£º
/** * Interface definition for a callback to be invoked when a view is clicked. */ public interface OnClickListener { /** * Called when a view has been clicked. * * @param v The view that was clicked. */ void onClick(View v); }ÎÒÃÇÔÙÀ´¿´¿´setOnClickListener·½·¨
public void setOnClickListener(OnClickListener l) { if (!isClickable()) { setClickable(true); } getListenerInfo().mOnClickListener = l; }ÏÈÅжÏViewÊDz»Êǿɵã»÷µÄ£¬Ö÷ÒªÎÒÃÇÀ´¿´¿´ÏÂÃæÄÇÒ»¾ä£¬getListenerInfo().mOnClickListener = 1;
ListenerInfo getListenerInfo() { if (mListenerInfo != null) { return mListenerInfo; } mListenerInfo = new ListenerInfo(); return mListenerInfo; }´ÓÕâ¶Î´úÂë¿ÉÒÔ¿´³öÀ´£¬½«ÎÒÃǵÄOnClickListenerʵÀý±£´æµ½ÁËListenerInfor¶ÔÏóÖÐÁË£¬ÄÇôListenerInfor¶ÔÏóÊÇÓÃÀ´¸ÉÂïµÄÄØ£¿ÓÉÓÚÎÒµ±ÏÂûÓÐAndroidϵͳԴÂë¾Í²»ÔÚ¸ú×ÙÏÂÈ¥ÁË£¬¿ÉÒÔ²ÂÏëÕâ¸öÀà³ÖÓÐÎÒÃǵÄOnClickeListener¶ÔÏ󣬵±ÏµÍ³ÏìÓ¦ÆÁÄ»µã»÷ʼþµÄʱºò£¬Í¨¹ýʼþ·Ö·¢£¬¿ÉÒÔµ÷ÓÃonClick·½·¨À´¸æËßËùÓÐʵÏÖÁËOnClickeListener½Ó¿ÚµÄ¶ÔÏó¡£
½ÓÏÂÀ´ÎÒÃÇÀ´Ä£ÄâÒ»ÏÂIOSÖа´Å¥¼àÌýµÄʵÏÖ¡£
Button.hÎļþ
<†·Ÿ"http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD48cHJlIGNsYXNzPQ=="brush:java;">#import 2014-11-16 13:52:35.215 ProtocalTest[845:82273] °´Å¥±»µã»÷ÁË 2014-11-16 13:52:35.216 ProtocalTest[845:82273] °´Å¥±»³¤°´ÁË#import "Button.h"
@implementation Button
- (void)click {
//°´Å¥±»µã»÷ÁË£¬¾ÍÓ¦¸Ã֪ͨ¼àÌýÆ÷£¨ÕâÀïÊÇÄ£Ä⣩
//Èç¹ûonClick·½·¨±»ÊµÏÖ£¬µ÷ÓÃonClick·½·¨
if([_delegate respondsToSelector:@selector(onClick:)]){
[_delegate onClick:self];
}else{
NSLog(@"onClick¼àÌýÆ÷δʵÏÖ");
}
}
- (void)longClick {
//°´Å¥±»³¤°´£¨Ä£Äâϵͳ£©
if([_delegate respondsToSelector:@selector(onClick:)]){
[_longClickDeleate onLongClick:self];
}else{
NSLog(@"longClick¼àÌýÆ÷δʵÏÖ");
}
}
- (void)dealloc {
[_delegate release];
[_longClickDeleate release];
[super dealloc];
}
@end
ButtonListener.h#import
ButtonListener.m#import "ButtonListener.h"
#import "Button.h"
@implementation ButtonListener
- (void)onClick:(Button *)btn {
NSLog(@"°´Å¥±»µã»÷ÁË");
}
@end
ButtonLongClickListener.hÎļþ#import
ButtonLongClickListener.mÎļþ#import "ButtonLongClickListener.h"
#import "Button.h"
@implementation ButtonLongClickListener
- (void)onLongClick:(Button *)btn{
NSLog(@"°´Å¥±»³¤°´ÁË");
}
@end
main.mÎļþ#import
Êä³ö½á¹û£º