{"id":1205,"date":"2012-01-14T20:33:18","date_gmt":"2012-01-14T12:33:18","guid":{"rendered":"http:\/\/leybreeze.com\/blog\/?p=1205"},"modified":"2012-08-01T09:08:17","modified_gmt":"2012-08-01T01:08:17","slug":"android%e4%b8%ad%e5%90%84%e7%a7%8d%e7%b1%bb%e5%9e%8b%e5%af%b9%e8%af%9d%e6%a1%86%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/leybreeze.com\/blog\/?p=1205","title":{"rendered":"Android\u4e2d\u5404\u79cd\u7c7b\u578b\u5bf9\u8bdd\u6846\u7684\u4f7f\u7528"},"content":{"rendered":"<p><strong>\u53ea\u6709\u6807\u9898\u548c\u6587\u672c\u5185\u5bb9\u7b80\u5355\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">AlertDialog.Builder dlg;\r\ndlg = new AlertDialog.Builder(AlertDialogSample.this);\r\ndlg.setTitle(\"\u7b80\u5355\u5bf9\u8bdd\u6846\");\r\ndlg.setMessage(\"Hello, AlertDialog!\");\r\ndlg.show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u53ea\u6709\u6807\u9898\u4e0e\u5185\u5bb9\u7684\u7b80\u5355\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u4e00\u4e2a\u6309\u94ae\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">AlertDialog.Builder dlg;\r\ndlg = new AlertDialog.Builder(AlertDialogSample.this);\r\ndlg.setTitle(\"\u4f46\u6309\u94ae\u5bf9\u8bdd\u6846\");\r\ndlg.setMessage(\"Hello, AlertDialog!\");\r\ndlg.setPositiveButton(\"OK\", null)\r\ndlg.show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u4e00\u4e2a\u6309\u94ae\u7684\u7b80\u5355\u5bf9\u8bdd\u6846\uff0c\u70b9\u51fb\u8be5\u6309\u94ae\u5c06\u5173\u95ed\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u4e24\u4e2a\u6309\u94ae\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">new AlertDialog.Builder(AlertDialogSample.this)\r\n.setTitle(\"\u63d0\u793a\")\r\n.setMessage(\"\u786e\u5b9a\u9000\u51fa?\")\r\n.setCancelable(false)\r\n.setPositiveButton(\"Yes\", new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int id) {\r\n\t\tAlertDialogSample.this.finish();\r\n      }\r\n  })\r\n  .setNegativeButton(\"No\", new DialogInterface.OnClickListener() {\r\n      public void onClick(DialogInterface dialog, int id) {\r\n           dialog.cancel();\r\n      }\r\n  })\r\n  .show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u4e24\u4e2a\u6309\u94ae\u7684\u7b80\u5355\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u4e09\u4e2a\u6309\u94ae\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">new AlertDialog.Builder(this)\r\n.setIcon(R.drawable.icon)\r\n.setTitle(\"YES\/NO Dialog\")\r\n.setMessage(\"Is this a pen ?\")\r\n.setPositiveButton(\"Yes\", new DialogInterface.OnClickListener() {\r\n    public void onClick(DialogInterface dialog, int whichButton) {\r\n        \/* \u8fd9\u91cc\u662fYES\u7684\u5904\u7406 *\/\r\n    }\r\n})\r\n.setNeutralButton(\"No\", new DialogInterface.OnClickListener() {\r\n    public void onClick(DialogInterface dialog, int whichButton) {\r\n        \/* \u8fd9\u91cc\u662fNO\u7684\u5904\u7406 *\/\r\n    }\r\n})\r\n.setNegativeButton(\"Cancel\", new DialogInterface.OnClickListener() {\r\n    public void onClick(DialogInterface dialog, int whichButton) {\r\n        \/* \u8fd9\u91cc\u662fCancel\u7684\u5904\u7406 *\/\r\n    }\r\n})\r\n.show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u4e09\u4e2a\u6309\u94ae\u7684\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u4e00\u4e2a\u5355\u9009\u5217\u8868\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">final CharSequence[] items = {\"\u7ea2\", \"\u7eff\", \"\u84dd\"};\r\nnew AlertDialog.Builder(AlertDialogSample.this)\r\n.setTitle(\"\u9009\u62e9\u989c\u8272\")\r\n.setItems(items, new DialogInterface.OnClickListener() {\r\n    public void onClick(DialogInterface dialog, int item) {\r\n    \tbutton.setText(String.format(\"\u9009\u62e9\u4e86%s\u3002\",items[item]));\r\n    }\r\n})\r\n.show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u4e00\u4e2a\u5355\u9009\u5217\u8868\u7684\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u4e00\u4e2a\u591a\u9009\u5217\u8868\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">final CharSequence[] items = {\"\u7ea2\", \"\u7eff\", \"\u84dd\"};\r\nfinal boolean[] checkedItems = {true, false, true};\r\nnew AlertDialog.Builder(AlertDialogSample.this)\r\n.setTitle(\"Pick a color\")\r\n.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int which,boolean isChecked) {\r\n\t\tcheckedItems[which]=isChecked;\r\n\t}\r\n})\r\n.setPositiveButton(\"Ok\", new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int id) {\r\n\t\tfor(int i=0;i<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u4e00\u4e2a\u591a\u9009\u5217\u8868\u7684\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709Radio Button\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">String[] str_items = {\"\u7ea2\",\"\u7eff\",\"\u84dd\"};\r\nString result_item = \"\";\r\n\r\nint def_index = 2;\r\nresult_item = str_items[def_index];\r\n\r\nnew AlertDialog.Builder(this)\r\n.setIcon(R.drawable.icon)\r\n.setTitle(\"\u9009\u62e9\u989c\u8272\")\r\n.setSingleChoiceItems(str_items, def_index,\r\nnew DialogInterface.OnClickListener(){\r\n\tpublic void onClick(DialogInterface dialog, int which) {\r\n\t\tresult_item = str_items[which];\r\n\t}\r\n})\r\n.setPositiveButton(\"OK\", new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int whichButton) {\r\n\t\tnew AlertDialog.Builder(AlertDialogText.this)\r\n        \t\t.setTitle(\"color=\" + result_item)\r\n        \t\t.show();\r\n\t}\r\n})\r\n.setNegativeButton(\"Cancel\", new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int whichButton) {\r\n\t\tnew AlertDialog.Builder(AlertDialogText.this)\r\n        \t\t.setTitle(\"Canceled\")\r\n        \t\t.show();\r\n\t}\r\n})\r\n.show();\r\n}<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709Radio Button\u7684\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u81ea\u5b9a\u4e49\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">LayoutInflater inflater = LayoutInflater.from(AlertDialogSample.this);\r\nView layout = inflater.inflate(R.layout.custom_dialog,(ViewGroup) findViewById(R.id.layout_root));\r\nTextView text = (TextView) layout.findViewById(R.id.text);\r\ntext.setText(\"\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u8bdd\u6846!\");\r\nImageView image = (ImageView) layout.findViewById(R.id.image);\r\nimage.setImageResource(R.drawable.icon);\r\nnew AlertDialog.Builder(AlertDialogSample.this)\r\n.setView(layout)\r\n.show();<\/pre>\n<p>\u5176\u4e2d\u4ee3\u7801\u7684\u7b2c\u4e00\u884c\u4e5f\u53ef\u4ee5\u7528\u4ee5\u4e0b\u4e24\u79cd\u4e4b\u4e00\u66ff\u6362\u6765\u5c55\u5f00\u5e03\u5c40\uff1a<\/p>\n<pre lang=\"JAVA\">LayoutInflater inflater = (LayoutInflater) getApplicationContext()\r\n.getSystemService(Context.LAYOUT_INFLATER_SERVICE);<\/pre>\n<pre lang=\"JAVA\">LayoutInflater inflater = SampleDialog1.this.getLayoutInflater();<\/pre>\n<p>\u76f4\u63a5\u901a\u8fc7\u4ee3\u7801\u751f\u6210\u5e03\u5c40\u4e5f\u53ef\u4ee5\uff0c\u867d\u7136\u4e0d\u662f\u592a\u63a8\u8350\u8fd9\u79cd\u505a\u6cd5\uff1a<\/p>\n<pre lang=\"JAVA\">LinearLayout layout = new LinearLayout(AlertDialogSample.this);\r\n\r\nImageView image = new ImageView(AlertDialogSample.this);\r\nimage.setImageResource(R.drawable.icon);\r\nlayout.addView(image);\r\n\r\nTextView text = new TextView(AlertDialogSample.this);\r\ntext.setText(\"\u8fd9\u662f\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5bf9\u8bdd\u6846!\");\r\nlayout.addView(text);\r\n\r\nnew AlertDialog.Builder(AlertDialogSample.this)\r\n\t.setView(layout)\r\n\t.show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u81ea\u5b9a\u4e49\u89c6\u56fe\u7684\u5bf9\u8bdd\u6846\uff0c\u5176\u4e2d\u7528\u5230\u4e86\u81ea\u5b9a\u4e49\u7684xml\u5e03\u5c40\u6587\u4ef6custom_dialog.xml\u3002<\/p>\n<p><strong>\u7efc\u5408\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">final CharSequence[] items = {\"\u7ea2\", \"\u7eff\", \"\u84dd\"};\r\nfinal boolean[] checkedItems = {true, false, true};\r\nTextView tv=new TextView(AlertDialogSample.this);\r\ntv.setText(\"setView\");\r\nnew AlertDialog.Builder(AlertDialogSample.this)\r\n.setIcon(R.drawable.icon)\r\n.setTitle(\"\u7efc\u5408\u5bf9\u8bdd\u6846\")\r\n.setView(tv)\r\n.setMessage(\"Hello, World!\")\r\n.setItems(items, new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int item) { }\r\n})\r\n.setSingleChoiceItems(items, 1, new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int item) { }\r\n})\r\n.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int which,\r\n\t\t\tboolean isChecked) { }\r\n        \t})\r\n.setPositiveButton(\"Yes\", new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int id) {\r\n\t\tAlertDialogSample.this.finish();\r\n      }\r\n})\r\n.setNegativeButton(\"No\", new DialogInterface.OnClickListener() {\r\n      public void onClick(DialogInterface dialog, int id) {\r\n           dialog.cancel();\r\n      }\r\n})\r\n.show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u7efc\u5408\u4f7f\u7528\u4e0a\u8ff0\u51e0\u79cd\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u6587\u672c\u6846\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">et = new EditText(this);\r\nnew AlertDialog.Builder(this)\r\n.setIcon(R.drawable.icon)\r\n.setTitle(\"\u5403\u4ec0\u4e48\uff1f\")\r\n.setView(et)\r\n.setPositiveButton(\"OK\", new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int whichButton) {\r\n\t\tnew AlertDialog.Builder(AlertDialogText.this)\r\n\t\t\t.setTitle(\"\u5403\uff1a \" + edtInput.getText().toString())\r\n\t\t\t.show();\r\n}\r\n})\r\n.setNegativeButton(\"Cancel\", new DialogInterface.OnClickListener() {\r\n\tpublic void onClick(DialogInterface dialog, int whichButton) {\r\n}\r\n})\r\n.show();<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u80fd\u591f\u8f93\u5165\u6587\u672c\u7684\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u8fdb\u5ea6\u6761\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">public class AlertDialogText extends Activity {\r\n\tpublic ProgressDialog progDlg;\r\n\tpublic Handler progHandler;\r\n    @Override\r\n    public void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.main);\r\n        \/\/ \u751f\u6210\u5bf9\u8bdd\u6846\r\n        progDlg = new ProgressDialog(this);\r\n        progDlg.setIcon(R.drawable.icon);\r\n        progDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);\r\n        progDlg.setMax(100);\r\n        progDlg.setButton(\"Cancel\", new DialogInterface.OnClickListener(){\r\n        \tpublic void onClick(DialogInterface dialog, int whichButton) {\r\n        \t\tprogDlg.cancel();\r\n        \t}\r\n        });\r\n        \/\/ \u4f7f\u8fdb\u5ea6\u6761\u524d\u8fdb\r\n        progHandler = new Handler() {\r\n        \tpublic void handleMessage(Message msg) {\r\n        \t\tsuper.handleMessage(msg);\r\n        \t\tint v = progDlg.getProgress();\r\n        \t\tif (v &gt;= 100) {\r\n        \t\t\tprogDlg.dismiss();\r\n        \t\t} else {\r\n        \t\t\tprogDlg.incrementProgressBy(1);\r\n        \t\t\tprogHandler.sendEmptyMessageDelayed(0, 100);\r\n        \t\t}\r\n        \t}\r\n        };\r\n        \/\/ \u6309\u4e0bOk\u6309\u94ae\u7684\u53cd\u5e94\r\n        new AlertDialog.Builder(this)\r\n        .setTitle(\"PUSH START BUTTON\")\r\n        .setPositiveButton(\"START\", new DialogInterface.OnClickListener() {\r\n        \tpublic void onClick(DialogInterface dialog, int whichButton) {\r\n        \t\tprogDlg.show();\r\n        \t\tprogHandler.sendEmptyMessage(0);\r\n        \t}\r\n        })\r\n        .show();\r\n   }\r\n}<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u8fdb\u5ea6\u6761\u7684\u5bf9\u8bdd\u6846\u3002<\/p>\n<p><strong>\u542b\u6709\u6b63\u5728\u8fd0\u884c\u6807\u8bc6\u7684\u5bf9\u8bdd\u6846<\/strong><\/p>\n<p>\u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a<\/p>\n<pre lang=\"JAVA\">ProgressDialog dialog = ProgressDialog.show(AlertDialogSample.this, \"\",\"\u8f7d\u5165\u4e2d\uff0c\u8bf7\u7a0d\u5019\", true);<\/pre>\n<p>\u8bf4\u660e\uff1a\u751f\u6210\u4e00\u4e2a\u542b\u6709\u4e00\u4e2a\u6b63\u5728\u8fd0\u884c\u7684\u5faa\u73af\u65cb\u8f6c\u6807\u8bc6\u7684\u7b80\u5355\u5bf9\u8bdd\u6846\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53ea\u6709\u6807\u9898\u548c\u6587\u672c\u5185\u5bb9\u7b80\u5355\u5bf9\u8bdd\u6846 \u4ee3\u7801\u4e0e\u8303\u4f8b\uff1a AlertDialog.Builder dlg; dlg = ne &hellip; <a href=\"http:\/\/leybreeze.com\/blog\/?p=1205\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cAndroid\u4e2d\u5404\u79cd\u7c7b\u578b\u5bf9\u8bdd\u6846\u7684\u4f7f\u7528\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[5,101],"_links":{"self":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1205"}],"collection":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1205"}],"version-history":[{"count":3,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1205\/revisions"}],"predecessor-version":[{"id":1207,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1205\/revisions\/1207"}],"wp:attachment":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1205"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}