对话框片段未显示

时间:2019-03-14 03:05:09

标签: android dialog fragment

我想显示片段对话框。但对话框未显示。我不知道哪里存在问题。我也确实在'onCreateDialog'中创建了视图。编写对话框片段xml代码时有什么规则吗?如果您知道问题出在哪里,请告诉我。总是谢谢你。

这是我的密码。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ReviewDialogFragment dialogFragment = ReviewDialogFragment.newInstance();
            dialogFragment.show(getSupportFragmentManager(), "dialog");
        }
    });
}



public class ReviewDialogFragment extends DialogFragment {

private static final String APP_ID_KEY = "appId";
private Context context;
private ImageView albumCoverImg;
private TextView mainTxt;
private TextView subTxt;
private Button likeBtn;
private TextView dislikeTxt;

public static ReviewDialogFragment newInstance() {
    ReviewDialogFragment fragment = new ReviewDialogFragment();
    return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_fragment_review, container, false);
    albumCoverImg = (ImageView) view.findViewById(R.id.review_album_cover_img);
    mainTxt = (TextView) view.findViewById(R.id.review_main_txt);
    subTxt = (TextView) view.findViewById(R.id.review_sub_txt);
    likeBtn = (Button) view.findViewById(R.id.review_like_btn);
    dislikeTxt = (TextView) view.findViewById(R.id.review_dislike_txt);
    dislikeTxt.setText(Html.fromHtml("<u>" + "아뇨, 별로에요 :(" + "</u>"));
    setLikeBtn();
    setDislikeTxt();
    return view;
}

private void setLikeBtn() {
    likeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    dislikeTxt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
}

private void setDislikeTxt() {
    this.dismiss();
}

2 个答案:

答案 0 :(得分:1)

尝试此解决方案

private void showDialog(String text, boolean isUpdate) {

        FragmentManager fm = getActivity().getSupportFragmentManager();
        if (fm != null) {
            ReviewDialogFragment  reviewDialogFragment = new ReviewDialogFragment ();
            reviewDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Light_NoTitleBar_Fullscreen);
            reviewDialogFragment.show(fm, ReviewDialogFragment.class.getName());
        }
    }

答案 1 :(得分:1)

也尝试一下:

void showDialog() {
mStackLevel++;

// DialogFragment.show() will take care of adding the fragment
// in a transaction.  We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = getFragmentManager().beginTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
    ft.remove(prev);
}
ft.addToBackStack(null);

// Create and show the dialog.
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
newFragment.show(ft, "dialog");

}

Doc:https://developer.android.com/reference/android/app/DialogFragment