Один AlertDialog, используемый в нескольких действиях?

#android #android-alertdialog

#Android #android-alertdialog

Вопрос:

У меня есть 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();
}
  

}

Ответ №1:

Попробуйте…

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

Затем вызовите его из действия с помощью…

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

Комментарии:

1. Спасибо @MisterSquonk!! Это сделало это.

2. @camelCaser: Рад быть полезным.

3. Вы знаете те дни, когда вы слишком много думаете о проблеме только для того, чтобы понять, что ответ так прост? <3 SOF

4. @Jacksonkr : О … У меня довольно часто бывают такие дни. 😀