2014-09-02 44 views
-1

我已經使用AlertDialog,當點擊按鈕它將轉到主屏幕。我沒有得到警戒框?意外的行爲onclick

alertDialog.setPositiveButton(R.id.button1, new 
    DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 
      Toast.makeText(getActivity(), "save me**********************", Toast.LENGTH_SHORT).show(); 
      Toast.makeText(getActivity(), "you have pressed save", Toast.LENGTH_SHORT).show(); 

     } 
    }); 

alertDialog.setNegativeButton("cancel", new DialogInterface.OnClickListener() { 
+1

請發佈完整的代碼,並解釋正確。 – 2014-09-02 06:38:55

+1

您是否爲'AlertDialog'調用'show()'方法? – Piyush 2014-09-02 06:45:02

+0

我認爲可能我alertDialog.show()不叫你 – 2014-09-02 06:47:35

回答

0

試試這個

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this); 
alertDialogBuilder.setTitle(Your Title); 

alertDialogBuilder.setMessage(Your Message); 

// set positive button: Yes message 

alertDialogBuilder.setPositiveButton(R.id.button1,new DialogInterface.OnClickListener() { 

    public void onClick(DialogInterface dialog,int id) { 

     //Your Code Here 
    } 
}); 

// set negative button: No message 

alertDialogBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() { 

    public void onClick(DialogInterface dialog,int id) { 

     // cancel the alert box and put a Toast to the user 
     dialog.cancel(); 

     //Your Code Here 

    } 

}); 
AlertDialog alertDialog = alertDialogBuilder.create(); 

// show alert 

alertDialog.show(); 
+0

嘗試通過上下文而不是傳遞性 – 2015-03-26 12:46:20