2013-02-22 4 views
5

私はdialog.Iは多くのソリューションを試みたが、私は次のコードを持つ単純な警告ダイアログを作成しますアラートダイアログで[OK]とキャンセルボタンを表示したいですか?

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

少ない労力: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(); 
user370305

+1

OMGどのような質問??? – duggu

+0

@HCD私もタフな質問があります。あなたは答えたいですか? – user2033660

答えて

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

+0

* negativeイベントは 'null'に設定することもできます:http://stackoverflow.com/a/25504486/756976 – skofgar

6

をplese..thanks succeede..guide didntの[OK]を表示し、私の警告にボタンをキャンセルしたいです1つのボタン。次のコードでは、setTitle()メソッドは、タイトルを警告ダイアログに設定するために使用されます。 setMessage()は、警告ダイアログへのメッセージを設定するために使用されます。検索の

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

ダイアログを警告するアイコンを設定することで、あなたはどこにでもなどからこれを呼び出すことができます。 http://developer.android.com/guide/topics/ui/dialogs.html
+0

サブクラス' dialog = alert; 'にスーパークラスオブジェクトを代入していますが、例外はありませんか? – user3207655

関連する問題