2012-04-24 41 views
0

我在這個類文件中構建了一個AlertDialog,並且對話框很好,但是當執行單擊操作時,這些方法似乎不會被調用。我究竟做錯了什麼?謝謝你的幫助!Android對話框OnClick方法不起作用?

//AlertView Builder 
    final AlertDialog.Builder builder; 
    builder = new AlertDialog.Builder(this); 
    builder.setTitle(" "); 
    builder.setIcon(R.drawable.banner); 
    builder.setMessage("Share this picture using any of the below!"); 

    builder.setPositiveButton("Email", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        System.out.print("Emailing..."); 
       } 
      }); 

    builder.setNeutralButton("Facebook", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        System.out.print("Facebooking..."); 
       } 
      }); 

    builder.setNegativeButton("Twitter", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        System.out.print("Tweeting..."); 
       } 
      }); 
+0

告訴我,從你在哪裏調用此方法的代碼?並且在方法聲明的地方? – 2012-04-24 04:07:08

+0

現在的方法只是system.out.println語句,但他們永遠不會執行 – rjfellman 2012-04-24 04:08:27

+0

是的。嘗試使用JRaymond下面給出的陳述。 – 2012-04-24 04:11:02

回答

1

你期待它做什麼?你有沒有現在沒有的那些塊的代碼?

的Android默認情況下不使用System.out.print();任何東西 - 用日誌:

Log.d(getClass().getName(),"Facebooking..."); 
+0

對不起,我一直在使用System.out.println,它已經顯示在logcat中,但不在這裏。你的改變雖然工作。我想知道爲什麼它不會在這裏的工作... – rjfellman 2012-04-24 16:50:39

+0

@ russ216我認爲有一定的上下文,它將工作,在Android團隊已經管道system.out logcat - 但對話實際上是一個單獨的活動與一個單獨的窗口,所以它可能沒有相同的訪問類型,所以更好地習慣使用他們的日誌系統 - 無論如何,控制是更好的。 – JRaymond 2012-04-24 16:56:38

0

嘗試下面的代碼爲setNeutralButtonsetPositiveButtonsetNegativeButton不針對特定API的工作。

//AlertView Builder 
    final AlertDialog.Builder builder; 
    builder = new AlertDialog.Builder(this); 
    builder.setTitle(" "); 
    builder.setIcon(R.drawable.banner); 
    builder.setMessage("Share this picture using any of the below!"); 

    builder.setButton1("Email", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        System.out.print("Emailing..."); 
       } 
      }); 

    builder.setButton2("Facebook", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        System.out.print("Facebooking..."); 
       } 
      }); 

    builder.setButton3("Twitter", 
      new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        System.out.print("Tweeting..."); 
       } 
      }); 

希望它可以幫助...