搜尋此網誌

2015年9月22日 星期二

【Android】How to create a common class for alertDialog Box

ref:http://stackoverflow.com/a/8585364

Create a class like CustomDialog.class,
and inside of it create a static method like the one below,
 public static void ExitAppDialog(final Activity activity)
 {
     AlertDialog.Builder alertbox = new AlertDialog.Builder(activity);
     alertbox.setTitle("Warning");
     alertbox.setMessage("Exit Application?");
     alertbox.setPositiveButton("Yes", new
     DialogInterface.OnClickListener() {
     public void onClick(DialogInterface arg0, int arg1) {
         activity.finish();
     }
     });
     alertbox.setNegativeButton("No", new
     DialogInterface.OnClickListener() {
     public void onClick(DialogInterface arg0, int arg1) {

     }
     });
     alertbox.show();
 }
And in your activity's onBackPressed() call this method like this,
public void onBackPressed()
{
    CustomDialog.ExitAppDialog(myActivity.this );
}

沒有留言: