2011-04-27 80 views
2

我有一個AlertDialog需要在幾個活動中使用。我如何用自己的類來完成這個任務?感謝您的幫助!〜!One AlertDialog,用於多項活動?

我嘗試了以下存取權限,並在它的活動寬:

Alerts.sdCardMissing(); 

我 '嘗試' 創建類:

public class Alerts { 

public static void sdCardMissing() { 
    AlertDialog alertDialog = new AlertDialog.Builder(null).create(); 
    alertDialog.setTitle("External Storage State"); 
    alertDialog 
      .setMessage("Your SD-Card is not mounted! If the device is plugged into a computer via the USB, please disconect the device."); 
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // this.finish(); 
     } 
    }); 
    // alertDialog.setIcon(R.drawable.icon); 
    alertDialog.show(); 
} 

}

回答

2

嘗試...

public class Alerts { 
    public static void sdCardMissing(Context context) { 
     // Pass context to AlertDialog.Builder 
     AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 
     ... 
    } 
} 

然後從Activit中調用它y ...

// Pass the Activity context as 'this' 
Alerts.sdCardMissing(this); 
+0

Thnx @MisterSquonk !!那樣做了。 – CelticParser 2011-04-27 01:24:29

+0

@camelCaser:很高興能有幫助。 – Squonk 2011-04-27 04:07:05

+0

你知道那些只要意識到答案就是這麼簡單就可以解決問題的日子嗎? <3 SOF – Jacksonkr 2016-02-05 13:54:36