2014-02-10 15 views
4

喜的朋友我設定是一個對話框,沒有按鈕,但只有NO是可見的是按鈕是不可見的,請建議我'是'按鈕不顯示在Android應用程序的確認框中?

這是我事先用感謝

 alertDialog.setButton("YES", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog,int which) { 

       // Write your code here to invoke YES event 
       Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      // Setting Negative "NO" Button 
    alertDialog.setButton("NO bad", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int which) { 
       // Write your code here to invoke NO event 
       Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); 
       dialog.cancel(); 
       } 
      }); 
+0

如果這實際上是一個AlertDialog,請使用正負按鈕。 – CommonsWare

+0

它顯示錯誤,如果我使用正面和負面 – Vji

+1

它顯示什麼錯誤? – Piyush

回答

3

我THI你應該遵循下面給出的這個Builder模式的例子。

AlertDialog.Builder builder = new AlertDialog.Builder(context) 
     .setCancelable(true) 
     .setTitle(titleResourceId) 
     .setMessage(messageResourceId) 
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Toast.makeToast(mContext, "Yes", Toast.LENGTH_SHORT).show(); 
       dialog.dismiss(); 
      } 
     }).setNegativeButton("No", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Toast.makeToast(mContext, "No", Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    builder.show(); 

使用setPositiveButton()setNegativeButton()允許Android根據應用程序在運行平臺上放置的按鈕按正確的順序。

+1

是的,我非常感謝你... – Vji

2

,而不是

代碼
alertDialog.setButton 

使用

alertDialog.setPositiveButton 
alertDialog.setNegativeButton 
1

而不是

setButton() 

使用setPositiveButton()爲「是」, setNegativeButton()爲「否」

相關問題