why alertbox is closing after clicking ok button?

2019-06-13 19:29发布

This question already has an answer here:

 @Override
 protected Dialog onCreateDialog(int id)
    {

    final AlertDialog.Builder alert = new AlertDialog.Builder(this);    
    alert.setTitle("Test");
    alert.setIcon(R.drawable.logo1);
    alert.setMessage("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Nam cursus.");
    LinearLayout lila1= new LinearLayout(this);
    lila1.setOrientation(1); //1 is for vertical orientation
    final EditText input = new EditText(this); 
    final EditText input1 = new EditText(this);
    input.setId(0);
    input1.setId(1);
    input.setHint("Enter your email");
    input1.setHint("Enter your message");
    input1.setHeight(200);
    lila1.addView(input);
    lila1.addView(input1);
    alert.setView(lila1);

    alert.setPositiveButton("Send", new DialogInterface.OnClickListener() 
    {   
       public void onClick(DialogInterface dialog, int whichButton) 
       {              
                String value = input.getText().toString().trim();  
                String value1 = input1.getText().toString().trim();  

                int emailIn= input.getText().toString().length();
                int message=input1.getText().toString().length();

              if( emailIn == 0 || message==0 )
              {   
                if(emailIn==0)
                {
                input.setError("Name is Required"); 
               //DialogBox should not close.
                }
                if(message==0)  
                {
                input1.setError("Description is Required");
                           //DialogBox should not close.
                }

              } 
             else
            {
         // Toast.makeText(getApplicationContext(), value, Toast.LENGTH_SHORT).show();   
               // Toast.makeText(getApplicationContext(), value1, Toast.LENGTH_SHORT).show(); 

            Intent email = new Intent(Intent.ACTION_SEND);
            email.putExtra(Intent.EXTRA_EMAIL, new String[]{getResources().getString(R.string.toEmail)});         
            email.putExtra(Intent.EXTRA_SUBJECT, "Test");
            email.putExtra(Intent.EXTRA_TEXT, value +  value1);
            email.setType("message/rfc822");
            startActivity(Intent.createChooser(email, "Choose an Email:"));

            }
            }});  
        alert.setNegativeButton("Cancel",        
                new DialogInterface.OnClickListener()
        {                           
            public void onClick(DialogInterface dialog, int whichButton) {          
                dialog.cancel();   
        }});         
    return alert.create();      
 } 


 }

I am checking condition in setPositiveButton. But after it checks the condition the alert box is closed automatically. I want the alertbox to show until the process is completed. I have implemented lot of things, but am unable to find the solution.

2条回答
来,给爷笑一个
2楼-- · 2019-06-13 20:12

I tried it to once to do it but was not able to find a solution , so here is what i did. Donot use the setPositiveButton, or negative or neutral button because all of these bydefault close the dialog box. So since you hav already created LinearLayout lila1= new LinearLayout(this); just add two buttons dynamically and add click listner on them and when your done with your logic call the onBackPressed()

Here is the code:

LinearLayout lila1= new LinearLayout(this);
    LinearLayout linbuttons = new LinearLayout(this);
    linbuttons.setOrientation(LinearLayout.HORIZONTAL);
    Button btnPositive = new Button(this);
    Button btnNegative = new Button(this);
    btnPositive.setText("Send");
    btnPositive.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // write your code for sending
            onBackPressed();
        }
    });
    btnNegative.setText("Cancel");
    btnNegative.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });
    linbuttons.addView(btnPositive);
    linbuttons.addView(btnNegative);
    lila1.addView(linbuttons);
查看更多
Explosion°爆炸
3楼-- · 2019-06-13 20:15

Try the following:

//Create the alert with builder the following way
alert.setPositiveButton("Send", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {}
});
this.alert = alert.create();

this.alertReady = false;

/*
 * Add an OnShowListener to change the OnClickListener on the
 * first time the alert is shown. Calling getButton() to get the positive button
 * before the alert is shown will return null. Then using a regular
 * View.OnClickListener will not dismiss the AlertDialog
 * after it has been called which should do the trick.
 */
alert.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        if (alertReady == false) {
            Button button = alert.getButton(DialogInterface.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //do your controls here
                    String value = input.getText().toString().trim();  
                    String value1 = input1.getText().toString().trim();  

                    int emailIn= input.getText().toString().length();
                    int message=input1.getText().toString().length();


                    if( emailIn == 0 || message==0 )
                    {   
                       if(emailIn==0)
                       {
                          input.setError("Name is Required"); 

                       }
                      if(message==0)   
                      {
                         input1.setError("Description is Required");
                         //DialogBox should not close.

                      }
                   }
            });
            alertReady = true;
        }
    }
});
查看更多
登录 后发表回答