2013-02-22 32 views
5

Tôi muốn hiển thị ok và nút hủy trong dialog.I cảnh báo của tôi đã cố gắng rất nhiều giải pháp nhưng didnt succeede..guide tôi plese..thanksTôi muốn hiển thị nút OK và hủy trong hộp thoại cảnh báo của mình?

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();} 
+2

Ít nỗ lực tìm kiếm. http://developer.android.com/guide/topics/ui/dialogs.html – user370305

+1

OMG câu hỏi là gì ??? – duggu

+0

@HCD Tôi cũng có câu hỏi khó .. bạn có muốn trả lời không ?? – user2033660

Trả lời

38
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(); 
+0

Nicolas> Bạn thay đổi màu nền của hộp thoại này như thế nào? Hay một cách đơn giản khác để làm cho nó trông đẹp hơn một chút? – Jasper

+0

Sự kiện * phủ định * cũng có thể được đặt thành 'null': http://stackoverflow.com/a/25504486/756976 – skofgar

6

Các mã sau sẽ tạo ra một hộp thoại cảnh báo đơn giản với một nút. Trong các mã sau đây setTitle() phương pháp được sử dụng cho thiết lập Tiêu đề để cảnh báo hộp thoại. setMessage() được sử dụng để đặt hộp thoại thông báo thành cảnh báo. setIcon() là để thiết lập biểu tượng để cảnh báo thoại

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

bạn có thể gọi đây là từ bất cứ nơi nào như:

 showDialog(DIALOG_ID); 
+0

bạn đang gán đối tượng siêu lớp cho lớp con' dialog = alert; 'wont nó thông qua ngoại lệ? – user3207655

Các vấn đề liên quan