{"id":825,"date":"2015-07-02T15:31:40","date_gmt":"2015-07-02T07:31:40","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=825"},"modified":"2016-06-07T16:52:26","modified_gmt":"2016-06-07T08:52:26","slug":"android%e5%af%b9%e8%af%9d%e6%a1%86dialog","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=825","title":{"rendered":"Android\u5bf9\u8bdd\u6846Dialog"},"content":{"rendered":"<p>\u5bf9\u8bdd\u6846\u987e\u540d\u601d\u4e49\u5373\u4eba\u4e0e\u5e94\u7528\u7cfb\u7edf\u5bf9\u8bdd\u4ea4\u4e92\u7684\u7a97\u53e3\uff0c\u767b\u5f55\u64cd\u4f5c\u4e2d\u68c0\u67e5\u7528\u6237\u4fe1\u606f\u8f93\u5165\u60c5\u51b5\u53ef\u4ee5\u7528\u5bf9\u8bdd\u6846\u8fdb\u884c\u63d0\u793a\uff0c\u9000\u51fa\u5e94\u7528\u65f6\u4e5f\u53ef\u4ee5\u5f39\u51fa\u4e00\u4e2a\u9000\u51fa\u786e\u8ba4\u5bf9\u8bdd\u6846\u3002\u53ea\u8981\u6d89\u53ca\u5230\u4ea4\u4e92\u7684\u5730\u65b9\u90fd\u53ef\u4ee5\u4f7f\u7528\u5bf9\u8bdd\u6846.<br \/>\n<!--more--><\/p>\n<p>\u4e00\u4e2a\u5bf9\u8bdd\u6846\u4e00\u822c\u662f\u4e00\u4e2a\u51fa\u73b0\u5728\u5f53\u524dActivity\u4e4b\u4e0a\u7684\u4e00\u4e2a\u5c0f\u7a97\u53e3. \u5904\u4e8e\u4e0b\u9762\u7684Activity\u5931\u53bb\u7126\u70b9, \u5bf9\u8bdd\u6846\u63a5\u53d7\u6240\u6709\u7684\u7528\u6237\u4ea4\u4e92.\u5bf9\u8bdd\u6846\u4e00\u822c\u7528\u4e8e\u63d0\u793a\u4fe1\u606f\u548c\u4e0e\u5f53\u524d\u5e94\u7528\u7a0b\u5e8f\u76f4\u63a5\u76f8\u5173\u7684\u5c0f\u529f\u80fd.<br \/>\nAndroid API \u652f\u6301\u4e0b\u5217\u7c7b\u578b\u7684\u5bf9\u8bdd\u6846\u5bf9\u8c61:<\/p>\n<p>\u8b66\u544a\u5bf9\u8bdd\u6846 AlertDialog: \u4e00\u4e2a\u53ef\u4ee5\u67090\u52303\u4e2a\u6309\u94ae, \u4e00\u4e2a\u5355\u9009\u6846\u6216\u590d\u9009\u6846\u7684\u5217\u8868\u7684\u5bf9\u8bdd\u6846. \u8b66\u544a\u5bf9\u8bdd\u6846\u53ef\u4ee5\u521b\u5efa\u5927\u591a\u6570\u7684\u4ea4\u4e92\u754c\u9762, \u662f\u63a8\u8350\u7684\u7c7b\u578b.<br \/>\n\u8fdb\u5ea6\u5bf9\u8bdd\u6846 ProgressDialog: \u663e\u793a\u4e00\u4e2a\u8fdb\u5ea6\u73af\u6216\u8005\u4e00\u4e2a\u8fdb\u5ea6\u6761. \u7531\u4e8e\u5b83\u662fAlertDialog\u7684\u6269\u5c55, \u6240\u4ee5\u5b83\u4e5f\u652f\u6301\u6309\u94ae.<br \/>\n\u65e5\u671f\u9009\u62e9\u5bf9\u8bdd\u6846 DatePickerDialog: \u8ba9\u7528\u6237\u9009\u62e9\u4e00\u4e2a\u65e5\u671f.<br \/>\n\u65f6\u95f4\u9009\u62e9\u5bf9\u8bdd\u6846 TimePickerDialog: \u8ba9\u7528\u6237\u9009\u62e9\u4e00\u4e2a\u65f6\u95f4.<br \/>\n\u5982\u679c\u4f60\u5e0c\u671b\u81ea\u5b9a\u4e49\u4f60\u7684\u5bf9\u8bdd\u6846, \u53ef\u4ee5\u6269\u5c55Dialog\u7c7b.<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u5b66\u4e60Android\u7cfb\u7edf\u4e2d\u5e38\u7528\u5bf9\u8bdd\u6846\u4ee5\u53ca\u5b9e\u73b0\u65b9\u5f0f.<\/p>\n<p><span style=\"color: #993300;\"><strong>\u4e00\u3001Android\u5e38\u7528\u5bf9\u8bdd\u6846\u7ed3\u6784\u56fe<\/strong><\/span><br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702145915.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-822\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702145915.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150702145915\" width=\"541\" height=\"173\" \/><\/a><br \/>\n1\u3001\u5e38\u7528\u786e\u8ba4\u5bf9\u8bdd\u6846 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a02\u3001\u65e5\u671f\u5bf9\u8bdd\u6846<br \/>\n<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702145225.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-826\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702145225.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150702145225\" width=\"240\" height=\"147\" \/><\/a>\u00a0\u00a0<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150531.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-830\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150531.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150702150531\" width=\"243\" height=\"234\" \/><\/a><\/p>\n<p>3\u3001\u5355\u9009\u5bf9\u8bdd\u6846 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a04\u3001\u591a\u9009\u5bf9\u8bdd\u6846<\/p>\n<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150553.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-831\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150553.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150702150553\" width=\"239\" height=\"240\" \/><\/a>\u00a0\u00a0<a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150603.png\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-832\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150603.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150702150603\" width=\"238\" height=\"242\" \/><\/a><\/p>\n<p>5\u3001\u81ea\u5b9a\u4e49\u5bf9\u8bdd\u6846<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-833\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150543.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150702150543\" width=\"273\" height=\"197\" \/><\/p>\n<p><span style=\"color: #993300;\"><strong>\u4e8c\u3001\u5bf9\u8bdd\u6846\u5b9e\u73b0\u65b9\u5f0f<\/strong><\/span><br \/>\n1.\u9996\u5148\u5b9e\u4f8b\u5316\u5bf9\u8bdd\u6846,\u8bbe\u7f6e\u76f8\u5173\u5c5e\u6027.<br \/>\n\u5982:<\/p>\n<p>[java]<br \/>\nProgressDialog mProgressDialog = new ProgressDialog(this);<br \/>\nmProgressDialog.setMessage(&quot;\u4fe1\u606f\u4e0b\u8f7d\u52a0\u8f7d\u4e2d&#8230;.&quot;);<br \/>\nmProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);[\/java]<\/p>\n<p>2.\u8c03\u7528show\u65b9\u6cd5\u663e\u793a.<\/p>\n<p>[java] mProgressDialog.show();[\/java]<\/p>\n<p>3.\u8c03\u7528dismiss\u65b9\u6cd5\u9690\u85cf<\/p>\n<p>[java]  mProgressDialog.dismiss();[\/java]<\/p>\n<p><span style=\"color: #993300;\"><strong>\u4e09\u3001AlertDialog\u5bf9\u8bdd\u6846\u5b9e\u4f8b\u5316\u4e0e\u76f8\u5173\u5c5e\u6027\u8bbe\u7f6e<\/strong><\/span><\/p>\n<p>[java] AlertDialog.Builder builder = new AlertDialog.Builder(this);<br \/>\n     builder.setTitle(&quot;\u64cd\u4f5c\u63d0\u793a&quot;);<br \/>\n     builder.setMessage(&quot;\u662f\u5426\u9000\u51fa\uff1f&quot;);<br \/>\n     \/\/\u8bbe\u7f6e\u6309\u94ae\u4e8b\u4ef6<br \/>\n     builder.setNegativeButton(&quot;\u53d6\u6d88&quot;,<br \/>\n         new DialogInterface.OnClickListener() {<br \/>\n          @Override<br \/>\n          public void onClick(DialogInterface dialog, int which) {<br \/>\n            Toast.makeText(DialogAcitivity.this, &quot;\u53d6\u6d88&quot;,<br \/>\n                Toast.LENGTH_SHORT).show();<br \/>\n          }<br \/>\n     });<br \/>\n     \/\/\u521b\u5efa\u5bf9\u8bdd\u6846<br \/>\n     AlertDialog dialog = builder.create();[\/java]<\/p>\n<p><strong>android\u4e2dsetPositiveButton\u3001setNegativeButton\u548csetNeutralButton\u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\n\u5728\u5bf9\u8bdd\u6846\u4e2d\u51fa\u73b0\u7684\u4f4d\u7f6e\u4e0d\u540csetPositiveButton\u51fa\u73b0\u5728\u6700\u53f3\uff0cButton\u529f\u80fd\u8bbe\u7f6e\u4e3a&#8221;\u786e\u5b9a&#8221;; setNegativeButton\u8bbe\u7f6e\u7684\u662f\u51fa\u73b0\u5728\u6700\u5de6\uff0c\u4e00\u822c\u628a\u6700\u53f3\u7684button\u529f\u80fd\u8bbe\u7f6e\u4e3a\u201c\u53d6\u6d88\u201d\uff0c\u4e5f\u5c31\u662f\u8c03\u7528dismiss();setNeutralButton\u5982\u679c\u8bbe\u7f6e\u5219\u51fa\u73b0\u5728&#8221;\u4e2d\u95f4&#8221;\u3002<\/p>\n<p>\u7cfb\u7edf\u5bf9\u8bdd\u6846\u4e00\u822c\u7531\u4e09\u90e8\u4efd\u7ec4\u6210,\u6807\u9898\u533a\u57df,\u5185\u5bb9\u533a\u57df\uff0c\u6309\u94ae\u4e8b\u4ef6\u5904\u7406\u533a\u57df<br \/>\n<span style=\"color: #993300;\"><strong>\u56db\u3001\u81ea\u5b9a\u4e49\u5bf9\u8bdd\u6846\u5b9e\u73b0<\/strong><\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-833\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2015\/07\/QQ\u255c\u256a\u2550\u255d20150702150543.png\" alt=\"QQ\u255c\u256a\u2550\u255d20150702150543\" width=\"273\" height=\"197\" \/><br \/>\n1.\u5b9a\u4e49\u4e00\u4e2a\u7c7b\u7ee7\u627fAlertDialog<\/p>\n<p>[java] public class MyWeixinDialog extends AlertDialog {<br \/>\n  private Activity mContext;<br \/>\n  public MyWeixinDialog(Activity context) {<br \/>\n    super(context);<br \/>\n    mContext = context;<br \/>\n  }<\/p>\n<p>  @Override<br \/>\n  protected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.exit_dialog);<br \/>\n    Button confirm = (Button) findViewById(R.id.exitBtn0);<br \/>\n    Button cancel = (Button) findViewById(R.id.exitBtn1);<\/p>\n<p>    confirm.setOnClickListener(new View.OnClickListener() {<\/p>\n<p>     @Override<br \/>\n     public void onClick(View v) {<br \/>\n       mContext.finish();<br \/>\n     }<br \/>\n    });<\/p>\n<p>    cancel.setOnClickListener(new View.OnClickListener() {<\/p>\n<p>     @Override<br \/>\n     public void onClick(View v) {<br \/>\n       dismiss();<br \/>\n     }<br \/>\n    });<br \/>\n  }<br \/>\n}[\/java]<\/p>\n<p>[java]&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br \/>\n&lt;LinearLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;<br \/>\n    android:id=&quot;@+id\/exit_layout&quot;<br \/>\n    android:layout_width=&quot;280dp&quot;<br \/>\n    android:layout_height=&quot;wrap_content&quot;<br \/>\n    android:background=&quot;@drawable\/confirm_dialog_bg2&quot;<br \/>\n    android:gravity=&quot;center_horizontal&quot;<br \/>\n    android:layout_gravity=&quot;center_horizontal&quot;<br \/>\n    android:orientation=&quot;vertical&quot; &gt;<\/p>\n<p>    &lt;TextView<br \/>\n        android:layout_width=&quot;wrap_content&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot;<br \/>\n        android:layout_marginTop=&quot;4dp&quot;<br \/>\n        android:padding=&quot;5dp&quot;<br \/>\n        android:text=&quot;\u9000\u51fa\u5fae\u4fe1&quot;<br \/>\n        android:textColor=&quot;#333&quot;<br \/>\n        android:textSize=&quot;20sp&quot; \/&gt;<\/p>\n<p>    &lt;TextView<br \/>\n        android:layout_width=&quot;wrap_content&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot;<br \/>\n        android:layout_marginTop=&quot;1dp&quot;<br \/>\n        android:gravity=&quot;center_horizontal&quot;<br \/>\n        android:padding=&quot;10dp&quot;<br \/>\n        android:text=&quot;\u9000\u51fa\u540e\uff0c\u4f60\u5c06\u6536\u4e0d\u5230\u65b0\u7684\u6d88\\n\u606f.\u786e\u5b9a\u8981\u9000\u51fa\uff1f&quot;<br \/>\n        android:textColor=&quot;#333&quot;<br \/>\n        android:textSize=&quot;16sp&quot; \/&gt;<\/p>\n<p>    &lt;LinearLayout<br \/>\n        android:layout_width=&quot;wrap_content&quot;<br \/>\n        android:layout_height=&quot;wrap_content&quot;<br \/>\n        android:layout_marginBottom=&quot;8dp&quot;<br \/>\n        android:layout_marginTop=&quot;33dp&quot; &gt;<\/p>\n<p>        &lt;Button<br \/>\n            android:id=&quot;@+id\/exitBtn0&quot;<br \/>\n            android:layout_width=&quot;110dp&quot;<br \/>\n            android:layout_height=&quot;wrap_content&quot;<br \/>\n            android:background=&quot;@drawable\/btn_style_green&quot;<br \/>\n            android:gravity=&quot;center&quot;<br \/>\n            android:onClick=&quot;exitbutton0&quot;<br \/>\n            android:text=&quot;\u662f&quot;<br \/>\n            android:textColor=&quot;#fff&quot;<br \/>\n            android:textSize=&quot;16sp&quot; \/&gt;<\/p>\n<p>        &lt;Button<br \/>\n            android:id=&quot;@+id\/exitBtn1&quot;<br \/>\n            android:layout_width=&quot;110dp&quot;<br \/>\n            android:layout_height=&quot;wrap_content&quot;<br \/>\n            android:layout_marginLeft=&quot;10dp&quot;<br \/>\n            android:background=&quot;@drawable\/btn_style_white&quot;<br \/>\n            android:gravity=&quot;center&quot;<br \/>\n            android:onClick=&quot;exitbutton1&quot;<br \/>\n            android:text=&quot;\u5426&quot;<br \/>\n            android:textColor=&quot;#333&quot;<br \/>\n            android:textSize=&quot;16sp&quot; \/&gt;<br \/>\n    &lt;\/LinearLayout&gt;<\/p>\n<p>&lt;\/LinearLayout&gt;<br \/>\n[\/java]<\/p>\n<p>2.\u5b9e\u4f8b\u5316<\/p>\n<p>[java] Dialog weixinDialog = new MyWeixinDialog(this);[\/java]<\/p>\n<p>3.\u663e\u793a<\/p>\n<p>[java]  weixinDialog.show();[\/java]<\/p>\n<p><span style=\"color: #993300;\"><strong>\u4e94\u3001\u5bf9\u8bdd\u6846\u4e0eActivity\u7ed1\u5b9a<\/strong><\/span><\/p>\n<p>Activities\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7ba1\u7406\u7684\u521b\u5efa\u3001\u4fdd\u5b58\u3001\u56de\u590d\u7684\u5bf9\u8bdd\u6846\u673a\u5236\uff0c\u4f8b\u5982onCreateDialog(int),onPrepareDialog(int,Dialog),showDialog(int),dismissDialog(int)\u7b49\u65b9\u6cd5\uff0c\u5982\u679c\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u7684\u8bdd\uff0cActivity\u5c06\u901a\u8fc7getOwnerActivity()\u65b9\u6cd5\u8fd4\u56de\u8be5Activity\u7ba1\u7406\u7684\u5bf9\u8bdd\u6846(dialog).<\/p>\n<p>onCreateDialog(int):\u5f53\u4f60\u4f7f\u7528\u8fd9\u4e2a\u56de\u8c03\u51fd\u6570\u65f6\uff0cAndroid\u7cfb\u7edf\u4f1a\u6709\u6548\u7684\u8bbe\u7f6e\u8fd9\u4e2aActivity\u4e3a\u6bcf\u4e2a\u5bf9\u8bdd\u6846\u7684\u6240\u6709\u8005\uff0c\u4ece\u800c\u81ea\u52a8\u7ba1\u7406\u6bcf\u4e2a\u5bf9\u8bdd\u6846\u7684\u72b6\u6001\u5e76\u6302\u9760\u5230Activity\u4e0a\u3002\u8fd9\u6837\uff0c\u6bcf\u4e2a\u5bf9\u8bdd\u6846\u7ee7\u627f\u8fd9\u4e2aActivity\u7684\u7279\u5b9a\u5c5e\u6027\u3002\u6bd4\u5982\uff0c\u5f53\u4e00\u4e2a\u5bf9\u8bdd\u6846\u6253\u5f00\u65f6\uff0c\u83dc\u5355\u952e\u663e\u793a\u4e3a\u8fd9\u4e2aActivity\u5b9a\u4e49\u7684\u9009\u9879\u83dc\u5355\uff0c\u97f3\u91cf\u952e\u4fee\u6539Activity\u4f7f\u7528\u7684\u97f3\u9891\u6d41\u3002<\/p>\n<p>showDialog(int):\u5f53\u4f60\u60f3\u8981\u663e\u793a\u4e00\u4e2a\u5bf9\u8bdd\u6846\u65f6\uff0c\u8c03\u7528showDialog(intid)\u65b9\u6cd5\u5e76\u4f20\u9012\u4e00\u4e2a\u552f\u4e00\u6807\u8bc6\u8fd9\u4e2a\u5bf9\u8bdd\u6846\u7684\u6574\u6570\u3002\u5f53\u5bf9\u8bdd\u6846\u7b2c\u4e00\u6b21\u88ab\u8bf7\u6c42\u65f6\uff0cAndroid\u4ece\u4f60\u7684Activity\u4e2d\u8c03\u7528onCreateDialog(intid)\uff0c\u4f60\u5e94\u8be5\u5728\u8fd9\u91cc\u521d\u59cb\u5316\u8fd9\u4e2a\u5bf9\u8bdd\u6846Dialog\u3002\u8fd9\u4e2a\u56de\u8c03\u65b9\u6cd5\u88ab\u4f20\u4ee5\u548cshowDialog(intid)\u76f8\u540c\u7684ID\u3002\u5f53\u4f60\u521b\u5efa\u8fd9\u4e2a\u5bf9\u8bdd\u6846\u540e\uff0c\u5728Activity\u7684\u6700\u540e\u8fd4\u56de\u8fd9\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>onPrepareDialog(int,Dialog):\u5728\u5bf9\u8bdd\u6846\u88ab\u663e\u793a\u4e4b\u524d\uff0cAndroid\u8fd8\u8c03\u7528\u4e86\u53ef\u9009\u7684\u56de\u8c03\u51fd\u6570onPrepareDialog(intid,Dialog).\u5982\u679c\u4f60\u60f3\u5728\u6bcf\u4e00\u6b21\u5bf9\u8bdd\u6846\u88ab\u6253\u5f00\u65f6\u6539\u53d8\u5b83\u7684\u4efb\u4f55\u5c5e\u6027\uff0c\u4f60\u53ef\u4ee5\u5b9a\u4e49\u8fd9\u4e2a\u65b9\u6cd5\u3002\u8fd9\u4e2a\u65b9\u6cd5\u5728\u6bcf\u6b21\u6253\u5f00\u5bf9\u8bdd\u6846\u65f6\u88ab\u8c03\u7528\uff0c\u800conCreateDialog(int)\u4ec5\u5728\u5bf9\u8bdd\u6846\u7b2c\u4e00\u6b21\u6253\u5f00\u65f6\u88ab\u8c03\u7528\u3002\u5982\u679c\u4f60\u4e0d\u5b9a\u4e49onPrepareDialog()\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5bf9\u8bdd\u6846\u5c06\u4fdd\u6301\u548c\u4e0a\u6b21\u6253\u5f00\u65f6\u4e00\u6837\u3002\u8fd9\u4e2a\u65b9\u6cd5\u4e5f\u88ab\u4f20\u9012\u4ee5\u5bf9\u8bdd\u6846\u7684ID\uff0c\u548c\u5728onCreateDialog()\u4e2d\u521b\u5efa\u7684\u5bf9\u8bdd\u6846\u5bf9\u8c61\u3002<\/p>\n<p>dismissDialog(int):\u5f53\u4f60\u51c6\u5907\u5173\u95ed\u5bf9\u8bdd\u6846\u65f6\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u5bf9\u8fd9\u4e2a\u5bf9\u8bdd\u6846\u8c03\u7528dismiss()\u6765\u6d88\u9664\u5b83\u3002\u5982\u679c\u9700\u8981\uff0c\u4f60\u8fd8\u53ef\u4ee5\u4ece\u8fd9\u4e2aActivity\u4e2d\u8c03\u7528dismissDialog(intid)\u65b9\u6cd5\uff0c\u8fd9\u5b9e\u9645\u4e0a\u5c06\u4e3a\u4f60\u5bf9\u8fd9\u4e2a\u5bf9\u8bdd\u6846\u8c03\u7528dismiss()\u65b9\u6cd5\u3002\u5982\u679c\u4f60\u60f3\u4f7f\u7528onCreateDialog(intid)\u65b9\u6cd5\u6765\u7ba1\u7406\u4f60\u5bf9\u8bdd\u6846\u7684\u72b6\u6001\uff08\u5c31\u5982\u540c\u5728\u524d\u9762\u7684\u7ae0\u8282\u8ba8\u8bba\u7684\u90a3\u6837\uff09\uff0c\u7136\u540e\u6bcf\u6b21\u4f60\u7684\u5bf9\u8bdd\u6846\u6d88\u9664\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u5bf9\u8bdd\u6846\u5bf9\u8c61\u7684\u72b6\u6001\u5c06\u7531\u8be5Activity\u4fdd\u7559\u3002\u5982\u679c\u4f60\u51b3\u5b9a\u4e0d\u518d\u9700\u8981\u8fd9\u4e2a\u5bf9\u8c61\u6216\u8005\u6e05\u9664\u8be5\u72b6\u6001\u662f\u91cd\u8981\u7684\uff0c\u90a3\u4e48\u4f60\u5e94\u8be5\u8c03\u7528removeDialog(intid)\u3002\u8fd9\u5c06\u5220\u9664\u4efb\u4f55\u5185\u90e8\u5bf9\u8c61\u5f15\u7528\u800c\u4e14\u5982\u679c\u8fd9\u4e2a\u5bf9\u8bdd\u6846\u6b63\u5728\u663e\u793a\uff0c\u5b83\u5c06\u88ab\u6d88\u9664\u3002<br \/>\n\u5728Activity\u7c7b\u4e2d\u8c03\u7528showDialog(1)\u65b9\u6cd5\u65f6\u7cfb\u7edf\u4f1a\u81ea\u52a8\u8c03\u7528onCreateDialog\u65b9\u6cd5\u521b\u5efaDialog\u663e\u793a<br \/>\n\u6211\u4eec\u53ea\u9700\u5728onCreateDialog\u65b9\u6cd5\u4e2d\u5b9e\u73b0\u8fd4\u56deDialog.<\/p>\n<p>[java]<br \/>\n showDialog(1);<br \/>\n[\/java]<\/p>\n<p>[java]<br \/>\n@Override<br \/>\n  protected Dialog onCreateDialog(int id) {<br \/>\n      switch(id){<br \/>\n      case 1:<br \/>\n        return new DatePickerDialog();<br \/>\n      }<br \/>\n  }[\/java]<\/p>\n<p><span style=\"color: #993300;\"><strong>\u516d\u3001\u5bf9\u8bdd\u6846\u6700\u4f18\u5b9e\u73b0\u65b9\u5f0f\u91c7\u7528DialogFragment\u5b9e\u73b0\u5982\u4e0b:<\/strong><\/span><\/p>\n<p>[java] \/**<br \/>\n * \u8c03\u7528\u65b9\u6cd5:<br \/>\n * MyAlertDialogFragment dialogFragment = new MyAlertDialogFragment();<br \/>\n * dialogFragment.show(getFragmentManager(), &quot;dialog&quot;);<br \/>\n *<br \/>\n *\/<br \/>\npublic class MyAlertDialogFragment extends DialogFragment {<br \/>\n\t@Override<br \/>\n\tpublic void onCreate(Bundle savedInstanceState) {<br \/>\n\t\tsuper.onCreate(savedInstanceState);<br \/>\n\t}<br \/>\n\t\/\/\u6cd5\u4e8c<br \/>\n\t @Override<br \/>\n     public View onCreateView(LayoutInflater inflater, ViewGroup container,<br \/>\n            Bundle savedInstanceState) {<\/p>\n<p>        View v = inflater.inflate(R.layout.article_view, container, false);<br \/>\n        View tv = v.findViewById(R.id.article);<br \/>\n        ((TextView)tv).setText(mTitle);<br \/>\n        return v;<br \/>\n    }<\/p>\n<p>\t\/\/ \u6cd5\u4e00<br \/>\n\t@Override<br \/>\n\tpublic Dialog onCreateDialog(Bundle savedInstanceState) {<\/p>\n<p>\t\treturn new AlertDialog.Builder(getActivity())<br \/>\n\t\t\t\t.setIcon(android.R.drawable.ic_dialog_alert)<br \/>\n\t\t\t\t.setTitle(&quot;\u63d0\u793a\u6846&quot;)<br \/>\n\t\t\t\t.setPositiveButton(android.R.string.ok,<br \/>\n\t\t\t\t\t\tnew DialogInterface.OnClickListener() {<br \/>\n\t\t\t\t\t\t\tpublic void onClick(DialogInterface dialog,<br \/>\n\t\t\t\t\t\t\t\t\tint whichButton) {<br \/>\n\t\t\t\t\t\t\t\tToast.makeText(getActivity(), &quot;\u786e\u5b9a&quot;,<br \/>\n\t\t\t\t\t\t\t\t\t\tToast.LENGTH_SHORT).show();<br \/>\n\t\t\t\t\t\t\t}<br \/>\n\t\t\t\t\t\t})<br \/>\n\t\t\t\t.setNegativeButton(android.R.string.cancel,<br \/>\n\t\t\t\t\t\tnew DialogInterface.OnClickListener() {<br \/>\n\t\t\t\t\t\t\tpublic void onClick(DialogInterface dialog,<br \/>\n\t\t\t\t\t\t\t\t\tint whichButton) {<br \/>\n\t\t\t\t\t\t\t\tToast.makeText(getActivity(), &quot;\u53d6\u6d88&quot;,<br \/>\n\t\t\t\t\t\t\t\t\t\tToast.LENGTH_SHORT).show();<br \/>\n\t\t\t\t\t\t\t}<br \/>\n\t\t\t\t\t\t}).create();<br \/>\n\t}<br \/>\n}[\/java]<\/p>\n<p>\u8c03\u7528<\/p>\n<p>[java]MyAlertDialogFragment dialogFragment = new MyAlertDialogFragment();<br \/>\ndialogFragment.show(getFragmentManager(), &quot;dialog&quot;);[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u8bdd\u6846\u987e\u540d\u601d\u4e49\u5373\u4eba\u4e0e\u5e94\u7528\u7cfb\u7edf\u5bf9\u8bdd\u4ea4\u4e92\u7684\u7a97\u53e3\uff0c\u767b\u5f55\u64cd\u4f5c\u4e2d\u68c0\u67e5\u7528\u6237\u4fe1\u606f\u8f93\u5165\u60c5\u51b5\u53ef\u4ee5\u7528\u5bf9\u8bdd\u6846\u8fdb\u884c\u63d0\u793a\uff0c\u9000\u51fa\u5e94\u7528\u65f6\u4e5f\u53ef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/825"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=825"}],"version-history":[{"count":16,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/825\/revisions"}],"predecessor-version":[{"id":1609,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/825\/revisions\/1609"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}