我想在警报对话框中显示确定和取消按钮?

use*_*660 10 android android-alertdialog

我想在我的警报对话框中显示确定和取消按钮.我尝试了很多解决方案,但没有成功.请指导我.感谢...谢谢

AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Check Your Internet Connection!! you are not connected to the Internet..");

            AlertDialog alert = builder.create();
                             alert.show();} 
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 47

AlertDialog.Builder adb = new AlertDialog.Builder(this);
adb.setView(alertDialogView);
adb.setTitle("Title of alert dialog");
adb.setIcon(android.R.drawable.ic_dialog_alert);
adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        EditText et = (EditText)alertDialogView.findViewById(R.id.EditText1);
        Toast.makeText(Tutoriel18_Android.this, et.getText(),
                Toast.LENGTH_SHORT).show();
    }
});
adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
});
adb.show();
Run Code Online (Sandbox Code Playgroud)


Mur*_*san 7

以下代码将使用一个按钮创建一个简单的警报对话框.在下面的代码setTitle()方法中用于设置Title to alert对话框.setMessage()用于将消息设置为警报对话框.setIcon()是将图标设置为警告对话框

    AlertDialog alertDialog = new AlertDialog.Builder(
                    AlertDialogActivity.this).create();

    // Setting Dialog Title
    alertDialog.setTitle("Alert Dialog");

    // Setting Dialog Message
    alertDialog.setMessage("Welcome to AndroidHive.info");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.drawable.tick);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            // Write your code here to execute after dialog closed
            Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
            }
    });

    // Showing Alert Message
    alertDialog.show();
Run Code Online (Sandbox Code Playgroud)


Shi*_*hiv 5

protected final Dialog onCreateDialog(final int id) {
    Dialog dialog = null;
    switch (id) {
    case DIALOG_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(
                "some message")
                .setCancelable(false)
                .setPositiveButton("ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {
                                //to perform on ok


                            }
                        })
                .setNegativeButton("cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int id) {

                                //dialog.cancel();
                            }
                        });
        AlertDialog alert = builder.create();
        dialog = alert;
        break;

    default:

    }
    return dialog;
}
Run Code Online (Sandbox Code Playgroud)

您可以从任何地方调用此方法,例如:

      showDialog(DIALOG_ID);
Run Code Online (Sandbox Code Playgroud)