Android Dialog没有显示

时间:2017-05-30 17:16:11

标签: android android-fragments dialog dialogfragment

每当用户选择一个选项时,我都会尝试显示一个对话框。但是对话框没有显示出来。每当用户按下该选项时,屏幕变得暗淡并且没有显示任何内容。一旦我点击屏幕中的任何位置,屏幕将恢复正常亮度。我不知道是什么造成的,我不知道为什么对话框没有显示给用户。我试过寻找解决方案但找不到相关的东西。我正在调用函数如下

OrderTagsDialogFragment fragment = OrderTagsDialogFragment.newInstance();
fragment.show(getSupportFragmentManager(), fragment.getTitle());

OrderTagsDialogFragment如下所示:

public class OrderTagsDialogFragment extends BaseDialogFragment {

public static BaseDialogFragment newInstance() {
    return new OrderTagsDialogFragment();
}


private View view;

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    MaterialDialogBuilder builder = new MaterialDialogBuilder(getActivity());

    builder.setTitle(getTitle());
    builder.setContent(getContentView());

    builder.setPostiveButton("SUBMIT", new MaterialDialogBuilder.OnMaterialDialogItemClickListener() {
        @Override
        public void onClick(Dialog dialog) {
            sendTagRequest();
            dialog.dismiss();
        }
    });

    builder.setNegativeButton(getString(R.string.dialog_dismiss), new MaterialDialogBuilder.OnMaterialDialogItemClickListener() {
        @Override
        public void onClick(Dialog dialog) {
            dialog.dismiss();
        }
    });
    return builder.create();

}

@Override
public String getTitle() {
    return "Request Tags";
}

private void sendTagRequest() {
    String quantity = (String) ((Spinner) view.findViewById(R.id.tag_quantities_spinner)).getSelectedItem();
    String type = (String) ((Spinner) view.findViewById(R.id.tag_type_spinner)).getSelectedItem();

    final EnteredTagRequest request = new EnteredTagRequest();
    request.numberOfTags = Long.parseLong(quantity);
    request.tagType = type;

    DatabaseDefinition database = FlowManager.getDatabase(AppDatabase.class);
    Transaction transaction = database.beginTransactionAsync(new ITransaction() {
        @Override
        public void execute(DatabaseWrapper databaseWrapper) {
            request.save(databaseWrapper);
        }
    }).success(new Transaction.Success() {
        @Override
        public void onSuccess(Transaction transaction) {
            getBaseActivity().requestSync();
        }
    }).build();
    transaction.execute();
}

@Override
public View getContentView() {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    view = inflater.inflate(R.layout.order_tags_dialog, null);
    return view;
}
}

而BaseDialogFragment类如下:

public abstract class BaseDialogFragment<T> extends AppCompatDialogFragment {

private BaseActivity baseActivity;

public static final int RESULT_SELECTED = Utils.getFreshInt();
public static final int RESULT_DESELECTED = Utils.getFreshInt();

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if(context instanceof BaseActivity) {
        this.baseActivity = (BaseActivity) context;
    } else{
        throw new IllegalArgumentException("Provided context must be instance of BaseActivity");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    this.baseActivity = null;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final View contentView = getContentView();

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    if (contentView != null) {
        Log.d(TAG, "onCreateDialog: contentView != null");

        builder.setView(contentView);
        builder.setTitle(getTitle());

        if (getPositiveButtonAction() != null) {
            builder.setPositiveButton("SELECT", getPositiveButtonAction());
        }

        builder.setNegativeButton(getString(R.string.dialog_dismiss), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_CANCELED, null);
                dismiss();
            }
        });


    } else if (getDisplayList() != null) {
        Log.d(TAG, "onCreateDialog: getDisplayList() != null");
        final List<String> displayedItems = getDisplayList();
        final List<T> selectableItems = getItemList();

        builder.setTitle(getTitle());

        if (this.isMultipleChoice()) {
            builder.setMultiChoiceItems(
                    displayedItems.toArray(new String[displayedItems.size()]),
                    checkedItems(),
                    new DialogInterface.OnMultiChoiceClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int which, boolean selected) {
                            int result_code = selected ? RESULT_SELECTED : RESULT_DESELECTED;
                            getTargetFragment().onActivityResult(getTargetRequestCode(), result_code, createResultIntent(selectableItems.get(which)));
                        }
                    }
            );
        } else {
            builder.setItems(displayedItems.toArray(new String[displayedItems.size()]), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, createResultIntent(selectableItems.get(which)));
                }
            });
        }
    }

    return builder.create();

}

public abstract String getTitle();

public DialogInterface.OnClickListener getPositiveButtonAction() {
    return null;
}

public boolean isMultipleChoice() {
    return false;
}

public boolean[] checkedItems() {
    return new boolean[getItemList().size()];
}

public Intent createResultIntent(T item) {
    return null;
}

public View getContentView() {
    return null;
}

public List<String> getDisplayList() {
    return null;
}

public List<T> getItemList(){
    return null;
}

public BaseActivity getBaseActivity() {
    return baseActivity;
}
}

MaterialDialogBu​​ilder如下:

public class MaterialDialogBuilder {

private Context context;

private String title;
private String content;
private View viewContent;
private int layoutId;

private String negativeButtonText;
private String postiveButtonText;

private OnMaterialDialogItemClickListener negativeButtonAction;
private OnMaterialDialogItemClickListener positiveButtonAction;

public interface OnMaterialDialogItemClickListener {
    public void onClick(Dialog dialog);
}


public MaterialDialogBuilder(Context context) {
    this.context = context;
}

public MaterialDialogBuilder setTitle(String title) {
    this.title = title;
    return this;
}

public MaterialDialogBuilder setContent(String content) {
    this.content = content;
    return this;
}

public MaterialDialogBuilder setContent(int layoutId) {
    this.layoutId = layoutId;
    return this;
}

public MaterialDialogBuilder setContent(View content) {
    this.viewContent = content;
    return this;
}

public MaterialDialogBuilder setNegativeButton(@NonNull String title, @NonNull OnMaterialDialogItemClickListener listener) {
    this.negativeButtonText = title;
    this.negativeButtonAction = listener;
    return this;
}

public MaterialDialogBuilder setPostiveButton(@NonNull String title, @NonNull OnMaterialDialogItemClickListener listener) {
    this.postiveButtonText = title;
    this.positiveButtonAction = listener;
    return this;
}

public Dialog create() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(context);

    final Dialog dialog = builder.create();
    dialog.setContentView(createView(dialog));

    return dialog;
}

public View createView(final Dialog dialog) {
    View view = LayoutInflater.from(context).inflate(R.layout.generic_dialog, null, false);
    final AQuery dialogQuery = new AQuery(view);

    if (this.layoutId != 0) {
        this.viewContent = LayoutInflater.from(context).inflate(this.layoutId, (ViewGroup) view, false);
        LinearLayout contentContainer = (LinearLayout) dialogQuery.id(R.id.content_container).getView();
        contentContainer.addView(this.viewContent);
        dialogQuery.id(R.id.content).gone();
    } else if (this.viewContent != null) {
        LinearLayout contentContainer = (LinearLayout) dialogQuery.id(R.id.content_container).getView();
        contentContainer.addView(this.viewContent);
        dialogQuery.id(R.id.content).gone();
    }

    dialogQuery.id(R.id.title).text(this.title);
    dialogQuery.id(R.id.content).text(this.content);

    if (postiveButtonText != null) {
        dialogQuery.id(R.id.positive_button).text(this.postiveButtonText).clicked(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                positiveButtonAction.onClick(dialog);
            }
        });
    } else {
        dialogQuery.id(R.id.positive_button).gone();
    }

    if (negativeButtonText != null) {
        dialogQuery.id(R.id.negative_button).text(this.negativeButtonText).clicked(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                negativeButtonAction.onClick(dialog);
            }
        });
    } else {
        dialogQuery.id(R.id.negative_button).gone();
    }

    return view;
}
}

0 个答案:

没有答案