如何通过单击imageview关闭alertdialog

时间:2014-06-10 06:49:28

标签: android click imageview alertdialog

如何在右上角点击(x)imageview关闭alertdialog?我在右上角警告对话框中有(x)自定义警报对话框。有人可以解决我的问题吗?谢谢 以下是我的源代码:

convertView.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    //Toast.makeText(context, matArr[position].toString(), Toast.LENGTH_LONG).show();
                    LayoutInflater li = LayoutInflater.from(context);
                    final View inputdialogcustom = li.inflate(R.layout.activity_view_detail_item_fullscreen3, null);
                    final AlertDialog.Builder alert = new AlertDialog.Builder(context);

                    ImageView img=(ImageView) inputdialogcustom.findViewById(R.id.imgmainbig);                  
                    int imgIDBig=context.getResources().getIdentifier(matArr[position].toString(), "drawable", context.getPackageName());
                    Bitmap gmbr=BitmapFactory.decodeResource(context.getResources(),imgIDBig);                  
                    img.setImageBitmap(gmbr);

                    ImageView imgclose=(ImageView) inputdialogcustom.findViewById(R.id.btnclosepopup);                  
                    imgclose.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View view) {
                            // TODO Auto-generated method stub                          
                            //bla bla -------------HERE--------------------
                        }
                    });                 

                    alert.setView(inputdialogcustom);
                    alert.show();   
                }
            });

以下是我的布局结构:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="10dp">
    <ImageView
        android:id="@+id/btnclosepopup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_margin="2dp"
        android:src="@drawable/closebutton" />           
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >                      
        <ImageView 
            android:id="@+id/imgmainbig"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter" />
    </LinearLayout>    
</LinearLayout>     

2 个答案:

答案 0 :(得分:2)

在按钮点击中使用此代码

ImageView imgclose=(ImageView) inputdialogcustom.findViewById(R.id.btnclosepopup);                  
                        imgclose.setOnClickListener(new OnClickListener() {

                            @Override
                            public void onClick(View view) {
                               if(alert!= null && alert.isShowing()){
                             alert.dismiss();
                             }
                            }
                        });    

获得.dismiss()更改

final AlertDialog.Builder alert = new AlertDialog.Builder(context);

final AlertDialog.Builder alert = new AlertDialog.Builder(context).create();

答案 1 :(得分:1)

您只需要关闭对话框即可。要实现这一点,你的对话变量必须是最终的。

   alert.dismiss()