Android 如何关闭AlertDialog.Builder?

Android 如何关闭AlertDialog.Builder?,android,memory,dialog,alert,dismiss,Android,Memory,Dialog,Alert,Dismiss,在下面的代码中,如何关闭警报框?我不想造成内存泄漏。我在alertDialog上尝试了.disclose(),但无效。。。 谢谢 解雇()有什么不起作用 您应该能够使用或 只需重写create方法并保存对话框实例。然后你可以打电话辞退 @Override public AlertDialog create() { this.dialog = super.create(); return this.dialog; } 在代码的某个地方: dialog.dismiss(); 如果

在下面的代码中,如何关闭警报框?我不想造成内存泄漏。我在alertDialog上尝试了.disclose(),但无效。。。 谢谢


解雇()有什么不起作用

您应该能够使用或


只需重写create方法并保存对话框实例。然后你可以打电话辞退

@Override
public AlertDialog create() {
    this.dialog = super.create();
    return this.dialog;
}
在代码的某个地方:

dialog.dismiss();

如果您不想在自定义布局中放置任何按钮,比如说textview,并且您想在单击该textview时关闭警报对话框,则必须使用这种方式:

AlertDialog alertDialog = builder.show();
然后检查

if(alertDialog != null && alertDialog.isShowing()){
      alertDialog.dismiss();
}

如果您想在检查某些条件后在活动的其他地方取消该对话框,也可以这样做。

使用以下代码,您可以显示一个列表框警报对话框,当您按下某个项目时,您将取消该对话框。代码的顺序很重要

AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

String names[] ={"A","B","C","D"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,names);

LayoutInflater inflater = getLayoutInflater();
View convertView = (View)inflater.inflate(R.layout.list_layout, null);
ListView lv = (ListView) convertView.findViewById(R.id.listView1);
lv.setAdapter(adapter);
alertDialog.setView(convertView);
alertDialog.setTitle("List");
final AlertDialog ad = alertDialog.show();

lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        //Toast.makeText(mContext, position, Toast.LENGTH_LONG).show();
        ad.dismiss();
    }
});
AlertDialog.Builder AlertDialog=新建AlertDialog.Builder(mContext);
字符串名[]={“A”、“B”、“C”、“D”};
ArrayAdapter=新的ArrayAdapter(mContext,android.R.layout.simple\u list\u item\u 1,name);
LayoutInflater充气机=getLayoutInflater();
视图转换视图=(视图)充气器。充气(R.layout.list\u layout,null);
ListView lv=(ListView)convertView.findViewById(R.id.listView1);
低压设置适配器(适配器);
alertDialog.setView(convertView);
alertDialog.setTitle(“列表”);
最终AlertDialog ad=AlertDialog.show();
lv.setOnItemClickListener(新的AdapterView.OnItemClickListener()
{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id)
{
//Toast.makeText(mContext,position,Toast.LENGTH_LONG).show();
解雇;
}
});

此方法演示所需的代码。。根据Namrata先前的回答

@SuppressLint("InflateParams")
private static void showDialog(Activity activity, int layoutId)
{
    LayoutInflater inflater = activity.getLayoutInflater();
    View dialoglayout = inflater.inflate(layoutId, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setView(dialoglayout);

    CustomDialog.doCustomLogic(activity, layoutId, dialoglayout);

    final AlertDialog alertDialog = builder.show();

    // caller assumes there will be a btn_close element present
    Button closeNowBtn = (Button) dialoglayout.findViewById(R.id.btn_close);
    closeNowBtn.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            alertDialog.dismiss();
        }
    });
}

代码非常简单:

final AlertDialog show = alertDialog.show();
最后,在按钮的操作中,例如:

show.dismiss();
例如,使用自定义alertdialog:

在java上编写代码时,您可以创建一个对象警报对话框:

public class ViewAlertRating {

    Context context;
    public ViewAlertRating(Context context) {
        this.context = context;
    }

    public void showAlert(){

        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        View alertView = inflater.inflate(R.layout.layout_test, null);
        alertDialog.setView(alertView);

        final AlertDialog show = alertDialog.show();

        Button alertButton = (Button) alertView.findViewById(R.id.btn_test);
        alertButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                show.dismiss();
            }
        });
    }
}
代码XML布局_test.XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Valoración"
        android:id="@+id/text_test1"
        android:textSize="20sp"
        android:textColor="#ffffffff"
        android:layout_centerHorizontal="true"
        android:gravity="center_horizontal"
        android:textStyle="bold"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:background="#ff37dabb"
        android:paddingLeft="20dp"
        android:paddingRight="20dp" />


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:layout_marginTop="15dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:id="@+id/edit_test"
            android:hint="Descripción"
            android:textColor="#aa000000"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:textColorHint="#aa72777a"
            android:gravity="top" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:paddingTop="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingBottom="15dp" >

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="1.00"
                android:gravity="right" >

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Enviar"
                    android:id="@+id/btn_test"
                    android:gravity="center_vertical|center_horizontal"
                    android:textColor="#ffffffff"
                    android:background="@drawable/btn_flat_blue_selector" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

我试过了,成功了

.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               alertDialog.setCancelable(true);
            }
        });

关闭或取消
AlertDialog.Builder

dialog.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.dismiss()
    }
});

您必须在对话框界面上调用
disclose()

AlertDialog.Builder
没有
cancel
method@xmenW.K.
cancel()
是对话框本身的一种方法,而不是生成器。是的,但在您的代码中(&问题也是)
alertDialog
是一种
builder
。在
onClick
中,您应该使用param中的
对话框。谢谢=)很好的解决方案!别问了,太好了!非常感谢!非常感谢。这里的关键是关闭使用alertDialog.show()创建的对话框。从生成器创建一个对话框并在该对话框上调用disclose不会关闭我们在调用show()时创建的同一个对话框。这一个让我有点难堪(=)完美的和最好的答案与伟大的解释是的,这是方式。为什么他们没有在构建器中包含close方法,这让我很困惑。。。不是每个人都想使用默认按钮。对于自定义AlertDialog,这是目前唯一的方法。谢谢你,很好的解决方案。帮了我很多忙!我一直在寻找的东西:-)这是唯一适合我的解决方案。我创建了一个从AlertDialog.Builder扩展而来的类。有了它,我就可以创建自己的方法了。
ViewAlertRating alertRating = new ViewAlertRating(this);
alertRating.showAlert();
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
               alertDialog.setCancelable(true);
            }
        });
dialog.setNegativeButton("إلغاء", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {
        dialogInterface.dismiss()
    }
});