![](/img/trans.png)
[英]How to use getChildFragmentManager with a class that extends Dialog fragment
[英]How to use Dialog in Fragment class
我想讓我的自定義彈出窗口顯示在片段中。 不幸的是,Object 對話框沒有引用正確的布局。 我怎樣才能讓它指向正確的布局? 我希望有人可以向我解釋錯誤在哪里,以便我可以調用它。
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();
}
}
文檔建議您不應該在您的案例中直接實例化一個對話框,而是使用 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;
}
祝你有美好的一天
正如 Abhinav 建議的那樣,您不應該直接實例化 Dialog 。 創建一個擴展DialogFragment的 class 對我來說效果最好。 這樣,當我創建 class 的實例時,我只需要調用show()
方法。
Kotlin 中的示例:
class UnsavedChangesDialogFragment(private val listener: DialogInteractions) : DialogFragment() {
interface DialogInteractions {
fun onPositiveBtnPressed()
fun onNegativeBtnPressed()
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
builder
.setMessage(R.string.changes_dialog_title)
.setPositiveButton(R.string.changes_dialog_continue) { _, _ ->
listener.onPositiveBtnPressed()
}
.setNegativeButton(R.string.changes_dialog_back) {_, _ ->
listener.onNegativeBtnPressed()
dismiss()
}
builder.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
您可以通過添加界面輕松決定按下正面或負面按鈕后會發生什么。 這樣,在用戶與對話框交互后,將使用對話框的每個片段都可以執行不同的操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.