如何在android中打开警告对话框

时间:2013-05-17 07:35:14

标签: android android-intent

我想在成功提交数据后打开警告对话框。 我正在使用以下代码但不起作用。

     dialog = ProgressDialog.show(TanantDetails.this, "", "Please Wait...", true);
                new Thread(new Runnable() {
                       public void run() {
                String response;

             SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences sp=getSharedPreferences("login",MODE_WORLD_READABLE);
            try {
                 Utility utility=new Utility();
                // 

                 new_url=url+mobile_no.getText().toString();
                response = utility.getResponse(utility.urlEncode(new_url));
                dialog.dismiss();
                if (response.equals("Success"))
                {

                     AlertDialog alertbox = new AlertDialog.Builder(getBaseContext())
                        //.setIcon(R.drawable.no)
                        .setTitle("Submit successfully")
                        .setMessage("“Police will verify documents between preferred timing")
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {

                            // do something when the button is clicked
                            public void onClick(DialogInterface arg0, int arg1) {
                               TanantDetails.this.finish();
                                Intent i=new Intent(getApplicationContext(),MainActivity.class);
                                i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                                startActivity(i);

                            }
                        })
                          .show(); 


                }

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
                       }
                 }).start();       


        }  

toast显示消息响应成功。

我是android的新手

3 个答案:

答案 0 :(得分:4)

简单警报对话

AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Alert")
                   .setTitle("Warning");

AlertDialog alert =builder.create();
                alert.show();

如果您想添加确定,请取消按钮,然后添加

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           // User clicked OK button
                    }
                   });

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           // User clicked cancel button
                       }
                   });

答案 1 :(得分:1)

试试这段代码:

new AlertDialog.Builder(this)
.setTitle("Your title")
.setMessage("Your message")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // Your code
    }
 })
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) { 
        // do nothing
    }
 })
 .show();

答案 2 :(得分:0)

您的警告对话框需要显示在UI线程上。您运行的代码位于单独的线程上。在大多数情况下,当您想要在单独的线程中更新UI元素时,可以使用runOnUiThread()来完成 请参阅以下代码

if (response.equals("Success"))

{

runOnUiThread(new Runnable() {
@Override

public void run() {

AlertDialog alertbox = new AlertDialog.Builder(getBaseContext())

//.setIcon(R.drawable.no)

.setTitle("Submit successfully")

.setMessage("“Police will verify documents between preferred timing")

.setPositiveButton("ok", new DialogInterface.OnClickListener() {


// do something when the button is clicked

public void onClick(DialogInterface arg0, int arg1) {

TanantDetails.this.finish();

Intent i=new Intent(getApplicationContext(),MainActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

startActivity(i);

}

}).show(); 

}

});

}