DialogFragment为空,即使提供了xml

问题描述 投票:-1回答:1

我正在尝试使用DialogFragment创建一个弹出窗口,但是我的对话框只包含标题,负面和正面按钮。

Layout_selection_dialog

<Button
        android:text="Aalborg"
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:id="@+id/firstCityBtn"
        app:layout_constraintStart_toStartOf="parent"/>
<Button
        android:text="Aarhus"
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:id="@+id/thirdCityBtn"
        app:layout_constraintBottom_toBottomOf="parent"/>
<Button
        android:text="København"
        android:layout_width="200dp"
        android:layout_height="wrap_content" android:id="@+id/secondCityBtn"
        android:layout_marginTop="8dp"/>

SelectionDialog

public class SelectionDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.layout_selection_dialog, null);

        builder.setTitle("Vælg by").setNegativeButton("Back", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        return builder.create();
    }
}

在MainActivity中调用的函数

fun showCitySelection() {
   var dialog: CitySelectionDialog = CitySelectionDialog()
   var ft: FragmentTransaction = supportFragmentManager.beginTransaction()
    dialog.show(ft, null)
}

任何想法为什么我没有在对话框中获取我的按钮?

java android kotlin android-dialogfragment
1个回答
0
投票

您已创建View但未将其设置为对话框。加

builder.setView(view);
© www.soinside.com 2019 - 2024. All rights reserved.