Android callback interface in two ways
Last Update:2015-06-29
Source: Internet
Author: User
<span id="Label3"></p><p><p>Developers familiar with the ms-windows and x Windows Event-driven design patterns typically pass a pointer to a method to the event source, calling this method (also called a "callback") when an event Occurs. The Java object-oriented model does not currently support method pointers and does not seem to be able to use this handy mechanism.</p></p><p><p>But Java itself has interface, and we can use interface to achieve the same effect. In android, there are two ways to do this if we need to go through a Button's click Event.</p></p><p><p>The general steps in the first approach are:</p></p><p><p>1. Define a callback interface to capture and "store" click Events.</p></p><pre><pre> <span style="color: #0000ff;">public </span> <span style="color: #0000ff;">interface </span> <span style=" Color: #000000; "> Uibtninterface {</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> button event: </span> <span s tyle="color: #008000;">//</span> <span style="color: #008000;" "parameter: < span> <span style=" color: #008000; ">//</span> <span style=" color: # 008000; "> 1) Idevent is an event: </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> UIDEF.EVT_CL Icked ... </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> 2) Idbutton is the ID of the button; </span> <span sty le="color: #0000ff;">public </span> <span style="color: #0000ff;">void </span> onbuttonevent (<span style="color: #0000ff;">int </span> Idevent,<span style="color: #0000ff;">int </span> <span style="color: #000000;"> idbutton);} </span> </span></pre></pre><p><p>2, in the event source class, defines a callback interface member variable, the <span style="background-color: #ffffff; color: #000080;">constructor sets a callback interface parameter is used to instantiate this interface,</span> after the Click event of the button, immediately through the member variable call the custom interface method, the Click event "deposit" in the Interface.</p></p><pre><pre> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">int</span> <span style="color: #000000;">Defstyle,uitreeinterface Interface1) { </span><span style="color: #0000ff;">Super</span><span style="color: #000000;">(context, attrs, defstyle); </span> <span style="color: #0000ff;"></span>This. context =<span style="color: #000000;"> context; LOG.D (</span>"TAG", "nodetree"<span style="color: #000000;">); </span> = interface1; <span style="color: #000000;"> }</span></pre></pre><pre>ViewHolder.operate.setOnClickListener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Onclicklistener () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">onclick Admin button Click event Add release Popupwindow</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(nodetree.uitreeinterface! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {nodeTree.uiTreeInterface.onNodeChanged (node, Constant.IDBTN.ID_OPERATE); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(((UIButton) v). uibtninterface! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {((UIButton) v). uibtninterface.onbuttonevent (Constant.IDEVENT.EVT_CLICKED, ((UIButton) v). getId ()); }</span></span></pre><p><p>3, the target class, new an event source class, passed in an anonymous class implementation interface.</p></p><pre><pre><span style="color: #0000ff;">New</span> Nodetree (this,null,0,</pre></pre><pre><pre>New<span> uitreeinterface () { @Override public void onsearchevent (String querytext, list<node><span> Result { //TODO auto-generated Method Stub<span> } @Override public void onnodeevent (node node, int <span> Idevent) { //TODO auto-generated Method stub<span> } @Override public void onnodechanged (node node , int<span> Idevent) {//TODO auto-generated method stub<span> } @Override public void onbuttonevent (int idevent, int <span> Idbutton) {//TODO auto-generated Method stub<span> }};</span> </span> </span> </span> </span> </span> </span> </span> </span></pre></pre><pre><pre>);</pre></pre><p><p></p></p><p><p>In fact, the second way is similar, but the time to return the interface parameters has Changed.</p></p><p><p>1. Define a callback interface to capture and "store" click Events.</p></p><pre><pre> <span style="color: #0000ff;">public </span> <span style="color: #0000ff;">interface </span> <span style=" Color: #000000; "> Uibtninterface {</span> <span style="color: #008000;">//</span> <span style="color: #008000;"> button event: </span> <span s tyle="color: #008000;">//</span> <span style="color: #008000;" "parameter: < span> <span style=" color: #008000; ">//</span> <span style=" color: # 008000; "> 1) Idevent is an event: </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> UIDEF.EVT_CL Icked ... </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> 2) Idbutton is the ID of the button; </span> <span sty le="color: #0000ff;">public </span> <span style="color: #0000ff;">void </span> onbuttonevent (<span style="color: #0000ff;">int </span> Idevent,<span style="color: #0000ff;">int </span> <span style="color: #000000;"> idbutton);} </span> </span></pre></pre><p><p>2. In the event source class, define a member variable for the callback interface, and <span style="color: #333399;">define the register and the Unregister method to instantiate the Interface. </span>after the Click event of the button, the method of invoking the custom interface via the member variable is immediately called, and the Click event is "stored" in the Interface.</p></p><pre><span style="color: #000000;"><span style="color: #000000;"> } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* provided to the interface implementation Party to register themselves * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Listener</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Registeruitreelistener (uitreeinterface Listener) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">logutils.logd (TAG, Commonfunction.getfilelinemethod ());</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Uitreeinterface =<span style="color: #000000;"><span style="color: #000000;">listener; } </span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* provided to the interface implementation party to unregister itself * *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Listener</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Unregisteruitreelistener () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. uitreeinterface! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Uitreeinterface =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } }</span></span></pre><p><p></p></p><pre>ViewHolder.operate.setOnClickListener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Onclicklistener () {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">onclick Admin button Click event Add release Popupwindow</span></span><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View V) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(nodetree.uitreeinterface! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {nodeTree.uiTreeInterface.onNodeChanged (node, Constant.IDBTN.ID_OPERATE); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(((UIButton) v). uibtninterface! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {((UIButton) v). uibtninterface.onbuttonevent (Constant.IDEVENT.EVT_CLICKED, ((UIButton) v). getId ()); }</span></span></pre><p><p>3, the target class to implement this interface, new an event source class, through the register and the Unregister method to bind and Unbind.</p></p><pre>Nodetree.registeruitreelistener (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Uitreeinterface ()<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Callback Interface</span></span><span style="color: #000000;"><span style="color: #000000;">{@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onnodeevent (node node,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Event) { </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(event) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDEVENT.EVT_CLICKED:LOG.D (</span></span>"click", "CLICKED"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(node.isleaf ()) {toast.maketext (getapplicationcontext (), N Ode.getname (), toast.length_short). Show (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDEVENT.EVT_DRAG:LOG.D (</span></span>"DRAG", "DRAG"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDEVENT.EVT_SWIPELEFT:LOG.D (</span></span>"swipeleft", "swipeleft"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDEVENT.EVT_SWIPERIGHT:LOG.D (</span></span>"swiperight", "swiperight"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; }} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onnodechanged (node node,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Idevent) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(idevent) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDBTN.ID_DEL:LOG.D (</span></span>"del", "del"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDBTN.ID_EDIT:LOG.D (</span></span>"edit", "edit"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDBTN.ID_NEW:LOG.D (</span></span>"new", "new"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span><span style="color: #000000;"><span style="color: #000000;">CONSTANT.IDBTN.ID_OPERATE:LOG.D (</span></span>"new"<span style="color: #000000;"><span style="color: #000000;">, Node.getname ()); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; }} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onbuttonevent (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>idevent,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">Idbutton) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>LOG.D ("button"<span style="color: #000000;"><span style="color: #000000;">, string.valueof (idbutton)); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Switch</span></span><span style="color: #000000;"><span style="color: #000000;">(idbutton) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>42031:<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">manage, insert, delete, rename, copy</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>5:<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Check</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>0x1110:<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">New</span></span>Node NewNode =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Node (4, 2, "blue Flowers porcelain",<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">, beans); Nodetree.replacenode (</span></span>4<span style="color: #000000;"><span style="color: #000000;">, newNode); Nodetree.replacecommit (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>0x1111:<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Determine</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> case</span>0x1112:<span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Cancel</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">default</span></span><span style="color: #000000;"><span style="color: #000000;">: </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span><span style="color: #000000;"><span style="color: #000000;">; }} @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onsearchevent (String querytext, List</span></span><Node><span style="color: #000000;"><span style="color: #000000;">Result) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>LOG.D ("search"<span style="color: #000000;"><span style="color: #000000;">, querytext); } });</span></span></pre><p><p>The difference between the two applications is that the former is suitable for interfaces that must be bound, so that two methods can be defined less, while the latter provides an understanding of the binding mechanism and low coupling.</p></p><p><p>Android callback interface in two ways</p></p></span>