未调用Android自定义对话框片段回调
因此,在我的应用程序中,我需要使用几个相同类型的确认对话框片段,基本上,它有一条消息,是/否和一个积极消息的回调。我设法做到了,除了回调部分,我不明白为什么不调用它。任何帮助都将不胜感激。Thx未调用Android自定义对话框片段回调,android,callback,android-dialogfragment,Android,Callback,Android Dialogfragment,因此,在我的应用程序中,我需要使用几个相同类型的确认对话框片段,基本上,它有一条消息,是/否和一个积极消息的回调。我设法做到了,除了回调部分,我不明白为什么不调用它。任何帮助都将不胜感激。Thx public class MessageDialogFragment2 extends DialogFragment { * Config DialogFrag */ private static String title = ""; private st
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:代码中检测到的任何问题都可以指出,而不是与问题相关的问题。我一直致力于提高我的知识水平和编写更好的代码 显然,缺少了一些东西。我没有在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;
}
}
pfff不敢相信我有多傻:p我已经知道答案了。我很快就会做出正确的回答。