匿名内部类实现接口回调的总结以及demo

一、Android中为什么使用匿名内部类的方式?

   使用一次之后会自动被填充到垃圾回收机制被销毁,节省手机的内存,所以在Android中使用普遍。

二、如何使用

方式1

      1.1定义一个接口

     1.2 声明对象

     

      1.3.需要传递数据的地方写一个方法,然后把接口作为实际参数传递进去

      1.4、在获取数据的地方注册一下此接口,并实现接口的方法

          

 

         

源码:

GetAsynData接口:
public interface GetAsynData {
    void setName(String name);
}
MyHandler 类(传递数据的类)
public  class MyHandler extends Handler {
    WeakReference mainActivityWeakReference;
    public MyHandler(MainActivity mainActivity) {
        mainActivityWeakReference = new WeakReference(mainActivity);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        String name = msg.getData().getString("name");
        if (getAsynData!=null) {
            getAsynData.setName(name);
        }
    }
    public GetAsynData getAsynData ;
    public void setOnClickListener(GetAsynData getAsynData){
            this.getAsynData = getAsynData;
    }
}

MainActivity  接收数据的类

public class MainActivity extends AppCompatActivity implements GetAsynData {
    MyHandler handler = new MyHandler(this);
    private TextView mTvName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Message message = Message.obtain();
        message.what = 0;
        Bundle bundle = new Bundle();
        bundle.putString("name", "lucy");
        message.setData(bundle);
        handler.sendMessage(message);
    }

    private void initView() {
        mTvName = findViewById(R.id.id_tv_name);
        handler.setOnClickListener(this);
    }

    @Override
    public void setName(String name) {
        mTvName.setText(name);
    }
}

方式二:使用构造函数的方式,匿名内部类

 2.1创建一个接口

 

 2.2 声明接口并创建构造函数

匿名内部类实现接口回调的总结以及demo_第1张图片

2.3匿名内部类的方式实现接口

匿名内部类实现接口回调的总结以及demo_第2张图片 

源码:

GetAsynData 接口
public interface GetAsynData {
    void setName(String name);
}
传递数据
public  class MyHandler extends Handler {
    WeakReference mainActivityWeakReference;
    public GetAsynData getAsynData ;
    public MyHandler(MainActivity mainActivity,GetAsynData getAsynData) {
        mainActivityWeakReference = new WeakReference(mainActivity);
        this.getAsynData = getAsynData;
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        String name = msg.getData().getString("name");
        if (getAsynData!=null) {
            getAsynData.setName(name);
        }
    }

}

接收数据:

public class MainActivity extends AppCompatActivity {
    MyHandler handler = new MyHandler(this, new GetAsynData() {
        @Override
        public void setName(String name) {
            mTvName.setText(name);
        }
    });
    private TextView mTvName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Message message = Message.obtain();
        message.what = 0;
        Bundle bundle = new Bundle();
        bundle.putString("name", "lucy");
        message.setData(bundle);
        handler.sendMessage(message);
    }

    private void initView() {
        mTvName = findViewById(R.id.id_tv_name);
    }

}
源码已经贴出来了,就暂时不提供demo了

你可能感兴趣的:(匿名内部类实现接口回调的总结以及demo)