Android - 类似于iPhone SDK委托回调的东西?

Sid*_*Sid 24 android delegates callback

我刚刚从iPhone切换到Android,我正在寻找类似于iPhone SDK的地方,当一个类完成某个任务时,它会调用设置为委托的对象中的委托方法.

我不需要太多细节.我浏览了文档但没有找到任何内容(我得到的最接近的是"广播意图",看起来更像是iOS通知).

即使有人能指出我正确的文档,它也会很棒.

谢谢!

Sid*_*Sid 16

没关系......在这里找到答案:)

http://www.javaworld.com/javaworld/javatips/jw-javatip10.html


从文章中粘贴以保存它:

熟悉MS-Windows和X Window系统的事件驱动编程模型的开发人员习惯于在发生某些事件时传递被调用的函数指针(即"回调").Java的面向对象模型目前不支持方法指针,因此似乎排除了使用这种舒适的机制.但一切都不会丢失!

Java对接口的支持提供了一种机制,通过它我们可以获得相当于的回调.诀窍是定义一个简单的接口,声明我们希望调用的方法.

例如,假设我们希望在事件发生时得到通知.我们可以定义一个接口:

public interface InterestingEvent
{
    // This is just a regular method so it can return something or
    // take arguments if you like.
    public void interestingEvent ();
}
Run Code Online (Sandbox Code Playgroud)

这使我们能够抓住实现该接口的类的任何对象.因此,我们不需要关心任何其他类型的信息.这比使用Motif使用C++代码时使用小部件的数据字段来保存对象指针的黑客蹦床C函数要好得多.

将发出事件信号的类需要期望实现InterestingEvent接口的对象,然后根据需要调用interestingEvent()方法.

public class EventNotifier
{
    private InterestingEvent ie;
    private boolean somethingHappened; 
    public EventNotifier (InterestingEvent event)
    {
    // Save the event object for later use.
    ie = event; 
    // Nothing to report yet.
    somethingHappened = false;
    } 
    //...  
    public void doWork ()
    {
    // Check the predicate, which is set elsewhere.
    if (somethingHappened)
        {
        // Signal the even by invoking the interface's method.
        ie.interestingEvent ();
        }
    //...
    } 
    // ...
}
Run Code Online (Sandbox Code Playgroud)

在该示例中,我使用somethingHappened谓词来跟踪是否应该触发事件.在许多情况下,调用该方法的事实足以保证发出interestingEvent()信号.

希望接收事件通知的代码必须实现InterestingEvent接口,并将对自身的引用传递给事件通知程序.

public class CallMe implements InterestingEvent
{
    private EventNotifier en; 
    public CallMe ()
    {
    // Create the event notifier and pass ourself to it.
    en = new EventNotifier (this);
    } 
    // Define the actual handler for the event.
    public void interestingEvent ()
    {
    // Wow!  Something really interesting must have occurred!
    // Do something...
    } 
    //...
}
Run Code Online (Sandbox Code Playgroud)

这里的所有都是它的.我希望使用这个简单的Java习惯用法可以让你对Java的过渡更加紧张.

  • 虽然此链接可能会回答这个问题,但最好在此处包含答案的基本部分并提供参考链接.如果链接的页面发生更改,则仅链接的答案可能会无效. (12认同)

kuz*_*zdu 6

kotlin 的吊坠。

定义您的界面:在我的示例中,我使用外部库扫描信用卡。

interface ScanIOInterface {
     fun onScannedCreditCard(creditCard: CreditCard)
}
Run Code Online (Sandbox Code Playgroud)

创建一个类,您可以在其中注册Activity/ Fragment

class ScanIOScanner {
  var scannerInterface: ScanIOInterface? = null

  fun startScanningCreditCard() {
      val creditCard = Library.whichScanCreditCard() //returns CreditCard model
      scannerInterface?.onScannedCreditCard(creditCard)
  }
}
Run Code Online (Sandbox Code Playgroud)

在您的Activity/ 中实现接口Fragment

class YourClassActivity extends AppCompatActivity, ScanIOInterface {
    //called when credit card was scanned
    override fun onScannedCreditCard(creditCard: CreditCard) {
        //do stuff with the credit card information
    }

    //call scanIOScanner to register your interface
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
       super.onViewCreated(view, savedInstanceState)

       val scanIOScanner = ScanIOScanner()
       scanIOScanner.scannerInterface = this
    }
} 
Run Code Online (Sandbox Code Playgroud)

CreditCard是一个模型,可以随意定义。就我而言,它包括品牌、数字、有效期......

之后,您可以随心所欲scanIOScanner.startScanningCreditCard()地拨打电话。


Khu*_*Sim 5

视频教程的主要内容是演示如何使用接口来委派不同Fragment和活动之间的方法/数据交换,但这是学习如何在Java for Android中实现委派模式的一个很好的例子。

在此处输入图片说明