带复选框的警报对话框

AlertDialog with CheckBox

在我的应用程序中,我有一个显示 AlertDialog 的按钮。 AlertDialog 有一个复选框。现在,如果复选框被选中,我必须调用一个函数,如果没有被选中,我必须调用一个不同的函数。

        AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialogTheme);

        builder.setTitle("Delete Completed Tasks:");
        View view = getLayoutInflater().inflate(R.layout.del_completed,null);

        builder.setView(view);

        builder.setPositiveButton("Delete",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        CheckBox cb = findViewById(R.id.checkbox);

                        if(cb.isChecked()){
                            helper.delCompAndRec();
                        }
                        else helper.delCompleted();
                    }
                });

        builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        AlertDialog alert = builder.create();
        alert.show();
    }

问题是我总是将 cb.isChecked() 值设为 False,即使它已被选中。我该如何解决?

        AlertDialog.Builder builder = new AlertDialog.Builder(this,R.style.AlertDialogTheme);

        builder.setTitle("Delete Completed Tasks:");
        View view = getLayoutInflater().inflate(R.layout.del_completed,null);
        final CheckBox cb = view.findViewById(R.id.checkbox); //or declare as global variable
        builder.setView(view);

        builder.setPositiveButton("Delete",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        
                        if(cb.isChecked()){
                            helper.delCompAndRec();
                        }
                        else helper.delCompleted();
                    }
                });

        builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });

        AlertDialog alert = builder.create();
        alert.show();
    }

您需要在设计中定义复选框xml。然后您可以通过 view.findViewById(R.id.checkbox).

访问该复选框
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat);
    builder.setTitle("Delete completed task");
    
    View view = getLayoutInflater().inflate(R.layout.del_completed, null);
    final CheckBox cb = view.findViewById(R.id.checkbox); //like defined here
    builder.setView(view);

    builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {                        
                    if(cb.isChecked()){
                        helper.delCompAndRec();
                    }
                    else {
                        helper.delCompleted();
                     }
                }
            });

    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}

在xml中这样声明

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_margin="30dp"/>

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/image"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="20dp"
        android:textSize="25sp"
        android:text="Check this for test"/>

</androidx.constraintlayout.widget.ConstraintLayout>