2012-03-07 78 views
2

我希望用戶通過向他們顯示與消息以及「是」或「否」按鈕的對話來確認動作。我如何使它顯示,並根據他們選擇的按鈕執行操作?你如何在android中提示用戶類似toast的消息?

謝謝,AlertDialog看起來像我在找什麼。但是,有一個錯誤,它表示"AlertDialog.Builder(this);"告訴我,"The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined" -

+2

顯然......'System.err.println(「你喜歡烤麪包嗎?」);':-) – 2012-03-07 01:27:54

回答

3

你在找什麼是AlertDialog。使用示例here,您可以輕鬆創建一個是/否對話框,看起來會沿着線:您創建一個新的AlertDialog.Builder

enter image description here

1

,它傳遞一些參數,最後調用它create()方法和分配返回值爲AlertDialog對象以保存對其的引用。

AlertDialog.Builder adb = new AlertDialog.Builder(this); 
     adb.setTitle("Question"); 
     adb.setMessage(Html.fromHtml("Visit Stackoverflow?")); 
     adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // Action for YES 
       startActivity(
         new Intent(
           Intent.ACTION_VIEW, 
           Uri.parse("http://www.stackoverflow.com"))); 
       return; 
      } 
     }); 

     adb.setNegativeButton("No", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // Action for NO 
       return; 
      } 
     }); 

AlertDialog myDialog = adb.create(); 
4

如圖here

private static final int DIALOG = 1; 

到顯示對話框呼叫

showDialog(DIALOG); 

倍率onCreateDialog,檢查與用於對話ID的開關並插入

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setMessage("Are you sure about this?") 
    .setCancelable(false) 
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      // whatever if YES 
     } 
    }) 
    .setNegativeButton("No", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int id) { 
      // whetever if NO 
     } 
    }); 
AlertDialog alert = builder.create(); 
1

這很簡單如下所示

new AlertDialog.Builder(this) 
      .setTitle("Info") 
      .setMessage("hello") 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface arg0, int arg1) { 
        // Some stuff to do when ok got clicked     
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface arg0, int arg1) { 
        // Some stuff to do when ok got clicked 
       } 
      }) 
      .show(); 
相關問題