{"id":1266,"date":"2022-08-29T19:57:22","date_gmt":"2022-08-29T11:57:22","guid":{"rendered":"https:\/\/fireinsect.top\/?p=1266"},"modified":"2024-03-21T17:02:36","modified_gmt":"2024-03-21T09:02:36","slug":"kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0%ef%bc%884%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fireinsect.top\/index.php\/2022\/08\/29\/kotlin%e5%ae%89%e5%8d%93%e5%bc%80%e5%8f%91%e5%ad%a6%e4%b9%a0%ef%bc%884%ef%bc%89\/","title":{"rendered":"Kotlin\u5b89\u5353\u5f00\u53d1\u5b66\u4e60\uff084\uff09"},"content":{"rendered":"\n<p>\u524d\u9762\u6211\u4eec\u5b66\u4e60\u4e86Kotlin \u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u4f46\u6211\u4eec\u6700\u7ec8\u7684\u76ee\u7684\u662f\u4f5c\u4e3a\u5b89\u5353\u5f00\u53d1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5b66\u4e60\u5b89\u5353\u7684\u9875\u9762\u5f00\u53d1\u77e5\u8bc6\u3002<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">Activity<\/p>\n\n\n\n<p>Activity\u662f\u5b89\u5353\u5f00\u53d1\u7684\u6700\u91cd\u8981\u7ec4\u6210\u4e4b\u4e00\uff0c\u5b83\u662f\u4e00\u79cd\u53ef\u4ee5\u5305\u542b\u7528\u6237\u754c\u9762\u7684\u7ec4\u4ef6\uff0c\u4e3b\u8981\u7528\u4e8e\u548c\u7528\u6237\u8fdb\u884c\u4ea4\u4e92\u3002(\u4e2a\u4eba\u770b\u6765\u6682\u65f6\u7c7b\u4f3c\u4e8eHtml\u4e2d\u4f7f\u7528\u7684js)<\/p>\n\n\n\n<p>\u5728Android Studio\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u4f9b\u53f3\u952e->new->Activity\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Activity\u3002\u6b64\u65f6\u9664\u4e86\u6700\u57fa\u7840\u7684\u6837\u5f0f\u5916\uff0c\u901a\u8fc7\u52fe\u9009 Generate Layout File \u6765\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7ed1\u5b9a\u7684\u5e03\u5c40\u6587\u4ef6\uff0c\u52fe\u9009 Lancher Activity \u8868\u793a\u5c06\u5f53\u524dActivity\u5f53\u4f5c\u9879\u76ee\u4e3bActivity\u3002<\/p>\n\n\n\n<p><em>\u6240\u6709\u7684Activity\u90fd\u8981\u5728AndroidManifest.xml\u4e2d\u8fdb\u884c\u6ce8\u518c\u624d\u80fd\u751f\u6548\u5982\u4e0b\uff1a<\/em><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n          package=\"com.example.activitytest\">\n     &lt;application\n         android:allowBackup=\"true\"\n         android:icon=\"@mipmap\/ic_launcher\"\n         android:label=\"@string\/app_name\"\n         android:roundIcon=\"@mipmap\/ic_launcher_round\"\n         android:supportsRtl=\"true\"\n         android:theme=\"@style\/AppTheme\">\n         &lt;activity android:name=\".FirstActivity\"\n         &lt;\/activity>\n      &lt;\/application>\n&lt;\/manifest><\/code><\/pre>\n\n\n\n<p>Activity\u7684\u6ce8\u518c\u58f0\u660e\u8981\u653e\u5728<code>&lt;application><\/code>\u6807\u7b7e\u5185,\u4f7f\u7528<code>android:name<\/code>\u6765\u6307\u5b9a\u5177\u4f53\u6ce8\u518c\u54ea\u4e00\u4e2aActivity\uff0c\u800cActivity\u7684\u4f4d\u7f6e\u901a\u8fc7package\u5c5e\u6027\u6307\u660e\u4e86\u3002<\/p>\n\n\n\n<p>\u901a\u8fc7\u5185\u90e8\u52a0\u5165<code>&lt;intent-filter><\/code>\u6807\u7b7e\u5e76\u5728\u8fd9\u4e2a\u6807\u7b7e\u5185\u6dfb\u52a0<code>&lt;actionandroid:name=\"android.intent.action.MAIN\"\/><\/code> \u548c <code>&lt;categoryandroid:name=\"android.intent.category.LAUNCHER\" \/><\/code>\u8fd9\u4e24\u53e5\u58f0\u660e\u5373\u53ef\u914d\u7f6e\u4e3bActivity\uff08\u5373\u6700\u5148\u542f\u52a8\u7684Activity\uff09<\/p>\n\n\n\n<p>\u9664\u6b64\u4e4b\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>android:label<\/code>\u6307\u5b9aActivity\u4e2d\u6807\u9898\u680f\u7684\u5185\u5bb9<\/p>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u7ed9\u4e3bActivity\u6307\u5b9a\u7684label\u4e0d\u4ec5\u4f1a\u6210\u4e3a\u6807\u9898\u680f\u4e2d\u7684\u5185\u5bb9\uff0c\u8fd8\u4f1a\u6210\u4e3a\u542f\u52a8\u5668\uff08Launcher\uff09\u4e2d\u5e94\u7528\u7a0b\u5e8f\u663e\u793a\u7684\u540d\u79f0\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n          package=\"com.example.activitytest\">\n     &lt;application\n         ...>\n         &lt;activity android:name=\".FirstActivity\"\n             android:label=\"This is FirstActivity\">\n             &lt;intent-filter>\n                 &lt;action android:name=\"android.intent.action.MAIN\" \/>\n                 &lt;category android:name=\"android.intent.category.LAUNCHER\" \/>\n             &lt;\/intent-filter>\n         &lt;\/activity>\n      &lt;\/application>\n&lt;\/manifest><\/code><\/pre>\n\n\n\n<div style=\"height:16px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">Layout<\/p>\n\n\n\n<p>\u6b63\u5982Activity\u5bf9\u5e94\u4e86HTML\u4e2d\u7684js\uff0cAndroid\u901a\u8fc7\u521b\u5efaxml\u683c\u5f0f\u7684layout\u5e03\u5c40\u6587\u4ef6\u6765\u5bf9\u5e94HTML\u4e2d\u7684html\u3002<\/p>\n\n\n\n<p>\u5728\u521b\u5efa\u5e03\u5c40\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u9009\u62e9\u5f53\u524d\u5e03\u5c40\u7684\u6839\u5143\u7d20\uff08\u4e00\u822c\u4e3aLinearLayout\uff09\uff0c\u4e4b\u540e\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6839\u5143\u7d20\u5185\u90e8\u6dfb\u52a0\u65b0\u7684\u5143\u7d20\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6211\u4eec\u62ffButton\u6309\u94ae\u5143\u7d20\u6765\u4e3e\u4f8b\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>&lt;Button\/&gt;<\/code>\u6765\u521b\u5efa\u6309\u94ae\u5143\u7d20\uff0c\u540c\u65f6\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5185\u90e8\u5c5e\u6027\u6765\u5b9e\u73b0\u5c5e\u6027\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"xml\" class=\"language-xml\">&lt;Button\n android:id=\"@+id\/button1\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"wrap_content\"\n android:text=\"Button 1\"\n\/&gt;<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u6765\u5206\u6790\u4e00\u4e0b\u8fd9\u91cc\u7684\u51e0\u4e2a\u5c5e\u6027\u7684\u610f\u601d\uff1a<\/p>\n\n\n\n<p>\u9996\u5148 <code>android:id<\/code>\u8868\u793a\u8fd9\u4e2a\u5143\u7d20\u7684id\u5373\u8be5\u5143\u7d20\u7684\u552f\u4e00\u6807\u8bc6\u7b26\u3002\u4e0d\u8fc7\u6211\u4eec\u9700\u8981\u6ce8\u610f\u540e\u9762\u5c5e\u6027\u503c\u7684\u5199\u6cd5\u3002\u5728xml\u4e2d<code>@id\/button1<\/code>\u8868\u793a\u8d44\u6e90(id)\u5f15\u7528\uff0c\u4e0e\u4e4b\u76f8\u5bf9\u5e94\u7684\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2aid\uff0c\u5219\u9700\u8981\u52a0\u4e0a\u201c+\u201d,\u5373\u4e0a\u9762<code>@+id\/button1<\/code><\/p>\n\n\n\n<p>\u4e4b\u540e\u7684 <code>android:layout_width<\/code> \u548c <code>android:layout_height<\/code>\u5206\u522b\u6307\u5b9a\u4e86\u5f53\u524d\u5143\u7d20\u7684\u5bbd\u5ea6\u548c\u957f\u5ea6\u3002\u201cmatch_parent\u201d\u8868\u793a\u548c\u7236\u5143\u7d20\u76f8\u540c\u5927\u5c0f\u3002\u201cwrap_content\u201d\u8868\u793a\u5927\u5c0f\u53ea\u9700\u521a\u521a\u5305\u4f4f\u5185\u90e8\u5185\u5bb9\u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u6700\u540e<code>android:text<\/code>\u6307\u5b9a\u4e86\u5143\u7d20\u4e2d\u663e\u793a\u7684\u6587\u5b57\u5185\u5bb9\u3002<\/p>\n\n\n\n<div style=\"height:14px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<p class=\"has-vivid-red-color has-text-color has-large-font-size\">Toast<\/p>\n\n\n\n<p>Toast\u662fAndroid\u63d0\u4f9b\u7684\u4e00\u79cd\u975e\u5e38\u597d\u7684\u63d0\u9192\u65b9\u5f0f\uff0c\u5728\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u4f7f\u7528\u5b83\u5c06\u4e00\u4e9b\u77ed\u5c0f\u7684\u4fe1\u606f\u901a\u77e5\u7ed9\u7528\u6237\uff0c\u8fd9\u4e9b\u4fe1\u606f\u4f1a\u5728\u4e00\u6bb5\u65f6\u95f4\u540e\u81ea\u52a8\u6d88\u5931\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5360\u7528\u4efb\u4f55\u5c4f\u5e55\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u9700\u8981\u501f\u7531\u4e0a\u6587\u7684Button\u5b9a\u4e49\u4e00\u4e2a\u5f39\u51faToast\u7684\u89e6\u53d1\u70b9\u3002\u5728onCreate()\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">override fun onCreate(savedInstanceState: Bundle?) {\n     super.onCreate(savedInstanceState)\n     setContentView(R.layout.first_layout)\n     val button1: Button = findViewById(R.id.button1)\n     button1.setOnClickListener {\n         Toast.makeText(this, \"You clicked Button 1\", Toast.LENGTH_SHORT).show()\n     }\n}<\/code><\/pre>\n\n\n\n<p>\u5728Activity\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7findViewById()\u65b9\u6cd5\u83b7\u53d6\u5728\u5e03\u5c40\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u5143\u7d20\uff0c\u8fd9\u91cc\u6211\u4eec\u4f20\u5165R.id.button1\u6765\u5f97\u5230\u6309\u94ae\u7684\u5b9e\u4f8b\uff0c\u8fd9\u4e2a\u503c\u662f\u5728\ufb01rst_layout.xml\u4e2d\u901a\u8fc7android:id\u5c5e\u6027\u6307\u5b9a\u7684\u3002<\/p>\n\n\n\n<p>findViewById()\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2a\u7ee7\u627f\u81eaView\u7684\u6cdb\u578b\u5bf9\u8c61\uff0c\u56e0\u6b64Kotlin\u65e0\u6cd5\u81ea\u52a8\u63a8\u5bfc\u51fa\u5b83\u662f\u4e00\u4e2aButton\u8fd8\u662f\u5176\u4ed6\u63a7\u4ef6\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u5c06button1\u53d8\u91cf\u663e\u5f0f\u5730\u58f0\u660e\u6210Button\u7c7b\u578b\u3002<\/p>\n\n\n\n<p>\u5f97\u5230\u6309\u94ae\u7684\u5b9e\u4f8b\u4e4b\u540e\uff0c\u6211\u4eec\u901a\u8fc7\u8c03\u7528setOnClickListener()\u65b9\u6cd5\u4e3a\u6309\u94ae\u6ce8\u518c\u4e00\u4e2a\u76d1\u542c\u5668\uff0c\u70b9\u51fb\u6309\u94ae\u65f6\u5c31\u4f1a\u6267\u884c\u76d1\u542c\u5668\u4e2d\u7684onClick()\u65b9\u6cd5\u3002\u56e0\u6b64\uff0c\u5f39\u51faToast\u7684\u529f\u80fd\u5f53\u7136\u662f\u8981\u5728onClick()\u65b9\u6cd5\u4e2d\u7f16\u5199\u4e86\u3002<\/p>\n\n\n\n<p>Toast\u7684\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u901a\u8fc7\u9759\u6001\u65b9\u6cd5makeText()\u521b\u5efa\u51fa\u4e00\u4e2aToast\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528show()\u5c06Toast\u663e\u793a\u51fa\u6765\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n\n\n\n<p>makeText()\u65b9\u6cd5\u9700\u8981\u4f20\u5165\u4e09\u4e2a\u53c2\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662fContext\uff0c\u4e5f\u5c31\u662fToast\u8981\u6c42\u7684\u4e0a\u4e0b\u6587\uff0c\u7531\u4e8eActivity\u672c\u8eab\u5c31\u662f\u4e00\u4e2aContext\u5bf9\u8c61\uff0c\u56e0\u6b64\u8fd9\u91cc\u76f4\u63a5\u4f20\u5165this\u5373\u53ef\u3002\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662fToast\u663e\u793a\u7684\u6587\u672c\u5185\u5bb9\u3002\u7b2c\u4e09\u4e2a\u53c2\u6570\u662fToast\u663e\u793a\u7684\u65f6\u957f\uff0c\u6709\u4e24\u4e2a\u5185\u7f6e\u5e38\u91cf\u53ef\u4ee5\u9009\u62e9\uff1a<code>Toast.LENGTH_SHORT<\/code>\u548c<code>Toast.LENGTH_LONG<\/code>\u3002<\/p>\n\n\n\n<p><strong>\u8fd9\u91cc\u6211\u4eec\u8fd8\u80fd\u518d\u7b80\u5316\u4e00\u4e0b\u3002<\/strong>\u4f7f\u7528Kotlin\u7f16\u5199\u7684Android\u9879\u76ee\u5728app\/build.gradle\u6587\u4ef6\u7684\u5934\u90e8\u9ed8\u8ba4\u5f15\u5165\u4e86\u4e00\u4e2akotlin-android-extensions\u63d2\u4ef6\uff0c\u8fd9\u4e2a\u63d2\u4ef6\u4f1a\u6839\u636e\u5e03\u5c40\u6587\u4ef6\u4e2d\u5b9a\u4e49\u7684\u63a7\u4ef6id\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u5177\u6709\u76f8\u540c\u540d\u79f0\u7684\u53d8\u91cf\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Activity\u91cc\u76f4\u63a5\u4f7f\u7528\u8fd9\u4e2a\u53d8\u91cf\uff0c\u800c\u4e0d\u7528\u518d\u8c03\u7528findViewById()\u65b9\u6cd5\u4e86\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6211\u4eec\u5728\u5199Activity\u65f6\uff0c\u76f4\u63a5\u5199\u5143\u7d20\u7684id\u5373\u53ef\uff0c\u4e0d\u9700\u8981\u518d\u67e5\u627e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"kotlin\" class=\"language-kotlin\">override fun onCreate(savedInstanceState: Bundle?) {\n     super.onCreate(savedInstanceState)\n     setContentView(R.layout.first_layout)\n     button1.setOnClickListener {\n         Toast.makeText(this, \"You clicked Button 1\", Toast.LENGTH_SHORT).show()\n     }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u9762\u6211\u4eec\u5b66\u4e60\u4e86Kotlin \u7684\u57fa\u672c\u8bed\u6cd5\uff0c\u4f46\u6211\u4eec\u6700\u7ec8\u7684\u76ee\u7684\u662f\u4f5c\u4e3a\u5b89\u5353\u5f00\u53d1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5b66\u4e60\u5b89\u5353\u7684\u9875\u9762\u5f00\u53d1\u77e5\u8bc6\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[1,16],"tags":[],"class_list":["post-1266","post","type-post","status-publish","format-standard","hentry","category-wcd","category-16"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1266"}],"collection":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/comments?post=1266"}],"version-history":[{"count":6,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1266\/revisions"}],"predecessor-version":[{"id":1273,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/posts\/1266\/revisions\/1273"}],"wp:attachment":[{"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/media?parent=1266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/categories?post=1266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fireinsect.top\/index.php\/wp-json\/wp\/v2\/tags?post=1266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}