SoFunction
Updated on 2025-04-07

Introduction to the use of callback interface in Android


package ;
import ;
import ;
import ;
/**
* Demo description:
* Use of callback interface in Android
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
(savedInstanceState);
setContentView();
init();
}
private void init() {
for (int i = 0; i < 10000; i++) {
if (i == 9527) {
showToast(i, new CallBackInterface() {
@Override
public void callBackFunction(int i) {
(, "My number:"+i, Toast.LENGTH_LONG).show();
}
});
}
}
}
//Define the function, one of the parameters is of CallBackInterface type
private void showToast(int i, CallBackInterface callBackInterface) {
(i);
}

//Define the interface. Define a method in the interface
public interface CallBackInterface {
public void callBackFunction(int i);
}
}