开发者

android Messagebox

开发者 https://www.devze.com 2023-04-05 08:40 出处:网络
android Messagebox doesn\'t show because of finish call, how to make this function wait for ok and then close

android Messagebox doesn't show because of finish call, how to make this function wait for ok and then close

public void msbox(String str,String str2)
{
    AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);                      
    dlgAlert.setMessage(str2);
    dlgAlert.setTitle(str);              
    dlgAlert.setPositiveButton("OK", null);
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();
    finish(); 
}

should be like this

public void msbox(String str,String str2)
{
    AlertDialog.Bu开发者_如何学JAVAilder dlgAlert  = new AlertDialog.Builder(this);                      
    dlgAlert.setTitle(str); 
    dlgAlert.setMessage(str2); 
    dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
             finish(); 
        }
   });
    dlgAlert.setCancelable(true);
    dlgAlert.create().show();
}


see SO question: AlertDialog doesn't wait for input

you will have to implement callback (OnClickListener) when user clicks OK on AlertDialog.

This all because Android dialog boxes are not modal (non-blocking invoker thread)

dlgAlert.setPositiveButton("OK",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // call your code here
    }
});


If you want to create a dialog box(Message Box,named in c#,vb.net etc) in android programming then just copy this code and paste it in the click event of any button where you need it.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder
    .setTitle("Deleting a Contact No")
    .setMessage("Are you sure?")
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setPositiveButton("Yes", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {       
               //do some thing here which you need
    }
    });             
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() 
    {
        public void onClick(DialogInterface dialog, int which) 
        {   
        dialog.dismiss();           
        }
    });         
AlertDialog alert = builder.create();
        alert.show();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号