Android接口回调

来源:互联网 发布:数据分析专业 上海财经 编辑:程序博客网 时间:2024/06/11 15:05
转载自http://blog.csdn.net/wzlyd1/article/details/44006443

题外话:这篇博客包含了我其中一个最耗费我时间去理解的知识点。理解能力渣渣,接口回调技术曾经用了两星期去理解才理解,说理解我都不自信,因为我都不知道我写的是不是接口回调,只是因为我看着长得像,就写出来看看,顺便做自我记录,恳请大神们拍砖。

首先感谢loader大神和龙伟大神的指点还有csdn各个大神的博客,没有你们写的接口回调我也不会理解

接口回调技术,说起来高大上,很多人一听云里雾里的,官方解释更是让人感到乱七八糟,初学者很容易迷惑,跟特么说绕口令似的,比如,比较官方的解释是这么说的 :所谓回调:就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D,D这个方法就叫回调方法。卧槽,好特么绕,谁念到这里也会晕菜。要我说,干脆就把他功能说出来就行了,接口回调就是来传递结果的,多简单!比如你有两个类,A类B类,B类完成一个加法运算,那么它怎么把结果告诉A类呢,你就考虑接口回调就行了!说到这你不理解没关系,继续看。

先说一个需求,需求很简单,点击一下button,改一下textview的值,用接口回调方法实现。(我知道不用接口回调就能实现,这只是为了简单起见,一些大神们写博客为了把接口回调解释的清楚,说了很详细的例子,结果我是越看越晕,我们一刀见血,从最常见开始)。

首先定义一个button按钮的xml文件,很简单吧?
[html] view plain copy
print?
  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“wrap_content” >  
  5.   
  6.     <Button  
  7.         android:id=“@+id/id_custombutton”  
  8.         android:layout_width=“match_parent”  
  9.         android:layout_height=“wrap_content”  
  10.         android:text=“按钮” />  
  11.   
  12. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content" >    <Button        android:id="@+id/id_custombutton"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="按钮" /></LinearLayout>

下面我们定义这个button为CustomButton,代码如下

[java] view plain copy
print?
  1. package com.derek.com;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.LinearLayout;  
  9.   
  10. public class CustomButton extends LinearLayout {  
  11.   
  12.     private View customView;  
  13.     /** 
  14.      * 我们定义的接口,一开始我也不理解接口 
  15.      * 如果你也不理解, 
  16.      * 那么就把它想象成一个特殊的类 
  17.      */  
  18.     private CustomClickListener listener;  
  19.       
  20.       
  21.     private Button button;  
  22.   
  23.     private int mCount = 0;  
  24.     public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {  
  25.         super(context, attrs, defStyleAttr);      
  26.         initview(context);//加载布局  
  27.         initEvent();//注册监听  
  28.     }  
  29.   
  30.     public CustomButton(Context context, AttributeSet attrs) {  
  31.     this(context, attrs,0);  
  32.       
  33.     }  
  34.     public CustomButton(Context context) {  
  35.         this(context,null);  
  36.           
  37.     }  
  38.   
  39.     private void initview(Context context) {  
  40.         LayoutInflater inflater = LayoutInflater.from(context);  
  41.         //加载布局  
  42.         customView = inflater.inflate(R.layout.custombutton, this,true);  
  43.         //找到我们的button  
  44.         button = (Button) customView.findViewById(R.id.id_custombutton);  
  45.   
  46.     }  
  47.   
  48.     private void initEvent() {  
  49.         button.setOnClickListener(new OnClickListener() {  
  50.   
  51.             @Override    
  52.             public void onClick(View v) {  
  53.                   
  54.                 /** 
  55.                  * button被点击一次,mCount加一 
  56.                  */  
  57.                 mCount++;  
  58.                 /** 
  59.                  * 这一步是关键,你看懂了么?我们要把mCount的值传出去,对吧? 
  60.                  *理解listener参数么?(一开始我不理解的,因为我笨,想象成类的对象是不是就理解了) 
  61.                  * 
  62.                  * 
  63.                  *再再次:实例化后的接口对象调用你接口里的方法,并将你的结果传入。 
  64.                  */  
  65.                 listener.onCustomClick(mCount);  
  66.             }  
  67.   
  68.         });  
  69.   
  70.     }  
  71.       
  72.       
  73.       
  74.       
  75.     /** 
  76.      *  
  77.      *你定义一个接口,里面写一个方法,你需要传递什么结果,就把结果当方法的参数。 
  78.      * 
  79.      */  
  80.       
  81.     public  interface  CustomClickListener{  
  82.           
  83.         //我们传递的是mCount  
  84.         void onCustomClick(int count);  
  85.     }   
  86.     /** 
  87.      * 这一步理解吗?不要把它想的高端,就是为了初始化listener参数的.其实他就是我们常用的 
  88.      * set,get方法,在写bean的时候经常用,比如写一个Student的类,里面有name,id等等也是这么 
  89.      * 初始化的,是吧? 
  90.      *  
  91.      *  
  92.      *  
  93.      * 再次:写一个set方法初始化这个接口对象。 
  94.      * 
  95.      */  
  96.     public void setCustomClickListener(CustomClickListener listener) {  
  97.   
  98.         this.listener = listener;  
  99.     }  
  100. }  
package com.derek.com;import android.content.Context;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;public class CustomButton extends LinearLayout {    private View customView;    /**     * 我们定义的接口,一开始我也不理解接口     * 如果你也不理解,     * 那么就把它想象成一个特殊的类     */    private CustomClickListener listener;    private Button button;    private int mCount = 0;    public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);            initview(context);//加载布局        initEvent();//注册监听    }    public CustomButton(Context context, AttributeSet attrs) {    this(context, attrs,0);    }    public CustomButton(Context context) {        this(context,null);    }    private void initview(Context context) {        LayoutInflater inflater = LayoutInflater.from(context);        //加载布局        customView = inflater.inflate(R.layout.custombutton, this,true);        //找到我们的button        button = (Button) customView.findViewById(R.id.id_custombutton);    }    private void initEvent() {        button.setOnClickListener(new OnClickListener() {            @Override              public void onClick(View v) {                /**                 * button被点击一次,mCount加一                 */                mCount++;                /**                 * 这一步是关键,你看懂了么?我们要把mCount的值传出去,对吧?                 *理解listener参数么?(一开始我不理解的,因为我笨,想象成类的对象是不是就理解了)                 *                 *                 *再再次:实例化后的接口对象调用你接口里的方法,并将你的结果传入。                 */                listener.onCustomClick(mCount);            }        });    }    /**     *      *你定义一个接口,里面写一个方法,你需要传递什么结果,就把结果当方法的参数。     *     */    public  interface  CustomClickListener{        //我们传递的是mCount        void onCustomClick(int count);    }     /**     * 这一步理解吗?不要把它想的高端,就是为了初始化listener参数的.其实他就是我们常用的     * set,get方法,在写bean的时候经常用,比如写一个Student的类,里面有name,id等等也是这么     * 初始化的,是吧?     *      *      *      * 再次:写一个set方法初始化这个接口对象。     *     */    public void setCustomClickListener(CustomClickListener listener) {        this.listener = listener;    }}


然后主界面调用,方法如下

[java] view plain copy
print?
  1. package com.derek.com;  
  2.   
  3. import com.derek.com.CustomButton.CustomClickListener;  
  4.   
  5. import android.os.Bundle;  
  6. import android.support.v7.app.ActionBarActivity;  
  7. import android.widget.TextView;  
  8.   
  9. public class MainActivity extends ActionBarActivity {  
  10.   
  11.     private CustomButton button;  
  12.     private TextView tv;  
  13.   
  14.     @Override  
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.   
  19.         tv = (TextView) findViewById(R.id.id_maintext);  
  20.         button = (CustomButton) findViewById(R.id.id_maincustombutton);  
  21.   
  22.         button.setCustomClickListener(new CustomClickListener() {  
  23.   
  24.             @Override  
  25.             public void onCustomClick(int count) {  
  26.   
  27.                 tv.setText(”“ + count);  
  28.   
  29.             }  
  30.         });  
  31.     }  
  32.   
  33. }  
package com.derek.com;import com.derek.com.CustomButton.CustomClickListener;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.widget.TextView;public class MainActivity extends ActionBarActivity {    private CustomButton button;    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) findViewById(R.id.id_maintext);        button = (CustomButton) findViewById(R.id.id_maincustombutton);        button.setCustomClickListener(new CustomClickListener() {            @Override            public void onCustomClick(int count) {                tv.setText("" + count);            }        });    }}
是不是很简单,刚写博客的时候又发现了一个规律,如果你看完博客还是不明白,你就这么记

首先:接口回调是用来传递你的结果的。

其次:接口回调很简单,你定义一个接口,里面写一个方法,你需要传递什么结果,就把结果当方法的参数。
再次:写一个set方法初始化这个接口对象。
再再次:实例化后的接口对象调用你接口里的方法,并将你的结果传入。

哦了,接口回调写完了

我把这个规律写到注释中,你们就明白了

其实这个说白了就是利用了接口里的方法是必须要重写的,当接口的方法重写的时候,count的值就被传到另一个类里啦。

明白了吗大家,下一篇记录一下inflate方法里root参数,因为我在写这个例子的时候也特么出现问题了就是因为root不理解,擦擦擦。



对了,效果图如下:


————–安卓小小鸟成长记