如何监听用户自定义事件?(或者对象之间的通信机制是什么)



我是android(和java)的新手,我想知道对象之间的通信机制是什么?比如,如果我想监视私有成员的值,如果它改变了,我想触发一个方法。

我知道uievent有事件监听器。但如果不是鼠标点击,而是私有成员的值发生了变化呢?我试过intent/broadcastreceiver,但是我不认为那是我想做的。

非常感谢!!

这里可以使用基于发布者订阅者的方法。包含私有(Publisher)成员的类将有一个方法RegisterForChange,该方法将采用一些接口ISomeInterface作为参数,并且Subscribe r类将实现该接口。一旦私有成员发生了变化,你可以调用该接口的SomeMethod方法来通知Subscriber我的私有成员发生了变化

 //pseudocode
    public interface ISomeInterface
    {
        SomeMethod();
    }
    class Publisher 
    {
        RegisterForChange(ISomeInterface inter)
        {
            inter.SomeMEthod();
        }
        //whenever there is change call 
        inter.SomeMethod();
    }
    class Subsscriber implements ISomeInterface
    {
        //inside this class register for change by calling
        Publisher.RegsiterForChange(this);
        // this method will be called whenever there is change in private member
        public SomeMethod()
        {
        }
    }

相关内容

最新更新