【问题标题】:Closing a custom alert dialog on button click单击按钮时关闭自定义警报对话框
【发布时间】:2011-08-08 11:18:19
【问题描述】:

我无法关闭警报对话框。我正在使用布局充气机来制作对话框,所以我不确定在完成后我将如何关闭它。代码如下:

AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
DialogInterface dia = new DialogInterface();

//Create a custom layout for the dialog box
LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);

TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
Button add_item = (Button)layout.findViewById(R.id.add_button);

add_item.setOnClickListener(new OnClickListener()
{
        @Override
        public void onClick(View v)
        {
        //Need this to close the alert dialog box
        }
});

title.setText(workout_items[position]);
dialog.setView(layout);
dialog.show();

我无法调用完成,因为这会关闭启动警报对话框的列表视图,并且我无法使用 dialog.dismiss 调用。

你认为我应该怎么做才能解决这个问题?

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:
    AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
    DialogInterface dia = new DialogInterface();
    
    //Create a custom layout for the dialog box
    LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);
    
    TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
    Button add_item = (Button)layout.findViewById(R.id.add_button);
    
    title.setText(workout_items[position]);
    dialog.setView(layout);
    
    AlertDialog alertDialog = dialog.create();
    
    add_item.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            alertDialog.dismiss();
        }
    });
    
    
    alertDialog.show();
    

    【讨论】:

    • 变量 alertDialog 从内部类中访问。需要声明为最终版本。
    • DialogInterface dia 是干什么用的?
    【解决方案2】:

    试试这个:

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
        LayoutInflater inflater = getLayoutInflater();
        View dialogView = inflater.inflate(R.layout.brush_opts_dialog,null);
    
        builder.setView(dialogView);
    
    
        closeBtn = (Button)dialogView.findViewById(R.id.close_btn);
    
    
       final AlertDialog dialog = builder.create();
    
        closeBtn .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    
        dialog.show();
    

    您应该使用 AlertDialog.Builder 来“构建”警报对话框本身。然后,调用 AlertDialog.Builder 对象的 create() 方法。这个方法返回一个 AlertDialog 对象,它允许你调用dismiss()。

    您不必多次创建它,也不必使用任何 DialogInterface 对象。这对我有用。让我知道你的情况。

    【讨论】:

    • 您调用 create() 的解释正是我想要的。
    【解决方案3】:

    这样做,

    add_item.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                            dialog.dismiss();
                    }
                });
    

    【讨论】:

      【解决方案4】:

      我已经修改了你的代码请检查..

         AlertDialog.Builder dialog = new AlertDialog.Builder(AddData.this);
          DialogInterface dia = new DialogInterface();
      
          //Create a custom layout for the dialog box
          LayoutInflater inflater = (LayoutInflater)AddData.this.getSystemService(LAYOUT_INFLATER_SERVICE);
          View layout = inflater.inflate(R.layout.add_rep_1_set, parent, false);
      
          TextView title = (TextView)layout.findViewById(R.id.rep_1_title);
          Button add_item = (Button)layout.findViewById(R.id.add_button);
      
      
      
          final AlertDialog Dial = alertDialog.create();
          title.setText(workout_items[position]);
          Dial.setView(layout);
      
          AlertDialog alertDialog = dialog.create();
      
          add_item.setOnClickListener(new OnClickListener()
          {
              @Override
              public void onClick(View v)
              {
                  Dial.dismiss();
              }
          });
      
      
          Dial.show();
      

      刚刚添加

      final AlertDialog Dial = alertDialog.create(); 和改变 dialog.setView(layout);Dial.setView(布局);

      现在只需在 onclick 监听器中调用 Dial.dismiss();.. 对我来说很好。

      【讨论】:

      • @Abdus.. 为什么你调用 create();两次?
      【解决方案5】:

      在您的班级中使用此代码:

      Dialog dialog = new Dialog(context);
      dialog.setContentView(R.layout.custom);
      ((Button) dialog.findViewById(R.id.button))
         .setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              dialog.dismiss();
          }
      });
      dialog.show();
      

      并创建custom.xml,然后将这段代码粘贴到其中:

      <?xml version="1.0" encoding="utf-8"?>
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
          <Button
              android:id="@+id/button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text=" Ok "/>
      </RelativeLayout>
      

      来源:https://www.mkyong.com/android/android-custom-dialog-example/

      如果您不喜欢上面的代码,请查看以下链接: http://thedeveloperworldisyours.com/android/prevent-alertdialog-closing-button-clicked/

      【讨论】:

        【解决方案6】:

        试试 dialog.cancel();在您的 onclick 侦听器中。 它应该可以工作。

        【讨论】:

          【解决方案7】:

          您需要实现 DialogInterface.OnClickListener 而不是 View.OnClickListener。然后 dialog.dismiss() 将可用。

          new DialogInterface.OnClickListener(){
          public void onClick(DialogInterface dialog, int which){
                 dialog.dismiss();
          }
          }
          

          【讨论】:

            【解决方案8】:

            出现错误,试试这段代码

               dialog.setView(layout);
            
               final AlertDialog alertDialog = dialog.create();
            
               add_item.setOnClickListener(new OnClickListener(){
                @Override
                 public void onClick(View v)
                    {
                      if (alertDialog != null && alertDialog.isShowing()) {
                          alertDialog.dismiss();
                    }
                  }
                });
               alertDialog.show();

            【讨论】:

              【解决方案9】:
                   final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(DebugActivity.this);
                      viewProgressDialogue.show();
                      new Handler().postDelayed(new Runnable() {
                          @Override
                          public void run() {
                              Log.d("response","im calling");
                              viewProgressDialogue.dismiss();
                          }
                      }, 5000);
              
              > Make it one Instance so that it will work
              It Will in the Fragment too.
              
              for Fragment Use
              
                  final viewProgressDialogue viewProgressDialogue = new viewProgressDialogue(getActivity());
                      viewProgressDialogue.show();
                      new Handler().postDelayed(new Runnable() {
                          @Override
                          public void run() {
                              Log.d("response", "im calling");
                              viewProgressDialogue.dismiss();
                          }
                      }, 5000);
              
              > viewProgressDialogue Class
              
              public class viewProgressDialogue extends Dialog {
                  public viewProgressDialogue(@NonNull Context context) {
                      super(context);
              
                      WindowManager.LayoutParams wlmp = getWindow().getAttributes();
                      getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
                      wlmp.gravity = Gravity.CENTER_HORIZONTAL;
                      getWindow().setAttributes(wlmp);
                      setTitle(null);
                      setCancelable(false);
                      setOnCancelListener(null);
              
                      View view = LayoutInflater.from(context).inflate(
                              R.layout.custom_progress, null);
                      setContentView(view);
              
              
                  }
              }
              

              【讨论】:

                【解决方案10】:

                试试这个:

                btn_go_anyway.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v)
                    {
                        getDialog().dismiss();
                    }
                });
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多