简体   繁体   中英

Android alert dialog Issue

In my android application, I use one alert dialog to display some information to the user, and if the user click the dialog , it should finish the activity. My code is

offer.this.runOnUiThread(new Runnable() {
@Override
public void run() {
    // TODO Auto-generated method stub
    AlertDialog alert=new AlertDialog.Builder(offer.this).create();
    alert.setTitle("SVSugar Mill");
    alert.setMessage("Offer Number is "+offer_no.getText().toString());
    alert.setButton("Click to Dismiss", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        finish();
        //return;
        }
    });
    alert.show();
    }
});

It doesn't wait for the user response to finish(). Instead it will be called even if the user didn't click the Alert dialog. I know this is asynchronous, but I need to do this.(The OfferNO should be displayed to the user. When the user click the alert dialog it should finish the activity). Is there any way to do this?

Someone help me

Edit:

The activity will be finished without waiting for the user to click the alert dialog

public void ShowDialog(final Context context) {
        new AlertDialog.Builder(context)
                .setTitle(android.R.string.dialog_alert_title)  
                .setMessage(UContext.getContext().getString(R.string.network_error))
                .setPositiveButton(android.R.string.ok,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                this.finish();
                            }
                        }).show();
    }

The problem is that you created two AlertDialog instances here:

alertDialog=builder.create();
builder.create().show();

Then you called dismiss() on the dialog that is not actually shown. This should fix the problem:

alertDialog=builder.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