簡體   English   中英

如何在另一個類中使用方法中的變量?

[英]How to use a variable from method in another class?

我的警報對話框有一些問題...如何調用alert.show(); 在第二類中調用警報對話框?

我需要顯示alertdialog onReceive方法,但我不能這樣做...

有人可以幫我嗎?

ps對不起,我的英語。

主類:

    public class Main extends Activity {

    ...

     public void onTimeSet(TimePicker view, int hour, int minute) {

     ...


                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("ALARM");
                builder.setMessage("Wstajesz czy dalej drzemiesz ?!");

                builder.setPositiveButton("Wstaje...", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        // Do do my action here

                        dialog.dismiss();
                    }

                });

                builder.setNegativeButton("Spie!", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // I do not need any action here you might
                        dialog.dismiss();
                    }
                });
                AlertDialog alert = builder.create();
                alert.show();
                ....
    }

二等艙:

    public class Second extends BroadcastReceiver {

        @Override
        public void onReceive(Context k1, Intent k2) {

         /* 
           -->  here i want to call an alert using: alert.show(); It's possible ?

         */
        }
}

您無法顯示來自ReceiverDialog ,需要Activity 可以使用Intent啟動MainActivity也可以根據自己的實際需要將其設置為帶有Dialog theme的單獨Activity 您可以通過創建一個Activity並在manifest使用主題進行聲明來實現此目的

android:theme="@android:style/Theme.Dialog"

請注意,從Receiver創建Intent以啟動Activity時,需要設置Intent.FLAG_ACTIVITY_NEW_TASK標志

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM