1

我想显示一个带有确定和取消按钮的警报对话框,但我只得到取消。当我注释掉取消按钮时,我会得到确定按钮。奇怪的。无论如何,这是代码:

final AlertDialog ukucajIme = new AlertDialog.Builder(Kviz.this).create();
            ukucajIme.setTitle("Insert your name");
            final EditText input = new EditText(Kviz.this);

            ukucajIme.setView(input);

            ukucajIme.setButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                  Editable ukucanoIme = input.getText();
                  finish();
                  }
                });
            ukucajIme.setButton("Cancel", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int whichButton) {
                     ukucajIme.dismiss();
                     finish();
                  }
                  });
            ukucajIme.show();

第二个问题:每当我尝试使用 ukucajIme.setPositiveButton 之类的东西时,它都会给我一个错误并说将其更改为 setButton。这是为什么?

4

4 回答 4

5

尝试使用方法setPositiveButton()setNegativeButton()AlertDialogBu​​ilder 像这样

AlertDialog.Builder builder = new AlertDialog.Builder(Kviz.this);

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
    Editable ukucanoIme = input.getText();
    finish();
});


builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
         dialog.dismiss();
         finish();
         dialog.cancel();
    }
});

AlertDialog dialog = builder.create();
于 2013-03-07T18:29:37.673 回答
1

像这样使用。

使用两个按钮创建警报对话框。setPositiveButton()用于在警告对话框中创建一个肯定按钮,并setNegativeButton()用于调用否定按钮来警告对话框。

AlertDialog.Builder alertDialog = new AlertDialog.Builder(Kviz.this);
ukucajIme.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
       Editable ukucanoIme = input.getText();
       finish();
    }
});

// Setting Negative "Cancel" Button
ukucajIme.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        finish();
        dialog.cancel();
    }
});

ukucajIme.show();  // Showing Alert Message
于 2013-03-07T18:29:58.570 回答
1

警报对话框不包含 setNegativeButton 或 setPositiveButton。而是使用:

ukucajIme.setButton(AlertDialog.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
    Editable ukucanoIme = input.getText();
    finish();
});


ukucajIme.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
     ukucajIme.dismiss();
     finish();
});
于 2013-03-07T18:33:16.197 回答
0

好的,这是完整的代码。我只是改变了一些变量。

AlertDialog.Builder builder = new AlertDialog.Builder(Kviz.this);
                     builder.setTitle("Ukucaj svoje ime");
                     final EditText input = new EditText(Kviz.this);
                     builder.setView(input);

                     builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog,int which) {
                         Editable ukucanoIme = input.getText();
                         finish();
                     }
                     });


                     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog, int which) {
                              dialog.dismiss();
                              finish();
                         }
                     });
                     builder.show();
                     AlertDialog dialog = builder.create();
于 2013-03-07T20:09:34.263 回答