2013-02-22 84 views
5

我要显示OK,在我的警报dialog.I取消按钮尝试了多种解决方案,但没有succeede..guide我plese..thanks我想在我的警报对话框中显示确定并取消按钮?

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

事半功倍。 http://developer.android.com/guide/topics/ui/dialogs.html – user370305 2013-02-22 09:33:19

+1

OMG有什么问题? – duggu 2013-02-22 09:41:52

+0

@HCD我也有棘手的问题。你想回答吗? – user2033660 2013-02-22 09:51:32

回答

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>你会如何改变这个对话框的背景颜色?或者其他简单的方法使它看起来有点漂亮? – Jasper 2015-09-29 08:59:11

+0

* negative *事件也可以设置为'null':http://stackoverflow.com/a/25504486/756976 – skofgar 2015-12-16 18:04:57

6

下面的代码将创建一个简单的警告对话框一个按钮。在下面的代码中使用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(); 
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; 
} 

你可以从任何地方像称之为:搜索

 showDialog(DIALOG_ID); 
+0

您正在将超类对象分配给子类'dialog = alert;'不会通过异常吗? – user3207655 2015-07-15 11:39:05

相关问题