简体   繁体   English

当我在android中单击对话框中的任何位置时如何关闭对话框

[英]How to dismiss dialog when I click any where in dialog in android

 final Dialog dialog = new Dialog(context);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.setCancelable(true);
            dialog.setContentView(R.layout.dialog_image);
            ImageView dialogIv = (ImageView)dialog.findViewById(R.id.dialog_iv);
            TextView dialogTV = (TextView)dialog.findViewById(R.id.dialog_med_name);
            dialog.show();

When i click on anywhere on dialog it should dismiss.Full Screen its imageview dialog. 当我在对话框的任何地方单击时,它应该关闭。全屏显示其imageview对话框。

One thing you can do,Just set clicklistner on your Imageview. 您可以做的一件事,就是在Imageview上设置clicklistner。

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

setCanceledOnTouchOutside setCanceledOnTouchOutside

Added in API level 1 在API级别1中添加

void setCanceledOnTouchOutside (boolean cancel) setCanceledOnTouchOutside(布尔值取消)

Sets whether this dialog is canceled when touched outside the window's bounds. 设置在窗口范围之外触摸时是否取消此对话框。 If setting to true, the dialog is set to be cancelable if not already set. 如果设置为true,则将对话框设置为可取消(如果尚未设置)。

Parameterscancelboolean: Whether the dialog should be canceled when touched outside the window. 参数cancelboolean:在窗口外部触摸时是否应取消对话框。

You can create your own Touch Listener, and dismiss the dialog on the UP event. 您可以创建自己的触摸侦听器,并关闭UP事件上的对话框。

class MyTouchListener implement OnTouchListener{
public boolean onTouch(View v, MotionEvent event)
{
    if(event.getAction() == MotionEvent.ACTION_UP){
        // DISMISS DIALOG      
    }
    return true;
}

And then set this listener to your dialog 然后将此监听器设置为对话框

MyTouchListener l = new MyTouchListener();
dialog.setOnTouchListener(l);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM