说明
为对象外部设置一个方法,使得这个对象可以执行外部设置的方法。通常这个方法是定义在接口中的抽象方法,外部设置的时候,直接设置这个接口对象即可。
如何定义一个回调
-
定义接口 : 在类中定义一个Interface, 并在接口中定义一个抽象方法;
-
接口对象 : 在类中定义一个该接口的成员变量;
-
设置对象 : 在类中定义一个公共的方法, 可以设置这个接口的对象, 调用该方法给接口对象成员变量赋值;
-
调用方法 : 在合适的位置调用接口对象中的方法;
代码示例
public class ProxyAlertDialogFragment extends DialogFragment {
public static final String TAG = "ProxyAlertDialogFragment";
// 1.定义接口 : 在类中定义一个Interface, 并在接口中定义一个抽象方法
public interface OnAlertDialogCallbacks {
void onClick(String username, String password);
void cancel();
}
// 2.接口对象 : 在类中定义一个该接口的成员变量
public OnAlertDialogCallbacks mOnAlertDialogCallbacks = null;
public static ProxyAlertDialogFragment getInstance() {
ProxyAlertDialogFragment f = new ProxyAlertDialogFragment();
return f;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
setCancelable(false);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Dialog dialog;
final EditText usernameInput = new EditText(getActivity());
usernameInput.setHint(getResources().getString(R.string.txt_add_proxy_user_name_text_hint));
final EditText passwordInput = new EditText(getActivity());
passwordInput.setHint(getResources().getString(R.string.txt_add_proxy_password_text_hint));
passwordInput.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
LinearLayout linerLayout = new LinearLayout(getActivity());
linerLayout.setOrientation(LinearLayout.VERTICAL);
linerLayout.addView(usernameInput);
linerLayout.addView(passwordInput);
dialog = builder
.setTitle(getString(R.string.txt_proxy_auth_title))
.setMessage(getString(R.string.txt_proxy_auth_message))
.setView(linerLayout)
.setPositiveButton(R.string.action_settings, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 3.设置对象 : 在类中定义一个公共的方法, 可以设置这个接口的对象, 调用该方法给接口对象成员变量赋值
if (mOnAlertDialogCallbacks != null) {
mOnAlertDialogCallbacks.onClick(usernameInput.getText().toString(), passwordInput.getText().toString());
}
}
})
.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (mOnAlertDialogCallbacks != null) {
mOnAlertDialogCallbacks.cancel();
}
}
}).create();
return dialog;
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
public void setOnAlertDialogCallbacks(OnAlertDialogCallbacks callbacks) {
mOnAlertDialogCallbacks = callbacks;
}
}
public class UseConditionFragment extends Fragment implements View.OnClickListener {
private WebView mWebView;
private boolean mIsCancelProxy = false;
// 4.调用方法 : 在合适的位置调用接口对象中的方法
private void showDialog(HttpAuthHandler handler){
ProxyAlertDialogFragment dialogFragment = ProxyAlertDialogFragment.getInstance();
dialogFragment.setOnAlertDialogCallbacks(new ProxyAlertDialogFragment.OnAlertDialogCallbacks() {
@Override
public void onClick(String username, String password) {
handler.proceed(username,password);
}
@Override
public void cancel() {
mIsCancelProxy = true;
mWebView.reload();
}
});
dialogFragment.show(getParentFragmentManager(), null);
}
}