接口回调

Android 中的接口回调

在android中,接口回调是一种很常见的机制,它可以大大降低代码间耦合性,提高程序的复用性。我们经常碰到的点击事件就是利用的接口回调机制。

什么是接口回调

接口回调其本质与向上转型是一样的,不同的是:接口回调是利用接口句柄来得到并调用实现这个接口的子类引用;而向上转型则是用父类的句柄来得到并调用继承此类的子类的引用。接口回调,强调使用接口来实现回调对象方法,它并不关心方法的具体实现,而向上转型则牵涉到多态和运行期绑定。

为什么要使用接口回调

接口回调和java设置模式里面的模板方法模式(Template Method)有点类似,当我们不知道调用者具体要干什么的时候,我们只需要暴露出对应的接口或者抽象方法,让他们自己去实现方法就好的,所以说,接口回调能提高代码的复用性,降低耦合性。

如何实现接口回调

下面就简单的说说接口回调的思路:

  • 首先创建一个接口,这个接口用于你在某个情景下执行相应的操作

    public interface Test {
        //接口方法
        public void  doSomething();
    }
    
  • 创建一个功能类,然后,在这个类里面声明回调接口的对象,之后在这个类里面创建在某个情景下需要执行的方法,而且在这个方法里面为声明的接口对象赋值。

    public class Function {
        //声明接口对象
        Test onTest;
    
        public void setOnTest(Test onTest) {
            this.onTest = onTest;
        }
        public  void show(){
            if (onTest!=null) {
                onTest.doSomething();
            }
        }
    }
    

    还有一种方式,就是不声明接口对象,直接将接口对象以参数的形式传进去

    public class Function {
        public void show(Test onTest){
            if (onTest != null) {
                onTest.doSomething();
            }
        }
    }
    
  • 在其他的类中使用这个功能类就可以了。这里我们在MainActivity里面来进行测试,在布局文件中写了一个Button和TextView,当点击不同控件的时候进行不同操作。
    布局文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:padding="15dp"
            android:background="#00ffff"
            android:text="TextView" />
    
        <Button 
            android:id="@+id/bt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            />
    

    MainActivity.java

    public class MainActivity extends Activity implements OnClickListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            TextView tv = (TextView) findViewById(R.id.tv);
            Button bt = (Button) findViewById(R.id.bt);
            tv.setOnClickListener(this);
            bt.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            Function function =new Function();
            switch (v.getId()) {
            //以第一种方式指定点击后执行的具体事件
            case R.id.tv:
                //设置接口对象
                function.setOnTest(new Test() {
                    //被点击后要执行的具体事件
                    @Override
                    public void doSomething() {
                        Toast.makeText(MainActivity.this, "TextView被点击了", 0).show();
                    }
                });
                //调用功能类里面的方法
                function.show();
                break;
            case R.id.bt:
                //以第二种方式指定点击后执行的具体事件
                function.show(new Test() {
    
                    @Override
                    public void doSomething() {
                        Toast.makeText(MainActivity.this, "Button被点击了", 0).show();
                    }
                });
                break;
    
            }
        }
    }
    
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
接口回调是一种常见的编程模式,用于实现程序之间的通信和交互。在Python中,可以通过函数或者类方法来实现接口回调。 如果使用函数来实现接口回调,可以将一个函数作为参数传递给另一个函数或者类的方法,然后在适当的时候调用这个函数来进行回调操作。 示例代码如下: ```python def callback_function(arg1, arg2): # 执行回调操作 print("Callback function called with arguments:", arg1, arg2) def perform_operation(callback): # 进行一些操作 result = 42 # 调用回调函数 callback("arg1", "arg2") # 使用回调函数 perform_operation(callback_function) ``` 上述代码中,`perform_operation` 函数接收一个回调函数作为参数,并在适当的时候调用这个回调函数。在这个例子中,我们调用 `perform_operation` 函数时会执行回调函数 `callback_function`,并传递两个参数 `"arg1"` 和 `"arg2"`。 如果使用类方法来实现接口回调,可以定义一个接口类,其中包含一个或多个方法,然后创建一个实现该接口的类,并将其实例传递给另一个类的方法进行回调操作。 示例代码如下: ```python class CallbackInterface: def callback_method(self, arg1, arg2): # 执行回调操作 print("Callback method called with arguments:", arg1, arg2) class PerformOperation: def __init__(self, callback): self.callback = callback def perform_operation(self): # 进行一些操作 result = 42 # 调用回调方法 self.callback.callback_method("arg1", "arg2") # 使用回调方法 callback_instance = CallbackInterface() perform_operation = PerformOperation(callback_instance) perform_operation.perform_operation() ``` 上述代码中,`CallbackInterface` 类定义了一个回调方法 `callback_method`。`PerformOperation` 类接收一个实现了 `CallbackInterface` 接口的对象作为参数,并在适当的时候调用该对象的回调方法。 无论是使用函数还是类方法来实现接口回调,关键在于正确地传递回调函数或者实现了回调接口的对象,并在适当的时候进行调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值