// Create an anonymous implementation of OnClickListener privateOnClickListenermCorkyListener=newOnClickListener(){publicvoidonClick(Viewv){// do something when the button is clicked }};protectedvoidonCreate(BundlesavedValues){...// Capture our button from layout Buttonbutton=(Button)findViewById(R.id.corky);// Register the onClick listener with the implementation above button.setOnClickListener(mCorkyListener);...}
采用嵌套接口实现:
123456789101112
publicclassExampleActivityextendsActivityimplementsOnClickListener{protectedvoidonCreate(BundlesavedValues){...Buttonbutton=(Button)findViewById(R.id.corky);button.setOnClickListener(this);}// Implement the OnClickListener callback publicvoidonClick(Viewv){// do something when the button is clicked }...}
//WorkingMessage.java privatefinalMessageStatusListenermStatusListener;publicinterfaceMessageStatusListener{/** * Called when the protocol for sending the message changes from SMS * to MMS, and vice versa. * * @param mms If true, it changed to MMS. If false, to SMS. */voidonProtocolChanged(booleanmms);/** * Called when an attachment on the message has changed. */voidonAttachmentChanged();/** * Called just before the process of sending a message. */voidonPreMessageSent();/** * Called once the process of sending a message, triggered by * {@link send} has completed. This doesn't mean the send succeeded, * just that it has been dispatched to the network. */voidonMessageSent();/** * Called if there are too many unsent messages in the queue and we're not allowing * any more Mms's to be sent. */voidonMaxPendingMessagesReached();/** * Called if there's an attachment error while resizing the images just before sending. */voidonAttachmentError(interror);}privateWorkingMessage(ComposeMessageActivityactivity){mActivity=activity;mContentResolver=mActivity.getContentResolver();mStatusListener=activity;//设置 mAttachmentType=TEXT;mText="";}publicstaticWorkingMessagecreateEmpty(ComposeMessageActivityactivity){// Make a new empty working message. WorkingMessagemsg=newWorkingMessage(activity);returnmsg;}privatevoidsendSmsWorker(StringmsgText,StringsemiSepRecipients,longthreadId){String[]dests=TextUtils.split(semiSepRecipients,";");if(LogTag.VERBOSE||Log.isLoggable(LogTag.TRANSACTION,Log.VERBOSE)){LogTag.debug("sendSmsWorker sending message: recipients="+semiSepRecipients+", threadId="+threadId);}MessageSendersender=newSmsMessageSender(mActivity,dests,msgText,threadId);try{sender.sendMessage(threadId);// Make sure this thread isn't over the limits in message count Recycler.getSmsRecycler().deleteOldMessagesByThreadId(mActivity,threadId);}catch(Exceptione){Log.e(TAG,"Failed to send SMS message, threadId="+threadId,e);}mStatusListener.onMessageSent();//调用 }//ComposeMessageActivity.java publicclassComposeMessageActivityextendsActivityimplementsView.OnClickListener,TextView.OnEditorActionListener,MessageStatusListener/*实现该接口*/,Contact.UpdateListener{....publicvoidonProtocolChanged(finalbooleanmms){//实现onProtocolChanged接口 // Have to make sure we're on the UI thread. This function can be called off of the UI // thread when we're adding multi-attachments runOnUiThread(newRunnable(){publicvoidrun(){toastConvertInfo(mms);setSendButtonText(mms);}});}....publicvoidinitialize(BundlesavedInstanceState,longoriginalThreadId){Intentintent=getIntent();// Create a new empty working message. mWorkingMessage=WorkingMessage.createEmpty(this);// }