AlertDialog ...没有OK按钮,只有取消

时间:2013-03-07 18:26:56

标签: java android

我想用OK和Cancel按钮显示一个警告对话框,但我只收到取消。当我注释掉取消按钮我然后得到确定按钮。奇怪的。无论如何,这是代码:

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 个答案:

答案 0 :(得分:5)

尝试使用AlertDialogBu​​ilder上的方法setPositiveButton()setNegativeButton()

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();

答案 1 :(得分: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

答案 2 :(得分: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();
});

答案 3 :(得分: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();