{"id":341,"date":"2023-02-25T06:56:13","date_gmt":"2023-02-24T22:56:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=341"},"modified":"2023-04-30T14:48:46","modified_gmt":"2023-04-30T06:48:46","slug":"android-studio-install-and-use-butterknife","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/android-studio-install-and-use-butterknife\/","title":{"rendered":"Android Studio\u4e2dButterKnife\u5b89\u88c5\u4f7f\u7528"},"content":{"rendered":"<h2>ButterKnife \u7b80\u4ecb<\/h2>\n<p>Github\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/github.com\/JakeWharton\/butterknife\">https:\/\/github.com\/JakeWharton\/butterknife<\/a><\/p>\n<p><!-- more --><\/p>\n<p>ButterKnife \u4f18\u52bf\uff1a<\/p>\n<ul>\n<li>1.\u5f3a\u5927\u7684View\u7ed1\u5b9a\u548cClick\u4e8b\u4ef6\u5904\u7406\u529f\u80fd\uff0c\u7b80\u5316\u4ee3\u7801\uff0c\u63d0\u5347\u5f00\u53d1\u6548\u7387<\/li>\n<li>2.\u65b9\u4fbf\u7684\u5904\u7406Adapter\u91cc\u7684ViewHolder\u7ed1\u5b9a\u95ee\u9898<\/li>\n<li>3.\u8fd0\u884c\u65f6\u4e0d\u4f1a\u5f71\u54cdAPP\u6548\u7387\uff0c\u4f7f\u7528\u914d\u7f6e\u65b9\u4fbf<\/li>\n<li>4.\u4ee3\u7801\u6e05\u6670\uff0c\u53ef\u8bfb\u6027\u5f3a<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u5fc3\u5f97\uff1a<\/p>\n<ul>\n<li>1.Activity ButterKnife.bind(this);\u5fc5\u987b\u5728setContentView();\u4e4b\u540e\uff0c\u4e14\u7236\u7c7bbind\u7ed1\u5b9a\u540e\uff0c\u5b50\u7c7b\u4e0d\u9700\u8981\u518dbind<\/li>\n<li>2.Fragment ButterKnife.bind(this, mRootView);<\/li>\n<li>3.\u5c5e\u6027\u5e03\u5c40\u4e0d\u80fd\u7528private or static \u4fee\u9970\uff0c\u5426\u5219\u4f1a\u62a5\u9519<\/li>\n<li>4.setContentView()\u4e0d\u80fd\u901a\u8fc7\u6ce8\u89e3\u5b9e\u73b0\u3002\uff08\u5176\u4ed6\u7684\u6709\u4e9b\u6ce8\u89e3\u6846\u67b6\u53ef\u4ee5\uff09<\/li>\n<\/ul>\n<h2>\u4fee\u6539\u914d\u7f6e\u6587\u4ef6 build.gradle<\/h2>\n<h3>Android Studio \u7248\u672c 2.1\u53ca\u4ee5\u4e0b<\/h3>\n<p>\u5728project\u5c42\u7ea7\u7684build.gradle<\/p>\n<pre><code>buildscript {\n    repositories {\n        jcenter()\n        \/\/\u652f\u6301java \u4f9d\u8d56\u5e93\u7ba1\u7406\uff08maven\/ivy\uff09\uff0c\u7528\u4e8e\u9879\u76ee\u7684\u4f9d\u8d56\n        mavenCentral()\n    }\n    \/\/\u4f9d\u8d56\u5305\u7684\u5b9a\u4e49\u3002\u652f\u6301maven\/ivy\u3001\u8fdc\u7a0b\u3001\u672c\u5730\u5e93\n    dependencies {\n        classpath &#039;com.android.tools.build:gradle:2.1.2&#039;\n        classpath &#039;com.neenbedankt.gradle.plugins:android-apt:1.8&#039;\n        \/\/ NOTE: Do not place your application dependencies here; they belong\n        \/\/ in the individual module build.gradle files\n    }\n}<\/code><\/pre>\n<p>\u5728app\u5c42\u7ea7\u7684build.gradle<\/p>\n<pre><code>apply plugin: &#039;com.neenbedankt.android-apt&#039;\n\nandroid {\n    \/\/...\n}\n\ndependencies{\n    \/\/...\n    compile &#039;com.jakewharton:butterknife:8.4.0&#039;\n    apt &#039;com.jakewharton:butterknife-compiler:8.4.0&#039;\n}<\/code><\/pre>\n<h3>Android Studio \u7248\u672c 2.2+<\/h3>\n<p>\u5728project\u5c42\u7ea7\u7684build.gradle<\/p>\n<pre><code>buildscript {\n    repositories {\n        jcenter()\n        \/\/\u652f\u6301java \u4f9d\u8d56\u5e93\u7ba1\u7406\uff08maven\/ivy\uff09\uff0c\u7528\u4e8e\u9879\u76ee\u7684\u4f9d\u8d56\n        mavenCentral()\n    }\n    \/\/\u4f9d\u8d56\u5305\u7684\u5b9a\u4e49\u3002\u652f\u6301maven\/ivy\u3001\u8fdc\u7a0b\u3001\u672c\u5730\u5e93\n    dependencies {\n        classpath &#039;com.android.tools.build:gradle:2.2.2&#039;\n        classpath &#039;com.jakewharton:butterknife-gradle-plugin:8.4.0&#039;\n        \/\/ NOTE: Do not place your application dependencies here; they belong\n        \/\/ in the individual module build.gradle files\n    }\n}<\/code><\/pre>\n<p>\u5728app\u5c42\u7ea7\u7684build.gradle<\/p>\n<pre><code>apply plugin: &#039;com.jakewharton.butterknife&#039;\n\nandroid {\n    \/\/...\n}\n\ndependencies{\n    \/\/...\n    compile &#039;com.jakewharton:butterknife:8.4.0&#039;\n    annotationProcessor &#039;com.jakewharton:butterknife-compiler:8.4.0&#039;\n}<\/code><\/pre>\n<blockquote>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u4e0d\u914d\u7f6eapt\u6216\u8005annotationProcessor\uff0c@BindView \u53ef\u80fd\u62a5\u7a7a\u6307\u9488\u5f02\u5e38\uff0c@OnClick\u4e5f\u4e0d\u8d77\u4f5c\u7528<\/p>\n<\/blockquote>\n<h2>\u5b89\u88c5\u63d2\u4ef6Android ButterKnife Zelezny<\/h2>\n<p>\u540c\u6b65\u914d\u7f6e\u6587\u4ef6\uff0c\u7136\u540e\u5728\u4e0a\u8ff0\u914d\u7f6e\u7684\u57fa\u7840\u4e0a\u5b89\u88c5\u63d2\u4ef6Android ButterKnife Zelezny\uff0c\u5b89\u88c5\u5b8c\u6210\u91cd\u542fAndroid Studio\u3002<\/p>\n<p>\u81f3\u6b64\uff0cButterKnife\u63d2\u4ef6\u5c31\u5df2\u5b89\u88c5\u6210\u529f\u3002\u5728activity\u6216\u8005fragment\u7684R.layout.*\u5904\u5c31\u53ef\u4ee5\u4f7f\u7528\u53f3\u952eGenerate ButterKnife Injections<\/p>\n<h2>\u5982\u4f55\u4f7f\u7528 ButterKnife<\/h2>\n<ul>\n<li>1\u3001\u6709\u6240\u4f7f\u7528\u7684\u5e03\u5c40 ID \u4e0a\u70b9\u51fb\u53f3\u952e (\u4f8b\u5982 R.layout.activity_mian )\uff0c \u7136\u540e\u9009\u62e9 Generate -&gt; Generate ButterKnife Injections<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u5728R.layout\u5904\u53f3\u952eGenerate.png\" alt=\"\u5728R.layout\u5904\u53f3\u952eGenerate\" \/><\/p>\n<p>![Generate ButterKnife Injections](<a target=\"_blank\" rel=\"noopener\" href=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/Generate\">http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/Generate<\/a> ButterKnife Injections.png)<\/p>\n<ul>\n<li>2\u3001\u5728\u5bf9\u8bdd\u6846\u4e2d\u9009\u62e9\u9700\u8981\u6ce8\u5165\u7684 View\uff0c\u8fd8\u6709\u4e2a\u9009\u9879\u53ef\u4ee5\u7ed9 Adapter \u521b\u5efa\u4e00\u4e2a ViewHolder\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"http:\/\/www.yezhou.me\/AppBlog\/images\/Android\/\u914d\u7f6e\u6ce8\u5165\u63a7\u4ef6.png\" alt=\"\u914d\u7f6e\u6ce8\u5165\u63a7\u4ef6\" \/><\/p>\n<ul>\n<li>3\u3001\u70b9\u51fb Confirm\uff0c \u4ee3\u7801\u81ea\u52a8\u751f\u6210\uff01<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>ButterKnife \u7b80\u4ecb Github\uff1ahttps:\/\/github.com\/JakeWharton\/bu [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[117],"tags":[114],"class_list":["post-341","post","type-post","status-publish","format-standard","hentry","category-butterknife","tag-butterknife"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/341","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=341"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/341\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}