Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中打开警报对话框_Android_Android Intent - Fatal编程技术网

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

如何在android中打开警报对话框,android,android-intent,Android,Android Intent,我想在成功提交数据后打开警报对话框。 我使用以下代码,但不工作 dialog = ProgressDialog.show(TanantDetails.this, "", "Please Wait...", true); new Thread(new Runnable() { public void run() { String response; S

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

     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

简单警报对话框

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
                       }
                   });
简单警报对话框

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
                       }
                   });
请尝试以下代码:

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();
请尝试以下代码:

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();

您的警报对话框需要显示在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(); 

}

});

}

您的警报对话框需要显示在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(); 

}

});

}

你说它不起作用是什么意思?发生了什么?祝酒会意味着什么?警报秀与否?你说它不起作用是什么意思?发生了什么?祝酒会意味着什么?是否显示警报?我正在使用此代码,但警报对话框无法显示,请帮助我使用此代码,但警报对话框无法显示,请帮助我