简体   繁体   English

Android 警报对话框问题

[英]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.在我的 android 应用程序中,我使用一个警报对话框向用户显示一些信息,如果用户单击对话框,它应该完成活动。 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).我知道这是异步的,但我需要这样做。(应向用户显示 OfferNO。当用户单击警报对话框时,它应该完成活动)。 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 实例:

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

Then you called dismiss() on the dialog that is not actually shown.然后你在实际上没有显示的对话框上调用了dismiss()。 This should fix the problem:这应该可以解决问题:

alertDialog=builder.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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