因此,在我的应用程序中,我需要使用相同类型的多个确认dialogFragments,基本上,它有一条消息,是/否以及正面消息的回调。我设法做到了,除了回调部分,我无法弄清楚为什么它没有被调用。任何帮助,将不胜感激。 THX。
public class MessageDialogFragment2 extends DialogFragment {
* Config DialogFrag
*/
private static String title = "";
private static String message = "";
private static String positiveButtonValue = "";
private static String negativeButtonValue = "";
private static MessageDialogFragment2 myDialog;
public static void newInstance() {
myDialog = new MessageDialogFragment2();
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.exit_dialog_fragment, container, false);
setCancelable(false);
return v;
}
private static void showDialog(FragmentManager fragmentManager, String dialogId){
myDialog.show(fragmentManager, dialogId);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.inject(this, view);
//
// setValues();
}
@OnClick({R.id.positiveButton, R.id.negativeButton})
public void exit(View view){
switch(view.getId()){
case R.id.positiveButton:
break;
case R.id.negativeButton:
MessageDialogFragment2.this.dismiss();
break;
}
}
public static class MakeDialog{
private Activity activity;
private PositiveCallback positiveCallBack;
private NegativeCallback negativeCallBack;
public MakeDialog(Activity act){
this.activity = act;
newInstance();
}
public MakeDialog setTitle(String title2){
title = title2;
return this;
}
public MakeDialog setMessage(String message2){
message = message2;
return this;
}
public MakeDialog setPositiveButtonMessage(String message){
positiveButtonValue = message;
return this;
}
public MakeDialog setNegativeButtonMessage(String message){
negativeButtonValue = message;
return this;
}
public void show(){
showDialog(activity.getFragmentManager(), DIALOG_ID);
}
public MakeDialog setPositiveCallBack(PositiveCallback pcb){
this.positiveCallBack = pcb;
return this;
}
public MakeDialog setNegativeCallBack(NegativeCallback ncb){
this.negativeCallBack = ncb;
return this;
}
public interface PositiveCallback {
public void doPositiveCallback();
}
public interface NegativeCallback {
public void doNegativeCallback();
}
}
}
这样称呼:
new MessageDialogFragment2.MakeDialog(this)
.setTitle(getResources().getString(R.string.exit_title))
.setMessage(getResources().getString(R.string.exit_message))
.setPositiveButtonMessage(getResources().getString(R.string.yes))
.setNegativeButtonMessage(getResources().getString(R.string.no))
.setPositiveCallBack(new MessageDialogFragment2.MakeDialog.PositiveCallback() {
@Override
public void doPositiveCallback() {
doSomething();
}
})
.show();
预期结果:doSomething()被调用
实际结果:未调用doSomething()。
PS:可以指出在代码中检测到的任何问题都没有问题。我一直都在努力提高自己的知识,编写更好的代码!
答案 0 :(得分:0)
显然,有些东西不见了。我没有在DialogFragment类中调用回调函数!
@OnClick({R.id.positiveButton, R.id.negativeButton})
public void exit(View view){
switch(view.getId()){
case R.id.positiveButton:
if(positiveCallBack == null)
MessageDialogFragment2.this.dismiss();
else
positiveCallBack.doPositiveCallback();
break;
case R.id.negativeButton:
if(negativeCallBack == null)
MessageDialogFragment2.this.dismiss();
else
negativeCallBack.doNegativeCallback();
break;
}
}