Two simple ways of writing android interface callback

Two simple ways of writing android interface callback - B509 Tips

1. The first internal rewrite (simpler, commonly used asynchronously)
(1) Create an interface and implement the method (2) Implement the method of passing the interface object as a parameter in the acquisition data class
(3) Call the return data in the main class method and pass the interface object, and override the method

1. First create a CallBack interface to pass the content as follows

里面实现两个方法Success和Fail用来返回数据

public  class MyInterface  {
  public  interface CallBack{
    void Success(String success);
    void Fail(String fail);
  }
}

2. Secondly, write the class BackData for obtaining data (it can be called a transfer bridge)

这里面定义这个TransportContext方法用来接收参数也就是callBack对象

public class BackData {
/**
 * 效果就是第一次点击返回成功第二次返回失败
 */
private static boolean isInternet;//模拟网络情况

public static void TransportContext(MyInterface.CallBack callBack) {


    if (!isInternet) {
        callBack.Success("成功");

        isInternet = true;

    } else {
        callBack.Fail("失败");
    }
  }
}

3. Call the delivery method (bridge) in the main class to define a CallBackActivity

 调用TransportContext这个方法。传递callBack对象并重写里面的方法

public class CallBackActivity extends AppCompatActivity{
private TextView tv_callBack_click;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_back);
    tv_callBack_click = (TextView) findViewById(R.id.tv_callBack_click);
}
       //按钮点击事件
public void onClick_callback(View view) {

    BackData.TransportContext(new MyInterface.CallBack() {
        //重写接口方法 获取数据
        @Override
        public void Success(String success) {
            tv_callBack_click.setText(success);
        }
        @Override
        public void Fail(String fail) {
            tv_callBack_click.setText(fail);
        }
    });

}
}

4. The layout is too simple, so I won't post it, and I will post a rendering of it

write picture description here write picture description here

2. The second external rewriting method (in fact, the essence is the same)
Step
1. Create an interface and implement the methods in it
2. Where do you want to return the message (main class) to implement the interface and rewrite the method
3. In the main class The context is passed to another class (actually an interface object)
4. Call the method in the interface with the passed interface object
5. Get the message in the callback

1. The interface is the same as above
2. The method for obtaining data is the same
3. The main class

public class CallBackActivity extends AppCompatActivity implements MyInterface.CallBack {
private TextView tv_callBack_click;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_back);
    tv_callBack_click = (TextView) findViewById(R.id.tv_callBack_click);
}
 //按钮点击事件
public void onClick_callback(View view) {
    //传递上下文
    BackData.TransportContext(this);
}

//外部重写方法
@Override
public void Success(String success) {
    tv_callBack_click.setText(success);
}
@Override
public void Fail(String fail) {
    tv_callBack_click.setText(fail);
}
}

4. The effect picture is the same, so I won't post it, I hope it will be useful

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479168&siteId=291194637