如何通过在 Android 中单击对话框来关闭该对话框?

发布于 2025-01-03 14:04:20 字数 93 浏览 1 评论 0原文

我看过几篇关于如何通过单击外部来关闭对话框的帖子。但是有没有办法通过单击对话框窗口内部来获得相同的功能?

对话框是否有任何侦听器可以检测对话框窗口上的点击?

I have seen several posts on how to dismiss a dialog by clicking on the outside. But is there a way to get the same functionality by clicking the inside the dialog window?

Are there any listeners for the Dialog that would detect a tap on the Dialog Window?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

残月升风 2025-01-10 14:04:20

重写 Dialog.onTouchEvent(. ..) 捕获屏幕上任意位置的任何点击。要通过点击任意位置关闭对话框:

Dialog dialog = new Dialog(this) {
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // Tap anywhere to close dialog.
    this.dismiss();
    return true;
  }
};

此代码片段无需调用 dialogObject.setCanceledOnTouchOutside(true);

Overriding Dialog.onTouchEvent(...) catches any tap, anywhere on the screen. To dismiss the dialog by tapping anywhere:

Dialog dialog = new Dialog(this) {
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // Tap anywhere to close dialog.
    this.dismiss();
    return true;
  }
};

This snippet nullifies the need to call dialogObject.setCanceledOnTouchOutside(true);.

一口甜 2025-01-10 14:04:20

大概您想要检测对话框范围内任何位置的触摸事件。如果您正在创建自定义对话框(即通过将一组 View 组装到某种布局 View 中,然后设置父 View使用 .setContentView() 作为对话框的主要内容视图),那么也许您可以简单地为该内容父级 View 设置一个 onTouch 监听器。此外,您可以使用 mDialog.findViewById() 获取视图,因此,如果您使用 AlertDialog,也许您可​​以以某种方式确定要使用的资源 ID抓住它的主布局View

Presumably you want to detect a touch event anywhere within the bounds of a dialog. If you're creating a custom dialog (i.e. by assembling a set of Views into a layout View of some sort, and then setting the parent View as the dialog's main content view using .setContentView()) then perhaps you could simply set an onTouch listener to that content parent View. Furthermore, you could grab hold of views using mDialog.findViewById(), so if for example you were using an AlertDialog, perhaps you could determine somehow what resource ID to use to grab hold of its main layout View.

凑诗 2025-01-10 14:04:20

如果您的对话框中有一个布局,您可以将其作为视图引用,并在其上放置一个 onClickListener 。因此,假设您的对话框具有自定义布局,并且可以查看整个对话框,请获取对此的引用。

例如,假设一个对话框有一个名为 mainll 的 LinearLayout,其中包含您的自定义视图,您将:

LinearLayout ll - (LinearLayout) findViewById(R.id.mainll);
ll.setOnClickListener(...) { onClick()... }

然后,只要在 LinearLayout 中单击任何内容,它就会注册一个单击事件。

If you have a Layout in your Dialog, you could get a reference to that as view, and put a onClickListener on that. So assuming your dialog has a custom layout, and view for the entire dialog, get a reference to that.

For instance, assuming a dialog has a LinearLayout named mainll, that contains your custom views, you would:

LinearLayout ll - (LinearLayout) findViewById(R.id.mainll);
ll.setOnClickListener(...) { onClick()... }

Then anytime anything is clicked within the LinearLayout, it will register a click event.

白云不回头 2025-01-10 14:04:20

您始终可以创建自己的对话框活动,并在用户单击要关闭对话框的区域时调用 finish()。

You can always create your own Dialog activity and call finish() when the user clicks the area that you want to close your Dialog.

无法言说的痛 2025-01-10 14:04:20

这是一个示例,解释了如何在对话框中处理 onTouch 事件。诀窍在于创建自定义侦听器。

http://about-android.blogspot.co.uk /2010/02/create-custom-dialog.html

Here is an example which explains how to handle onTouch events within the dialog. The trick is in creating a custom listener.

http://about-android.blogspot.co.uk/2010/02/create-custom-dialog.html

我还不会笑 2025-01-10 14:04:20
here i have taken my close icon ,if u need u can take anything like button

first of all u have implement to the class

class somethingclass Dialog implements View.OnClickListener

then set the event for particular 

      icon_close.setOnClickListener(this);

then override the class function

    @Override
    public void onClick(View v) {
        if(R.id.icon_close==v.getId()){
            dismiss();
        }else
}


Note:  if passible u can give dilaog.dismiss();
here i have taken my close icon ,if u need u can take anything like button

first of all u have implement to the class

class somethingclass Dialog implements View.OnClickListener

then set the event for particular 

      icon_close.setOnClickListener(this);

then override the class function

    @Override
    public void onClick(View v) {
        if(R.id.icon_close==v.getId()){
            dismiss();
        }else
}


Note:  if passible u can give dilaog.dismiss();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文