{"id":132,"date":"2023-02-18T19:32:15","date_gmt":"2023-02-18T11:32:15","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=132"},"modified":"2023-02-18T19:33:37","modified_gmt":"2023-02-18T11:33:37","slug":"android-dialogfragment-click-external-area-dismiss","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/android-dialogfragment-click-external-area-dismiss\/","title":{"rendered":"DialogFragment\u70b9\u51fb\u5916\u90e8\u533a\u57df\u662f\u5426\u6d88\u5931"},"content":{"rendered":"<p>\u5728<code>DialogFragment<\/code>\u4e2d\u6709\u4e00\u4e2a<code>getDialog<\/code>\u7684\u65b9\u6cd5\uff0c\u8fd4\u56de\u7684\u662f<code>DialogFragment<\/code>\u4e2d\u7684<code>Dialog<\/code>\u5bf9\u8c61\uff0c\u70b9\u51fb<code>DialogFragment<\/code>\u5916\u90e8\u533a\u57df\u662f\u5426\u6d88\u5931\uff0c\u53ea\u9700\u8981\u64cd\u4f5c\u8fd9\u4e2a<code>Dialog<\/code>\u5bf9\u8c61\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-java\">DialogFragment.getDialog.setCancelable(false);\nDialogFragment.getDialog.setCanceledOnTouchOutside(false);<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u70b9\u51fb\u5916\u90e8\u533a\u57df\u6d88\u5931<\/h2>\n<pre><code class=\"language-java\">public class CustomFragment extends DialogFragment {\n\n    @Override\n    public void onStart() {\n        super.onStart();\n        \/\/\u70b9\u51fbwindow\u5916\u7684\u533a\u57df\uff0c\u662f\u5426\u6d88\u5931\n        getDialog().setCanceledOnTouchOutside(true);\n        \/\/\u662f\u5426\u53ef\u4ee5\u53d6\u6d88\uff0c\u4f1a\u5f71\u54cd\u4e0a\u9762\u90a3\u6761\u5c5e\u6027\n        \/\/setCancelable(false);\n        \/\/window\u5916\u53ef\u4ee5\u70b9\u51fb\uff0c\u4e0d\u62e6\u622a\u7a97\u53e3\u5916\u7684\u4e8b\u4ef6\n        \/\/getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);\n    }\n}<\/code><\/pre>\n<h2>\u4e0d\u53ef\u53d6\u6d88\u548c\u70b9\u51fb\u5916\u90e8\u4e0d\u6d88\u5931<\/h2>\n<pre><code class=\"language-java\">public class CustomFragment extends DialogFragment {\n\n    @Override\n    public void onStart() {\n        super.onStart();\n        \/\/\u70b9\u51fbwindow\u5916\u7684\u533a\u57df\uff0c\u662f\u5426\u6d88\u5931\n        getDialog().setCanceledOnTouchOutside(false);\n        \/\/\u662f\u5426\u53ef\u4ee5\u53d6\u6d88\uff0c\u4f1a\u5f71\u54cd\u4e0a\u9762\u90a3\u6761\u5c5e\u6027\n        setCancelable(false);\n        \/\/window\u5916\u53ef\u4ee5\u70b9\u51fb\uff0c\u4e0d\u62e6\u622a\u7a97\u53e3\u5916\u7684\u4e8b\u4ef6\n        \/\/getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);\n    }\n}<\/code><\/pre>\n<p>\u9644\u4e0a\u70b9\u51fb\u8fd4\u56de\u952e\u4e0d\u6d88\u5931<code>DialogFragment<\/code>\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-java\">DialogFragment.getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {\n    @Override\n    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {\n        if (keyCode == KeyEvent.KEYCODE_BACK) {\n            return true;\n        }\n        return false;\n    }\n});<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728DialogFragment\u4e2d\u6709\u4e00\u4e2agetDialog\u7684\u65b9\u6cd5\uff0c\u8fd4\u56de\u7684\u662fDialogFragment\u4e2d\u7684Dia [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-android-basic"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}