{"id":1048,"date":"2021-01-07T10:52:41","date_gmt":"2021-01-07T02:52:41","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1048"},"modified":"2021-01-07T11:10:08","modified_gmt":"2021-01-07T03:10:08","slug":"%e5%89%b5%e5%bb%bamenu-options-menu%e3%80%81context-menu%e3%80%81popup-menu","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/%e5%89%b5%e5%bb%bamenu-options-menu%e3%80%81context-menu%e3%80%81popup-menu\/","title":{"rendered":"\u5275\u5efaMenu Options Menu\u3001Context Menu\u3001Popup Menu"},"content":{"rendered":"<h3>1.\u5275\u5efaMenu\u9078\u55ae<\/h3>\n<h5>\u53f3\u9375 res > New > Android Resource Directory > Resource type \u9078\u64c7 menu<\/h5>\n<h5>\u53f3\u9375 menu > New > Menu Resource File<\/h5>\n<pre><code class=\"language-XML line-numbers\">&lt;menu xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"&gt;\n    &lt;item android:id=\"@+id\/developer\" android:title=\"\u958b\u767c\" app:showAsAction=\"ifRoom\"\/&gt;\n    &lt;item android:id=\"@+id\/setting\" android:title=\"\u8a2d\u5b9a\" app:showAsAction=\"ifRoom\"\/&gt;\n    &lt;item android:id=\"@+id\/save\" android:title=\"\u4fdd\u5b58\"\/&gt;\n    &lt;item android:title=\"\u66f4\u591a\u64cd\u4f5c\"&gt;\n        &lt;menu&gt;\n            &lt;item android:id=\"@+id\/edit\" android:title=\"\u7de8\u8f2f\" android:icon=\"@mipmap\/ic_launcher\"\/&gt;\n            &lt;item android:id=\"@+id\/share\" android:title=\"\u5206\u4eab\"\/&gt;\n        &lt;\/menu&gt;\n    &lt;\/item&gt;\n&lt;\/menu&gt;\n<\/code><\/pre>\n<pre><code class=\"line-numbers\">1. android:id: \u552f\u4e00\u6a19\u793a\n2. android:icon\uff1a\u5716\u793a\n3. android:title\uff1a\u6a19\u984c\n4. android:showAsAction\uff1a\u986f\u793a\u65b9\u5f0f\n- always\uff1a\u6c38\u9060\u986f\u793a\u5728menu\u5916\n- ifRoom\uff1a\u6709\u7a7a\u9593\u6703\u986f\u793a\u5728menu\u5916,\u5426\u5247\u5230menu\u4e2d\n- withText\uff1a\u53ea\u6703\u986f\u793a\u6a19\u984c\n- never\uff1a\u6c38\u9060\u986f\u793a\u5728menu\u4e2d\n<\/code><\/pre>\n<h3>2.Options Menu<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u986f\u793amenu\noverride fun onCreateOptionsMenu(menu: Menu?): Boolean {\n    menuInflater.inflate(R.menu.option, menu)\n    return super.onCreateOptionsMenu(menu)\n}\n\n\/\/menu\u76e3\u807d\noverride fun onOptionsItemSelected(item: MenuItem): Boolean {\n    when (item.itemId) {\n        R.id.developer -&gt; {\n            Toast.makeText(this, \"\u958b\u767c\", Toast.LENGTH_SHORT).show()\n        }\n        R.id.setting -&gt; {\n            Toast.makeText(this, \"\u8a2d\u5b9a\", Toast.LENGTH_SHORT).show()\n        }\n        R.id.save -&gt; {\n            Toast.makeText(this, \"\u4fdd\u5b58\", Toast.LENGTH_SHORT).show()\n        }\n        R.id.edit -&gt; {\n            Toast.makeText(this, \"\u7de8\u8f2f\", Toast.LENGTH_SHORT).show()\n        }\n        R.id.share -&gt; {\n            Toast.makeText(this, \"\u5206\u4eab\", Toast.LENGTH_SHORT).show()\n        }\n    }\n\n    return super.onOptionsItemSelected(item)\n}\n<\/code><\/pre>\n<h3>3.Context Menu<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">\/\/\u8a3b\u518a\u9577\u6309\u9ede\u64ca\u986f\u793amenu\noverride fun onCreate(savedInstanceState: Bundle?) {\n    super.onCreate(savedInstanceState)\n    setContentView(R.layout.activity_main)\n\n    registerForContextMenu(findViewById(R.id.contextMenu))\n}\n\n\/\/menu\u76e3\u807d\noverride fun onContextItemSelected(item: MenuItem): Boolean {\n    Toast.makeText(this, \"\u60a8\u9078\u64c7\u7684\u662f\" + item.title, Toast.LENGTH_SHORT).show();\n    return super.onContextItemSelected(item)\n}\n\n\/\/\u986f\u793amenu\noverride fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {\n    menuInflater.inflate(R.menu.option, menu)\n    super.onCreateContextMenu(menu, v, menuInfo)\n}\n<\/code><\/pre>\n<h3>4.Popup Menu<\/h3>\n<pre><code class=\"language-Kotlin line-numbers\">fun popMenu(view: View) {\n    val menu = PopupMenu(this , view)\n\n    menu.menuInflater.inflate(R.menu.option, menu.menu)\n\n    \/\/menu\u76e3\u807d\n    menu.setOnMenuItemClickListener {\n        Toast.makeText(applicationContext, it.title, Toast.LENGTH_SHORT).show()\n        super.onOptionsItemSelected(it);\n    }\n\n    \/\/\u986f\u793amenu\n    menu.show()\n}\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u5275\u5efaMenu\u9078\u55ae \u53f3\u9375 res > New > Android Resource Directory >  &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,15,164],"class_list":["post-1048","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-kotlin","tag-menu"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1048","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1048"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1048\/revisions"}],"predecessor-version":[{"id":1053,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1048\/revisions\/1053"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1048"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}