{"id":143,"date":"2023-02-18T20:33:34","date_gmt":"2023-02-18T12:33:34","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=143"},"modified":"2023-02-18T20:33:51","modified_gmt":"2023-02-18T12:33:51","slug":"android-dialog-usage","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/18\/android-dialog-usage\/","title":{"rendered":"Android\u5bf9\u8bdd\u6846\uff08Dialog\uff09\u4f7f\u7528\u65b9\u6cd5\u6c47\u603b"},"content":{"rendered":"<p>Android\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684Dialog\u51fd\u6570\uff0c\u672c\u6587\u4ecb\u7ecd\u6700\u5e38\u7528\u76848\u79cd\u5bf9\u8bdd\u6846\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5305\u62ec\u666e\u901a\uff08\u5305\u542b\u63d0\u793a\u6d88\u606f\u548c\u6309\u94ae\uff09\u3001\u5217\u8868\u3001\u5355\u9009\u3001\u591a\u9009\u3001\u7b49\u5f85\u3001\u8fdb\u5ea6\u6761\u3001\u7f16\u8f91\u3001\u81ea\u5b9a\u4e49\u7b49\u591a\u79cd\u5f62\u5f0f\u3002\u5728\u5bf9\u8bdd\u6846\u521b\u5efa\u6216\u5173\u95ed\u65f6\u5b8c\u6210\u4e00\u4e9b\u7279\u5b9a\u7684\u529f\u80fd\uff0c\u9700\u8981\u590d\u5199Dialog\u7684create()\u3001show()\u3001dismiss()\u7b49\u65b9\u6cd5\u3002<\/p>\n<p><!-- more --><\/p>\n<h2>\u666e\u901aDialog<\/h2>\n<h3>2\u4e2a\u6309\u94ae<\/h3>\n<pre><code class=\"language-java\">private void showNormalDialog() {\n    \/* \n     * @setIcon \u8bbe\u7f6e\u5bf9\u8bdd\u6846\u56fe\u6807\n     * @setTitle \u8bbe\u7f6e\u5bf9\u8bdd\u6846\u6807\u9898\n     * @setMessage \u8bbe\u7f6e\u5bf9\u8bdd\u6846\u6d88\u606f\u63d0\u793a\n     * setXXX\u65b9\u6cd5\u8fd4\u56deDialog\u5bf9\u8c61\uff0c\u56e0\u6b64\u53ef\u4ee5\u94fe\u5f0f\u8bbe\u7f6e\u5c5e\u6027\n     *\/\n    final AlertDialog.Builder normalDialog = \n        new AlertDialog.Builder(MainActivity.this);\n    normalDialog.setIcon(R.drawable.icon_dialog);\n    normalDialog.setTitle(&quot;\u666e\u901aDialog&quot;)\n    normalDialog.setMessage(&quot;\u4f60\u8981\u70b9\u51fb\u54ea\u4e00\u4e2a\u6309\u94ae\u5462?&quot;);\n    normalDialog.setPositiveButton(&quot;\u786e\u5b9a&quot;, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n\n        }\n    });\n    normalDialog.setNegativeButton(&quot;\u5173\u95ed&quot;, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n\n        }\n    });\n    \/\/ \u663e\u793a\n    normalDialog.show();\n}<\/code><\/pre>\n<h3>3\u4e2a\u6309\u94ae<\/h3>\n<pre><code class=\"language-java\">\/*\n * @setNeutralButton \u8bbe\u7f6e\u4e2d\u95f4\u7684\u6309\u94ae\n * \u82e5\u53ea\u9700\u4e00\u4e2a\u6309\u94ae\uff0c\u4ec5\u8bbe\u7f6e setPositiveButton \u5373\u53ef\n *\/\nprivate void showMultiBtnDialog() {\n    AlertDialog.Builder normalDialog = \n        new AlertDialog.Builder(MainActivity.this);\n    normalDialog.setIcon(R.drawable.icon_dialog);\n    normalDialog.setTitle(&quot;\u666e\u901aDialog&quot;).setMessage(&quot;\u4f60\u8981\u70b9\u51fb\u54ea\u4e00\u4e2a\u6309\u94ae\u5462?&quot;);\n    normalDialog.setPositiveButton(&quot;\u6309\u94ae1&quot;, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n\n        }\n    });\n    normalDialog.setNeutralButton(&quot;\u6309\u94ae2&quot;, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n\n        }\n    });\n    normalDialog.setNegativeButton(&quot;\u6309\u94ae3&quot;, new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n\n        }\n    });\n    \/\/ \u521b\u5efa\u5b9e\u4f8b\u5e76\u663e\u793a\n    normalDialog.show();\n}<\/code><\/pre>\n<h2>\u5217\u8868Dialog<\/h2>\n<pre><code class=\"language-java\">private void showListDialog() {\n    final String[] items = { &quot;\u6211\u662f1&quot;, &quot;\u6211\u662f2&quot;, &quot;\u6211\u662f3&quot;, &quot;\u6211\u662f4&quot; };\n    AlertDialog.Builder listDialog = \n        new AlertDialog.Builder(MainActivity.this);\n    listDialog.setTitle(&quot;\u5217\u8868Dialog&quot;);\n    listDialog.setItems(items, new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n            \/\/ which \u4e0b\u6807\u4ece0\u5f00\u59cb\n            Toast.makeText(MainActivity.this, &quot;\u4f60\u70b9\u51fb\u4e86&quot; + items[which], Toast.LENGTH_SHORT).show();\n        }\n    });\n    listDialog.show();\n}<\/code><\/pre>\n<h2>\u5355\u9009Dialog<\/h2>\n<pre><code class=\"language-java\">int yourChoice;\nprivate void showSingleChoiceDialog() {\n    final String[] items = { &quot;\u6211\u662f1&quot;, &quot;\u6211\u662f2&quot;, &quot;\u6211\u662f3&quot;, &quot;\u6211\u662f4&quot; };\n    yourChoice = -1;\n    AlertDialog.Builder singleChoiceDialog = \n        new AlertDialog.Builder(MainActivity.this);\n    singleChoiceDialog.setTitle(&quot;\u5355\u9009Dialog&quot;);\n    \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u9ed8\u8ba4\u9009\u9879\uff0c\u6b64\u5904\u8bbe\u7f6e\u4e3a0\n    singleChoiceDialog.setSingleChoiceItems(items, 0, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n            yourChoice = which;\n        }\n    });\n    singleChoiceDialog.setPositiveButton(&quot;\u786e\u5b9a&quot;, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n            if (yourChoice != -1) {\n                Toast.makeText(MainActivity.this, \n                &quot;\u4f60\u9009\u62e9\u4e86&quot; + items[yourChoice], \n                Toast.LENGTH_SHORT).show();\n            }\n        }\n    });\n    singleChoiceDialog.show();\n}<\/code><\/pre>\n<h2>\u591a\u9009Dialog<\/h2>\n<pre><code class=\"language-java\">ArrayList&lt;Integer&gt; yourChoices = new ArrayList&lt;&gt;();\nprivate void showMultiChoiceDialog() {\n    final String[] items = { &quot;\u6211\u662f1&quot;, &quot;\u6211\u662f2&quot;, &quot;\u6211\u662f3&quot;, &quot;\u6211\u662f4&quot; };\n    \/\/ \u8bbe\u7f6e\u9ed8\u8ba4\u9009\u4e2d\u7684\u9009\u9879\uff0c\u5168\u4e3afalse\u9ed8\u8ba4\u5747\u672a\u9009\u4e2d\n    final boolean initChoiceSets[]={false, false, false, false};\n    yourChoices.clear();\n    AlertDialog.Builder multiChoiceDialog = \n        new AlertDialog.Builder(MainActivity.this);\n    multiChoiceDialog.setTitle(&quot;\u591a\u9009Dialog&quot;);\n    multiChoiceDialog.setMultiChoiceItems(items, initChoiceSets,\n        new DialogInterface.OnMultiChoiceClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which,\n            boolean isChecked) {\n            if (isChecked) {\n                yourChoices.add(which);\n            } else {\n                yourChoices.remove(which);\n            }\n        }\n    });\n    multiChoiceDialog.setPositiveButton(&quot;\u786e\u5b9a&quot;, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n            int size = yourChoices.size();\n            String str = &quot;&quot;;\n            for (int i = 0; i &lt; size; i++) {\n                str += items[yourChoices.get(i)] + &quot; &quot;;\n            }\n            Toast.makeText(MainActivity.this, &quot;\u4f60\u9009\u4e2d\u4e86&quot; + str, Toast.LENGTH_SHORT).show();\n        }\n    });\n    multiChoiceDialog.show();\n}<\/code><\/pre>\n<h2>\u7b49\u5f85Dialog<\/h2>\n<pre><code class=\"language-java\">private void showWaitingDialog() {\n    \/* \n     * \u7b49\u5f85Dialog\u5177\u6709\u5c4f\u853d\u5176\u4ed6\u63a7\u4ef6\u7684\u4ea4\u4e92\u80fd\u529b\n     * @setCancelable \u4e3a\u4f7f\u5c4f\u5e55\u4e0d\u53ef\u70b9\u51fb\uff0c\u8bbe\u7f6e\u4e3a\u4e0d\u53ef\u53d6\u6d88(false)\n     * \u4e0b\u8f7d\u7b49\u4e8b\u4ef6\u5b8c\u6210\u540e\uff0c\u4e3b\u52a8\u8c03\u7528\u51fd\u6570\u5173\u95ed\u8be5Dialog\n     *\/\n    ProgressDialog waitingDialog= \n        new ProgressDialog(MainActivity.this);\n    waitingDialog.setTitle(&quot;\u7b49\u5f85Dialog&quot;);\n    waitingDialog.setMessage(&quot;\u7b49\u5f85\u4e2d...&quot;);\n    waitingDialog.setIndeterminate(true);\n    waitingDialog.setCancelable(false);\n    waitingDialog.show();\n}<\/code><\/pre>\n<h2>\u8fdb\u5ea6\u6761Dialog<\/h2>\n<pre><code class=\"language-java\">private void showProgressDialog() {\n    \/* \n     * @setProgress \u8bbe\u7f6e\u521d\u59cb\u8fdb\u5ea6\n     * @setProgressStyle \u8bbe\u7f6e\u6837\u5f0f\uff08\u6c34\u5e73\u8fdb\u5ea6\u6761\uff09\n     * @setMax \u8bbe\u7f6e\u8fdb\u5ea6\u6700\u5927\u503c\n     *\/\n    final int MAX_PROGRESS = 100;\n    final ProgressDialog progressDialog = \n        new ProgressDialog(MainActivity.this);\n    progressDialog.setProgress(0);\n    progressDialog.setTitle(&quot;\u8fdb\u5ea6\u6761Dialog&quot;);\n    progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);\n    progressDialog.setMax(MAX_PROGRESS);\n    progressDialog.show();\n    \/* \n     * \u6a21\u62df\u8fdb\u5ea6\u589e\u52a0\u7684\u8fc7\u7a0b\n     * \u65b0\u5f00\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u6bcf\u4e2a100ms\uff0c\u8fdb\u5ea6\u589e\u52a01\n     *\/\n    new Thread(new Runnable() {\n        @Override\n        public void run() {\n            int progress= 0;\n            while (progress &lt; MAX_PROGRESS){\n                try {\n                    Thread.sleep(100);\n                    progress++;\n                    progressDialog.setProgress(progress);\n                } catch (InterruptedException e){\n                    e.printStackTrace();\n                }\n            }\n            \/\/ \u8fdb\u5ea6\u8fbe\u5230\u6700\u5927\u503c\u540e\uff0c\u7a97\u53e3\u6d88\u5931\n            progressDialog.cancel();\n        }\n    }).start();\n}<\/code><\/pre>\n<h2>\u7f16\u8f91Dialog<\/h2>\n<pre><code class=\"language-java\">private void showInputDialog() {\n    \/*\n     * @setView \u88c5\u5165\u4e00\u4e2aEditView\n     *\/\n    final EditText editText = new EditText(MainActivity.this);\n    AlertDialog.Builder inputDialog = \n        new AlertDialog.Builder(MainActivity.this);\n    inputDialog.setTitle(&quot;\u8f93\u5165Dialog&quot;).setView(editText);\n    inputDialog.setPositiveButton(&quot;\u786e\u5b9a&quot;, \n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n            Toast.makeText(MainActivity.this,\n            editText.getText().toString(), \n            Toast.LENGTH_SHORT).show();\n        }\n    }).show();\n}<\/code><\/pre>\n<h2>\u81ea\u5b9a\u4e49Dialog<\/h2>\n<pre><code class=\"language-xml\">&lt;!-- res\/layout\/dialog_customize.xml--&gt;\n&lt;!-- \u81ea\u5b9a\u4e49View --&gt;\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\n    android:orientation=&quot;vertical&quot;\n    android:layout_width=&quot;match_parent&quot;\n    android:layout_height=&quot;match_parent&quot;&gt;\n    &lt;EditText\n        android:id=&quot;@+id\/edit_text&quot;\n        android:layout_width=&quot;match_parent&quot;\n        android:layout_height=&quot;wrap_content&quot; \n        \/&gt;\n&lt;\/LinearLayout&gt;<\/code><\/pre>\n<pre><code class=\"language-java\">private void showCustomizeDialog() {\n    \/*\n     * @setView \u88c5\u5165\u81ea\u5b9a\u4e49View ==&gt; R.layout.dialog_customize\n     * dialog_customize.xml\u53ea\u653e\u7f6e\u4e86\u4e00\u4e2aEditView\n     * dialog_customize.xml\u53ef\u81ea\u5b9a\u4e49\u66f4\u590d\u6742\u7684View\n     *\/\n    AlertDialog.Builder customizeDialog = \n        new AlertDialog.Builder(MainActivity.this);\n    final View dialogView = LayoutInflater.from(MainActivity.this)\n        .inflate(R.layout.dialog_customize,null);\n    customizeDialog.setTitle(&quot;\u6211\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49Dialog&quot;);\n    customizeDialog.setView(dialogView);\n    customizeDialog.setPositiveButton(&quot;\u786e\u5b9a&quot;,\n        new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n            \/\/ \u83b7\u53d6EditView\u4e2d\u7684\u8f93\u5165\u5185\u5bb9\n            EditText edit_text = \n                (EditText) dialogView.findViewById(R.id.edit_text);\n            Toast.makeText(MainActivity.this,\n                edit_text.getText().toString(),\n                Toast.LENGTH_SHORT).show();\n        }\n    });\n    customizeDialog.show();\n}<\/code><\/pre>\n<h2>\u590d\u5199\u56de\u8c03\u51fd\u6570<\/h2>\n<pre><code class=\"language-java\">\/*\n * \u590d\u5199Builder\u7684create\u548cshow\u51fd\u6570\uff0c\u53ef\u4ee5\u5728Dialog\u663e\u793a\u524d\u5b9e\u73b0\u5fc5\u8981\u8bbe\u7f6e\n * \u4f8b\u5982\u521d\u59cb\u5316\u5217\u8868\u3001\u9ed8\u8ba4\u9009\u9879\u7b49\n * @create \u7b2c\u4e00\u6b21\u521b\u5efa\u65f6\u8c03\u7528\n * @show \u6bcf\u6b21\u663e\u793a\u65f6\u8c03\u7528\n *\/\nprivate void showListDialog() {\n    final String[] items = { &quot;\u6211\u662f1&quot;, &quot;\u6211\u662f2&quot;, &quot;\u6211\u662f3&quot;, &quot;\u6211\u662f4&quot; };\n    AlertDialog.Builder listDialog = \n        new AlertDialog.Builder(MainActivity.this) {\n\n        @Override\n        public AlertDialog create() {\n            items[0] = &quot;\u6211\u662fNo.1&quot;;\n            return super.create();\n        }\n\n        @Override\n        public AlertDialog show() {\n            items[1] = &quot;\u6211\u662fNo.2&quot;;\n            return super.show();\n        }\n    };\n    listDialog.setTitle(&quot;\u6211\u662f\u4e00\u4e2a\u5217\u8868Dialog&quot;);\n    listDialog.setItems(items, new DialogInterface.OnClickListener() {\n        @Override\n        public void onClick(DialogInterface dialog, int which) {\n\n        }\n    });\n    \/* @setOnDismissListener Dialog\u9500\u6bc1\u65f6\u8c03\u7528\n     * @setOnCancelListener Dialog\u5173\u95ed\u65f6\u8c03\u7528\n     *\/\n    listDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {\n        public void onDismiss(DialogInterface dialog) {\n            Toast.makeText(getApplicationContext(), &quot;Dialog\u88ab\u9500\u6bc1\u4e86&quot;, Toast.LENGTH_SHORT).show();\n        }\n    });\n    listDialog.show();\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Android\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684Dialog\u51fd\u6570\uff0c\u672c\u6587\u4ecb\u7ecd\u6700\u5e38\u7528\u76848\u79cd\u5bf9\u8bdd\u6846\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5305\u62ec\u666e\u901a\uff08\u5305\u542b\u63d0\u793a\u6d88\u606f\u548c\u6309\u94ae [&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":[63],"class_list":["post-143","post","type-post","status-publish","format-standard","hentry","category-android-basic","tag-dialog"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/143","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=143"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/143\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}