【问题标题】:How to close alertdialog with click on imageview如何通过单击 imageview 关闭警报对话框
【发布时间】:2014-06-10 06:49:27
【问题描述】:

如何通过点击右上角的 (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>     

【问题讨论】:

  • 使用AlertDialog alertDialog = alert.create(); 然后使用alertDIalog.dismiss()
  • Hii Raghunandan,我找不到dismiss(),我已经阅读了所有教程,它说使用dismiss()、cancel()、dialog.dismiss()等,但我找不到dismiss方法() 当我在键盘上键入 Ctrl+Space 时。 ——
  • 那是因为它是AlertDialog.Builder。看我之前的评论
  • 它没有工作 AlertDialog alertDialog = alert.create();然后使用 alertDIalog.dismiss()
  • 会而且应该

标签: android click imageview android-alertdialog


【解决方案1】:

在您的按钮单击中使用此代码

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

【讨论】:

  • 感谢 Giru Bhai,我已经修改了您的代码 final AlertDialog.Builder alert = new AlertDialog.Builder(context).create() 成为 final AlertDialog alert = new AlertDialog.Builder(context).create()
  • @user3172337 乐于助人,尽情享受
【解决方案2】:

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

   alert.dismiss()

【讨论】:

  • 嗨西奥,我找不到dismiss(),我已经阅读了所有教程,它说使用dismiss()、cancel()、dialog.dismiss()等,但我找不到dismiss方法() 当我在键盘上键入 Ctrl+Space 时。 ——
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 1970-01-01
  • 2012-01-13
  • 2023-02-16
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
相关资源
最近更新 更多