【问题标题】:why alertbox is closing after clicking ok button? [duplicate]为什么单击确定按钮后警报框正在关闭? [复制]
【发布时间】:2013-04-27 19:36:11
【问题描述】:
 @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();      
 } 


 }

我正在检查 setPositiveButton 中的条件。但在检查条件后,警报框会自动关闭。我希望在该过程完成之前显示警报框。我已经实现了很多东西,但找不到解决方案。

【问题讨论】:

标签: android android-alertdialog


【解决方案1】:

我尝试过一次,但无法找到解决方案,所以这就是我所做的。不要使用setPositiveButtonnegativeneutral 按钮,因为所有这些都默认关闭对话框。因此,既然您已经创建了LinearLayout lila1= new LinearLayout(this);,只需动态添加两个按钮并在它们上添加点击列表,当您完成逻辑调用 onBackPressed()

代码如下:

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

【讨论】:

    【解决方案2】:

    尝试以下方法:

    //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;
            }
        }
    });
    

    【讨论】:

    • 感谢您的努力。我无法在我的代码中实现这个 setOnShowListener ..
    • 没问题但是为什么不能使用setOnShowListener?
    • 因为 setOnShowListener 不支持它显示错误。
    • 你用的是哪个sdk版本?
    • 使用 setOnShowListener 通常应该没问题,因为它从 android 8 或更高版本开始受支持,但如果它仍然产生错误,您可以遵循以下线程的方法:stackoverflow.com/questions/4016313/…
    猜你喜欢
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多