简体   繁体   中英

How to add confirm box when i click on checkbox?

I have two dynamic check box one is for deleting video and another is for saving video.I want to add a confirm box so that it confirms my action when i click on checkbox.How to add the confirm box in my code?

deleteCheckBox.setOnClickListener(deleteRelatedThumbnail(deleteCheckBox));

 View.OnClickListener deleteRelatedThumbnail(final CheckBox checkBox) {

        return new View.OnClickListener() {
            public void onClick(View v) {
                int index = checkBox.getId();
                if (((CheckBox) v).isChecked() && deleteVideoFile(index - 1)){

                    // if(deleteVideoFile(index-1)){
                    bitMapsAvailable.remove(index - 1);
                    bitMapsFilePath.remove(index - 1);
                    Toast.makeText(MainActivity.this, "Selected video file is deleted successfully.", Toast.LENGTH_SHORT).show();
                    showThumbnails();
                }else{
                    Toast.makeText(MainActivity.this, "Not deleted", Toast.LENGTH_SHORT).show();
                }

            }
        };
    }





    saveCheckBox.setOnClickListener(saveRelatedThumbnail(saveCheckBox));
    View.OnClickListener saveRelatedThumbnail(final CheckBox checkBox) {

        return new View.OnClickListener() {
            public void onClick(View v) {
                int index = checkBox.getId()-31;
                if (((CheckBox) v).isChecked()){
                String src = bitMapsFilePath.get(index-1);
                String destination = mVideoFolder+"/"+new File(src).getName();

                File srcFile = new File(src);
                srcFile.renameTo(new File(destination));
                Toast.makeText(MainActivity.this, "Saved in "+destination, Toast.LENGTH_SHORT).show();
                bitMapsAvailable.clear();
                for(String filePath: bitMapsFilePath ) {
                    File file = new File(filePath);
                    file.delete();
                }
                bitMapsFilePath.clear();
                Toast.makeText(MainActivity.this, "Temporary videos are deleted successfully",Toast.LENGTH_SHORT).show();
                showThumbnails();
            }
        }
    };
    }

You can use alert dialog

    deleteCheckBox.setOnClickListener(deleteRelatedThumbnail(deleteCheckBox));

 View.OnClickListener deleteRelatedThumbnail(final CheckBox checkBox) {

        return new View.OnClickListener() {
            public void onClick(View v) {
                int index = checkBox.getId();
                if (((CheckBox) v).isChecked() && deleteVideoFile(index - 1)){
                    AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);// use you activity name
              builder.setMessage("Are you sure you want to delete? ")
                      .setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                      bitMapsAvailable.remove(index - 1);
                    bitMapsFilePath.remove(index - 1);
                    Toast.makeText(MainActivity.this, "Selected video file is deleted successfully.", Toast.LENGTH_SHORT).show();
                    showThumbnails();

                    }
                })

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

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
             Toast.makeText(MainActivity.this, "Not deleted", Toast.LENGTH_SHORT).show();

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

                    // if(deleteVideoFile(index-1)){

                }else{
                    Toast.makeText(MainActivity.this, "Not deleted", Toast.LENGTH_SHORT).show();
                }

            }
        };
    }

Just user alertdialog for it:

private void showLocationDialog() {  

AlertDialog.Builder builder = new AlertDialog.Builder(yOURActivity.this);
builder.setTitle(getString(R.string.dialog_title));
builder.setMessage(getString(R.string.dialog_message));

String positiveText = getString(android.R.string.ok);
builder.setPositiveButton(positiveText,
        new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // positive button logic
    }
});

String negativeText = getString(android.R.string.cancel);
builder.setNegativeButton(negativeText,
        new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // negative button logic
    }
});

AlertDialog dialog = builder.create();
// display dialog
dialog.show();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM