我希望将自定义弹出窗口显示在 fragment 中。不幸的是,对象对话框没有引用正确的布局。我怎样才能让它指向正确的布局?我希望有人能向我解释错误在哪里,以便我可以调用它。
public class UserFragmentBestell<Textview> extends Fragment {
RecyclerView recyclerView;
TextView notfound;
Dialog epicDialog;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_fragment_bestell, container, false);
recyclerView = view.findViewById(R.id.recyclerview_scroll);
notfound = view.findViewById(R.id.user_order_notfound);
getBestellungen();
epicDialog = new Dialog(getActivity());
return view;
}
public void callAction(int pId) {
System.out.println(pId);
epicDialog.setContentView(R.layout.user_popup_order_overview);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btn_order_overview_finish = (Button) epicDialog.findViewById(R.id.btn_order_overview_finish);
//System.out.println(bestellung.get(position).getBestellnummer());
btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
epicDialog.dismiss();
}
});
epicDialog.show();
}
}
最佳答案
documentation建议您不要在您的情况下直接实例化对话框,而是使用 AlterDialog
public void callAction(int pId){ // idk what you're doing with pid
new AlertDialog.Builder(getActivity())
.setMessage("message") //set the message on the dialog
.setView(R.layout.user_popup_order_overview) // set layout as view of this dialog
.setNegativeButton("cancel",null) // pressing cancel will dismiss this dialog
}).create().show() // create and show this dialog;
}
祝你有美好的一天
关于java - 如何在Fragment类中使用Dialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62486865/