Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import android.view.KeyEvent;

public class Main {
    public static void createNonCancellableAcceptOrCancelDialog(Context context, String title, String message,
            String acceptButtonText, String cancelButtonText, final Runnable onAccept, final Runnable onCancel) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setCancelable(false);
        builder.setTitle(title);
        builder.setMessage(message);
        builder.setInverseBackgroundForced(true);
        builder.setPositiveButton(acceptButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (onAccept != null)
                    onAccept.run();
            }
        });

        builder.setNegativeButton(cancelButtonText, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (onCancel != null)
                    onCancel.run();
            }
        });
        AlertDialog alert = builder.create();
        alert.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
                    return true; // Pretend we processed it
                }
                return false; // Any other keys are still processed as normal
            }
        });
        alert.show();
    }
}