关闭按钮上的自定义提醒对话框单击

问题描述:

我在关闭提醒对话框时遇到问题。我正在使用布局充气器来制作对话框,因此我不确定在完成该操作后如何关闭它。代码如下:关闭按钮上的自定义提醒对话框单击

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电话不提供给我。

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

+4

调用'解雇( );' – Vivek 2011-04-19 08:09:56

尝试dialog.cancel();在你的onclick监听器中。 它应该工作。

像这样做,

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

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

变量alertDialog是从内部类中访问的。需要被宣布为最终。 – 2012-12-20 09:24:01

+0

什么是DialogInterface dia? – 2018-01-27 15:58:14

我已修改了代码PLZ检查..

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

最新加入

最后AlertDialog Dial = alertDialog.create(); and change dialog.setView(layout);Dial.setView(layout);

现在只需要调用Dial.dismiss(); onclick listener .. 适合我。

+1

@Abdus ..为什么你调用create();两次? – DJhon 2013-10-26 18:05:11

试试这个:

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对象。这对我有用。让我知道它是如何为你。

+0

您的解释称为create()是我正在寻找的。 – zeeshan 2015-02-17 20:44:38

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

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

使用此代码在类:

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

并创建自定义。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/