安卓接口回调之我见-------仅是安卓小小鸟的见解

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

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

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

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

首先定义一个button按钮的xml文件,很简单吧?
<?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,代码如下

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;
	}
}


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

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不理解,擦擦擦。



对了,效果图如下:


--------------安卓小小鸟成长记

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值