繁体   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