简体   繁体   中英

AlertDialog Extern - all fine but not returning the value as I wish

I'm trying to make a extern class for AlertDialog. I want to have an universal class to use it quickly.
I know the code isn't difficult at all, but there are anyhow many rows to write (or copy) and if I would find a mistake I maybe had to change many code...
I've everything but one thing I don't get.
So it works but returning the correct onClick doesn't work.
I've also tried to make an while loop before returning, but then the app is hanging....
Has somebody any idea?

public class RalaAlertDialog{
private static AlertDialog.Builder alertDialog;
private static long onClick=RalaInterfaceDefault.FehlerSpezialZahl;

//neutralButton
public static long AlertDialogNeutral(Context class_this, String mssg, String ntrlBttnTxt, boolean dismissable, String title){
    onClick=RalaInterfaceDefault.FehlerSpezialZahl; //default error number
    alertDialog=new AlertDialog.Builder(class_this);
    if(mssg.equals("")){
        mssg="DEFAULT-TEXT";
    }
    if(title.equals("")){
        title="DEFAULT-TITLE";
    }
    if(ntrlBttnTxt.equalsIgnoreCase("")){
        System.out.println("No values set - default in use.");
        ntrlBttnTxt="OK";
    }
    alertDialog.setMessage(mssg)
    .setCancelable(dismissable);
    alertDialog.setTitle(title);

    alertDialog.setPositiveButton(ntrlBttnTxt,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id){
                    onClick=0;
                    dialog.dismiss();
                }
            }
    );

    AlertDialog a=alertDialog.create();
    a.show();
    //wait until button is click before continuing
    return onClick;
}
public static AlertDialog getAlertDialog(Context ctx, String title, String message, String posButton, boolean dismissable, final DialogInterface.OnClickListener ocl) {

    AlertDialog.Builder builder =new AlertDialog.Builder(ctx);
    builder.setTitle(title)
        .setMessage(message)
        .setCancelable(dismissable)
        .setPositiveButton(posButton,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id){
                    dialog.dismiss();
                    if(ocl!=null) ocl.onClick(dialog, id);
                }
            });

    AlertDialog dialog = builder.create();
    return dialog;
}

Use it like this :

    AlertDialog dialog = getAlertDialog(this,"Hello","World","OK",false,new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Log.i("DIALOG","OK Clicked");
        }
    });
    dialog.show();

Of course you need only one OnClickListener, but I like it better that way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM