1

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

     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 显示消息响应成功。

我是安卓新手

4

3 回答 3

5

简单的警报对话框

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
                       }
                   });
于 2013-05-17T07:43:52.347 回答
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();
于 2013-05-17T07:47:52.997 回答
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(); 

}

});

}
于 2013-08-28T15:28:27.667 回答