简体   繁体   中英

ProgressDialog gets dismissed when button is pressed

I have a "please wait" ProgressDialog that includes a button, the problem is that when the button is clicked the dialog is dismissed (it disappears), how can I make the dialog stay on the screen ?

    // Please wait dialog
private void showWaitDialog() {
    prgDialg = new ProgressDialog(context);
    prgDialg.setTitle("Working...");
    prgDialg.setMessage("Please wait.");
    prgDialg.setCancelable(false);
    prgDialg.setButton("Do something", myListener);
    prgDialg.show();
}

// Click listener
public OnClickListener myListener = new OnClickListener(){

    @Override
    public void onClick(DialogInterface dialog, int which) {
        textViev1.setText("xxx");
    }

Try this

prgDialg.setButton("Do something", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {
            // Use either finish() or return() to either close the activity or just the dialog
            return;
        }
    });

prgDialg.setButton("Do something", myListener); the above method setButton(CharSequence text, DialogInterface.OnClickListener listener) is deprecated.

you can use

private void showWaitDialog() {
    prgDialg = new ProgressDialog(this);
    prgDialg.setTitle("Working...");
    prgDialg.setMessage("Please wait.");
    prgDialg.setCancelable(false);
    prgDialg.setButton(DialogInterface.BUTTON_NEUTRAL, "Do something", this);
    prgDialg.show();
}

@Override
public void cancel() {

}

@Override
public void dismiss() {

}
private void showWaitDialog() {
    prgDialg = new ProgressDialog(context);
    prgDialg.setTitle("Working...");
    prgDialg.setMessage("Please wait.");
    prgDialg.setCancelable(false);
    prgDialg.setNeutralButton("Do Something", new View.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
                textViev1.setText("xxx");
    }
    });
    prgDialg.show();
}

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