寫(xiě)作原因:Android回調(diào)機(jī)制是Android開(kāi)發(fā)者繞不開(kāi)的一個(gè)點(diǎn)缕碎,對(duì)于它的掌握學(xué)習(xí)十分重要。本文由于作者水平有限可能存在紕漏,對(duì)于回調(diào)機(jī)制的學(xué)習(xí)掌握仍在途中熙揍,以后會(huì)推出更多的相關(guān)文章茬故。
簡(jiǎn)介?
接口回調(diào)是Android中一種常用的機(jī)制盖灸,如Button點(diǎn)擊事件就是接口回調(diào)的具體實(shí)現(xiàn)。Fragment與Activity之間的通信機(jī)制也是基于接口回調(diào)實(shí)現(xiàn)磺芭,感興趣的可以閱讀博主上篇關(guān)于Fragment通信的文章(基于Google官方文檔):(Fragment間的通信)赁炎。對(duì)于接口回調(diào),作為初學(xué)者钾腺,個(gè)人有一下的一點(diǎn)認(rèn)識(shí):我們可以把接口看作是一個(gè)呼叫端口徙垫,是在某個(gè)功能類(lèi)(發(fā)送方)完成自身功能過(guò)程中觸發(fā)某些動(dòng)作時(shí)通過(guò)接口中的某一個(gè)方法呼叫接口,然后接口將這種呼叫傳至實(shí)現(xiàn)該接口的類(lèi)(接收處理方)中放棒,該類(lèi)通過(guò)實(shí)現(xiàn)回調(diào)方法對(duì)該呼叫作出反饋姻报。下面是Wikipedia上面關(guān)于回調(diào)機(jī)制的定義,有興趣可以看看间螟。
In computer programming, a callback is a piece of executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at some convenient time. The invocation may be immediate as in a synchronous callback, or it might happen at a later time as in an asynchronous callback. In all cases, the intention is to specify a function or subroutine as an entity that is, depending on the language, more or less similar to a variable.
實(shí)現(xiàn)?
實(shí)現(xiàn)代碼是最好的學(xué)習(xí)方式吴旋,下面博主通過(guò)封裝一個(gè)簡(jiǎn)單的Dialog來(lái)講解接口回調(diào)的具體用法损肛。平常使用Dialog時(shí)如果按照標(biāo)準(zhǔn)寫(xiě)法來(lái)有時(shí)會(huì)顯得很繁瑣,博主因?yàn)樘囟ㄐ枰獙⑵浞庋b成一個(gè)只有message和“確定”荣瑟,“取消”按鈕點(diǎn)擊事件的工具荧关。下面我將按照步驟一步步實(shí)現(xiàn):
?定義接口?
在簡(jiǎn)介中已經(jīng)說(shuō)過(guò),接口是一個(gè)呼叫端口褂傀,是通信的中介忍啤。內(nèi)部的方法類(lèi)比為不同請(qǐng)求。的本例中的接口是在我們點(diǎn)擊Positive Button和Negative Button時(shí)被呼叫的仙辟。所以定義一下的接口:
```
public interface onButtonClick{
public void OnPositive();
public void OnNegative();
}
```
上面的接口中有兩個(gè)方法同波,分別在點(diǎn)擊Positive Button和Negative Button時(shí)回調(diào)
實(shí)現(xiàn)接口
這包括兩個(gè)部分,一個(gè)是在功能類(lèi)(發(fā)送方)向接口發(fā)出呼叫時(shí)實(shí)現(xiàn)回調(diào):
先創(chuàng)建回調(diào)對(duì)象mCallBack:
```
private onButtonClick mButtonClickCallBack;
```
然后在觸發(fā)某個(gè)動(dòng)作時(shí)(當(dāng)點(diǎn)擊Dialog上的按鈕時(shí))發(fā)出回調(diào)(呼叫接口):
```
builderSec.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButtonClickCallBack.OnNegative();
}
});
在另一個(gè)類(lèi)(接收處理方)中實(shí)現(xiàn)回調(diào)方法對(duì)該呼叫作出反饋:
先實(shí)現(xiàn)該接口叠国,可以在功能類(lèi)中定義以下方法:
public void setOnButtonClick(onButtonClick mNegativeCallBack) {
this.mButtonClickCallBack = mNegativeCallBack;
}
MyDialog deleteDialog = new MyDialog(getActivity(),"RTGBrowser","是否刪除當(dāng)前歷史記錄未檩?");
deleteDialog.setOnButtonClick(new MyDialog.onButtonClick() {
@Override
public void OnPositive() {
//TODO:在這里實(shí)現(xiàn)點(diǎn)擊Positive按鈕的邏輯;
}
@Override
public void OnNegative() {
//TODO:在這里實(shí)現(xiàn)點(diǎn)擊Negative按鈕的邏輯粟焊;
}
});
這樣就通過(guò)接口回調(diào)實(shí)現(xiàn)對(duì)一個(gè)Dialog進(jìn)行封裝冤狡,下面貼出完整的代碼:
?源碼?
MyDialog.class
package com.example.vincebarry.exam.function;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
public class MyDialog {
private onButtonClick mButtonClickCallBack;
private Context context;
private String title;
private String message;
AlertDialog.Builder builderSec;
public interface onButtonClick{
public void OnPositive();
public void OnNegative();
}
public MyDialog(Context context,String title,String message){
this.context = context;
this.title = title;
this.message = message;
builderSec = new AlertDialog.Builder(context);
builderSec.setTitle(title);
builderSec.setMessage(message);
builderSec.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButtonClickCallBack.OnPositive();
}
});
builderSec.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mButtonClickCallBack.OnNegative();
}
});
AlertDialog dialogSec = builderSec.show();
}
public void setOnButtonClick(onButtonClick mNegativeCallBack) {
this.mButtonClickCallBack = mNegativeCallBack;
}
}
MainActivity.class
MyDialog deleteDialog = new MyDialog(getActivity(),"RTGBrowser","是否刪除當(dāng)前歷史記錄?");
deleteDialog.setOnButtonClick(new MyDialog.onButtonClick() {
@Override
public void OnPositive() {
DataSupport.delete(TableHistoryLocal.class,positionInDb);
queryAndTrans();
roadData();
}
@Override
public void OnNegative() {
//donothing.
}
});
水平不足项棠,存在紕漏或錯(cuò)誤請(qǐng)指教1ā!