【问题标题】:Dialog Fragment not showing对话框片段未显示
【发布时间】:2019-03-14 03:05:08
【问题描述】:

我想显示片段对话框。但对话框没有出现。不知道哪里有问题。我也在“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();
}

【问题讨论】:

  • 您发布的代码不会覆盖onCreateDialog() - 当您使用show() 时确实需要这样做。你对onCreateDialog() 的实现是什么样的?

标签: android dialog fragment


【解决方案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());
        }
    }

【讨论】:

    【解决方案2】:

    也试试这个:

    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");
    

    }

    文档:https://developer.android.com/reference/android/app/DialogFragment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多