java回调机制

hjgg6969 8年前
   <p>所谓回调:就是A类中调用B类中的某个方法,然后B类中反骨来调用A类中的方法D,D这个方法就叫做回调方法。<br> 1、Class A实现了接口CallBack callback<br> 2、class A包含一个class B的引用b<br> 3、class B 有一个参数为callback的方法f(Callback callback)<br> 4、A的对象a调用B的方法f(Callback callback)\<br> 5、然后b就可以在f(Callback callback)方法中调用A中的方法,————B类中的方法</p>    <pre>  <code class="language-java">//A  public interface Callback{           //回调函数          public void solve(String result);  }  public class Wang implements Callback{          private Li li;          public Wang(Li li){              this.li=li;          }          public void askQuestion(final Stirng question){                new thread(new Runnable(){                        @Override                        public void run(){                                li.executeMessage(Wang.this,question);                        }                }).start();                play();        }        public void play(){                  System.out.println("我去逛街去了");        }         //B的回调方法        @Override        public void solve(String result){                  System.out.ptintln("小李告诉小王的答案的是--- ");        }  }  //B  public class Li{        public void executeMessage(CallBack callBack,String question){                System.out.prinltn("问的问题--->"+question);                String result="8";                callBack.solve(result);      }  }  public class Test{          public static void main(String [] args){                    Li li=new Li();                    Wang wang=new Wang(li);                    wang.askQuestion("1+1=?");          }  }</code></pre>    <p>现在来分析下Android View的点击方法onlick(),我们知道onClick()是一个回调方法,当用户点击View就执行这个方法,我们用Button来举例</p>    <pre>  <code class="language-java">public interface OnClickListener{            void onClick(View v);  }  public class MainActivity extends Activity implements OnClickListener{              private Button button;              @Override              public void onCreate(Bundle savedInstanceState){                        super.onCreate(savedInstanceState);                        setContentView(R.layout.activity_main);                        button=(Button)findViewById(R.id.button1);                        button.setOnClickListener(this);              }              //这个是回调函数              @Override               public void onClick(View v){                        Toast.makeText(getApplication(), "OnClick", Toast.LENGTH_LONG).show();                 }  }    /**    * 这个View就相当于B类    * @author xiaanming    *    */    public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {        /**        * Listener used to dispatch click events.        * This field should be made private, so it is hidden from the SDK.        * {@hide}        */        protected OnClickListener mOnClickListener;          /**        * setOnClickListener()的参数是OnClickListener接口------>背景三        * Register a callback to be invoked when this view is clicked. If this view is not        * clickable, it becomes clickable.        *        * @param l The callback that will run        *        * @see #setClickable(boolean)        */          public void setOnClickListener(OnClickListener l) {            if (!isClickable()) {                setClickable(true);            }            mOnClickListener = l;        }            /**        * Call this view's OnClickListener, if it is defined.        *        * @return True there was an assigned OnClickListener that was called, false        *         otherwise is returned.        */        public boolean performClick() {            sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);              if (mOnClickListener != null) {                playSoundEffect(SoundEffectConstants.CLICK);                  //这个不就是相当于B类调用A类的某个方法D,这个D就是所谓的回调方法咯                mOnClickListener.onClick(this);                return true;            }              return false;        }    }</code></pre>    <p>这个例子就是Android典型的回调机制,看完这个你是不是更进一步的理解了回调机制呢? 线程run()也是一个回调方法,当执行Thread的start()方法就会回调这个run()方法,还有处理消息都比较经典等等</p>    <p>来自:http://www.jianshu.com/p/18ec24b6a39f</p>