开发者

Method not calling alert class

开发者 https://www.devze.com 2023-02-14 19:36 出处:网络
I have an Alert method set in the main class. Which is then called in an onclick within the main class like:

I have an Alert method set in the main class. Which is then called in an onclick within the main class like:

alertbox.show();

I am going to need a few alert dialogs to set validation within my app.

I have the alertdialog code as:

final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    alertDialog = new AlertDialog.Builder(this).create();
    alertbox.setTitle("Warning");
    alertbox.setMessage("Game May End");
    alertbox.setPositiveButton("CONTINUE",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    setContentView(webview);


                }
            });

    alertbox.setNegativeButton("CANCEL",
            new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

    alertbox.setIcon(R.drawable.icon);

having a few alert dialogs on top of what I already have in the main class makes the class too big. I would like to seperate the class into a mainvalidation class. Then call this alert dialog in the main class as

mainvalidation.alertbox.show();

Can someone please help me with this. Thanks

Edit:

I have tried putting it into another class and adding static. But it says only final is permitted alertbox shows an error when I use static:

        public class MainValidation extends Activity {

    static AlertDialog alertbox;
    static AlertDialog alertDialog; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final static AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            alertDialog = new AlertDialog.Builder(this).create();

        alertbox.setTitle("Warning");
        alertbox.setMessage("Game May End");
        alertbox.setPositiveButton("CONTINUE",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {



                    }
                });

        alertbox.setNegativeButton("CANCEL",
                new DialogInterface.OnClickListener() {

                   开发者_JAVA百科 @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

        alertDialog.setIcon(R.drawable.icon);


}
}

Please help, thanks


Use a class like DialogTest.

Show dialog : DialogTest.showDialog(ctx); // ctx is the context you are calling from.

Dismiss dialog : DialogTest.dismissDialog();

public class DialogTest
{
    private static AlertDialog  alertDialog;

    public static void showDialog(Context ctx)
    {
        final AlertDialog.Builder alertbox = new AlertDialog.Builder(ctx);
        alertbox.setTitle("Warning");
        alertbox.setMessage("Game May End");
        alertbox.setPositiveButton("CONTINUE", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {}
        });
        alertbox.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {}
        });
        alertDialog = alertbox.create();
        alertDialog.setIcon(R.drawable.icon);
        alertDialog.show();
    }

    public static void dismissDialog()
    {
        alertDialog.dismiss();
    }
}


You will just want to create a new class called mainvalidation and then declare your items as:

final static AlertDialog.Builder alertbox

Then you can call your method as you want.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号