Android 自定义对话框片段回调未被调用
Android Custom Dialog Fragment callback not being called
所以在我的应用程序中,我需要使用多个相同类型的确认 dialogFragments,基本上,它有一条消息,yes/no 和一个针对肯定消息的回调。我设法做到了,除了回调部分,我无法弄清楚为什么它没有被调用。任何帮助,将不胜感激。谢谢
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 class!
中调用回调
@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;
}
}
所以在我的应用程序中,我需要使用多个相同类型的确认 dialogFragments,基本上,它有一条消息,yes/no 和一个针对肯定消息的回调。我设法做到了,除了回调部分,我无法弄清楚为什么它没有被调用。任何帮助,将不胜感激。谢谢
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 class!
中调用回调@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;
}
}