{"id":238,"date":"2016-11-30T15:55:21","date_gmt":"2016-11-30T15:55:21","guid":{"rendered":"https:\/\/boochlin.com\/?p=238"},"modified":"2016-12-02T01:41:12","modified_gmt":"2016-12-02T01:41:12","slug":"test","status":"publish","type":"post","link":"https:\/\/boochlin.com\/?p=238","title":{"rendered":"Android ButterKnife introduction"},"content":{"rendered":"<p><a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/11\/butterKnifelogo.png\"><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone size-full wp-image-242\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/11\/butterKnifelogo.png\" alt=\"butterknifelogo\" width=\"293\" height=\"293\" \/><\/a><\/p>\n<p>Android \u5beb\u4e45\u4e86\uff0c\u5927\u5bb6\u7e3d\u6703\u6709\u4e00\u4e9b\u5b9a\u756a\u7684\u8d77\u624b\u5f0f\uff0c\u50cf\u4e4b\u524d\u5c31\u4ecb\u7d39\u904e\u4e0d\u5c11\u65b9\u4fbf\u7684 lib \uff0c\u4f46\u4ed4\u7d30\u60f3\u60f3\u597d\u50cf\u5f9e\u6c92\u6709\u63d0\u904e\u5e38\u7528\u7684 android studio plug-in \uff0c\u56e0\u6b64\u9019\u6b21\u5c31\u4f86\u4ecb\u7d39\u500b\u5be6\u7528 plug-in\uff0cButterKnife \uff0c\u96d6\u7136\u9019\u6a23\u7279\u5225\u958b\u4e00\u7ae0\u7bc0\u4f86\u8aaa\u597d\u50cf\u5f88\u53b2\u5bb3\uff0c\u4f46\u5176\u5be6\u53ea\u662f\u61f6\u4eba\u5de5\u5177\uff0c\u6709\u6c92\u6709\u90fd\u6c92\u95dc\u4fc2\uff0c\u6c92\u6709\u5c31\u53ea\u662f\u9ebb\u7169\u4e00\u9ede\uff0c\u4f46\u662f\u7528\u719f\u4e86\uff0c\u5927\u6982\u6703&#8230;.\u8b8a\u7fd2\u6163\uff1f<\/p>\n<p><!--more--><\/p>\n<h3><a href=\"http:\/\/jakewharton.github.io\/butterknife\/\">Butter-knife<\/a><\/h3>\n<p>\u5976\u6cb9\u5200\uff0cGithub \u4e0a\u6709\u4e0a\u842c star \u7684\u77e5\u540d\u958b\u6e90\u5c08\u6848\uff0c\u4e3b\u8981\u7528\u65bc Android \u7684 View \u6ce8\u5165\uff0c\u4f7f\u7528 Annotation \u7684\u65b9\u5f0f\u8b93\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u3002\u642d\u914d\u81ea\u52d5 Annotation \u5de5\u5177 Android ButterKnife Zelezny \u7c21\u76f4\u65b9\u4fbf\u5230\u70b8\u6389\u3002\u4ee5\u4e0b\u70ba\u56db\u5927\u8a34\u6c42<\/p>\n<ul>\n<li>Eliminate <code>findViewById<\/code> calls by using <code>@BindView<\/code> on fields.<\/li>\n<li>Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.<\/li>\n<li>Eliminate anonymous inner-classes for listeners by annotating methods with <code>@OnClick<\/code> and others.<\/li>\n<li>Eliminate resource lookups by using resource annotations on fields.<\/li>\n<\/ul>\n<p>\u96d6\u7136\u6709\u9ede\u7a81\u5140\uff0c\u4f46\u662f\u9084\u8acb\u5148\u8f09\u5165 lib<\/p>\n<pre>compile 'com.jakewharton:butterknife:8.4.0'\r\nannotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'\r\n<\/pre>\n<p>\u63a5\u8005\u4f86\u770b\u770b\u9019\u4e9b\u8a34\u6c42\u5230\u5e95\u662f\u5565 ? \u4ee5\u5f80\u8eab\u70ba\u4e00\u500b android \u5de5\u7a0b\u5e2b\u4e00\u751f\u4e2d\u6c92\u6709\u5beb\u5e7e\u767e\u6b21\u5e95\u4e0b\u7684 CODE \u5be6\u5728\u662f\u4e0d\u53ef\u80fd\uff0c\u6c92\u932f\u5c31\u662f findViewById and class cast<\/p>\n<pre>\/\/ \u9019\u500b\u5c31\u662f\u57fa\u672c\u6700\u5e38\u7528\u7684\u53d6\u51fa view , \u5f8c\u9762\u4e00\u5b9a\u6703\u642d\u914d\u4e00\u500b layout xml\r\nEditText edtTitle = (EditText) findViewById(R.id.edtTitle);\r\n<\/pre>\n<p>\u800c butter knife \u5c31\u53ea\u8981<\/p>\n<pre>\/\/ \u5e95\u4e0b\u5169\u884c\u5c31\u53ef\u4ee5\u6536\u5de5\r\n@BindView(R.id.edtTitle)\r\nEditText edtTitle;\r\n\/\/ \u9664\u4e86\u6ce8\u5165 view \u4ee5\u5916\uff0c\u4e5f\u53ef\u4ee5\u7528\u65bc\u5176\u4ed6\u8cc7\u6e90\r\n@BindString(R.string.title) String title;\r\n@BindDrawable(R.drawable.graphic) Drawable graphic;\r\n@BindColor(R.color.red) int red; \/\/ int or ColorStateList field\r\n@BindDimen(R.dimen.spacer) Float spacer; \/\/ int (for pixel size) or float (for exact value) field\r\n\/\/ ...\r\n<\/pre>\n<p>\u53e6\u5916\u4e00\u500b\u5c31\u662f\u5927\u5bb6\u5f88\u5e38\u5beb\u7684 Onclick\u4e8b\u4ef6<\/p>\n<pre>\/\/ \u901a\u5e38\u547c\u53eb setOnClickListener\r\nedtTitle.setOnClickListener(new View.OnClickListener {....});\r\n\/\/ \u800c butter knife \u5247\u53ea\u9700\u8981\r\nOnClick(R.id.edtTitle)\r\npublic void OnEdtTitleClick(){\r\n  \/\/do something.\r\n}\r\n\/\/ \u5f88\u591a\u500bview \u4e5f\u53ef\u4ee5\u7d81\u5728\u540c\u4e00\u500b click method\r\nOnClick({ R.id.door1, R.id.door2, R.id.door3 })\r\npublic void pickDoor(DoorView door) {\r\n  if (door.hasPrizeBehind()) {\r\n    Toast.makeText(this, \"You win!\", LENGTH_SHORT).show();\r\n  } else {\r\n    Toast.makeText(this, \"Try again\", LENGTH_SHORT).show();\r\n  }\r\n}\r\n\r\n<\/pre>\n<p>\u60f3\u8981\u4e00\u6b21\u8655\u7406\u4e00\u4e32 view \u4e5f\u53ef\u4ee5\u9019\u6a23\u5beb(\u901a\u5e38\u662fview \u4e4b\u9593\u6709\u76f4\u63a5\u7684\u95dc\u4fc2)<br \/>\nYou can group multiple views into a List or array. \u5c07\u591a\u500bview \u6574\u7406\u5728\u4e00\u500b list\u88e1<\/p>\n<pre>@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })\r\nList nameViews;\r\n<\/pre>\n<p>The apply method allows you to act on all the views in a list at once. \u91dd\u5c0d\u5c6c\u6027list \u5167\u7684 view \u4e00\u8d77\u8a2d\u5b9a (\u5176\u5be6\u9019\u9805\u529f\u80fd\u6211\u6c92\u5565\u7528\u904e\uff0c\u4f7f\u7528\u60c5\u5883\u4e0d\u591a)<\/p>\n<pre>ButterKnife.apply(nameViews, DISABLE);\r\nButterKnife.apply(nameViews, ENABLED, false);\r\n<\/pre>\n<pre>static final ButterKnife.Action DISABLE = new ButterKnife.Action() {\r\n  @Override public void apply(View view, int index) {\r\n    view.setEnabled(false);\r\n  }\r\n};\r\nstatic final ButterKnife.Setter&lt;View, Boolean&gt; ENABLED = new ButterKnife.Setter&lt;View, Boolean&gt;() {\r\n  @Override public void set(View view, Boolean value, int index) {\r\n    view.setEnabled(value);\r\n  }\r\n};\r\n<\/pre>\n<p>An Android Property can also be used with the apply method.\u4fee\u6539\u5217\u8868\u5167view \u7684\u6240\u6709 alpha \u503c<\/p>\n<pre>ButterKnife.apply(nameViews, View.ALPHA, 0.0f);\r\n<\/pre>\n<p>\u8ac7\u5230\u9019\u88e1 ButterKnife \u5df2\u7d93\u5e6b\u5de5\u7a0b\u5e2b\u7701\u4e86\u4e0d\u5c11\u529f\u592b\uff0c\u4f46\u61c9\u8a72\u9084\u662f\u6709\u4eba\u6703\u54ed\u8aaa\u300e\u96d6\u7136\u4e0d\u7528\u5beb findViewById\uff0c\u4f46\u662fannotation \u4e5f\u5beb\u4e86\u4e00\u5927\u5806\uff0c\u9019\u4e5f\u9084\u662f dirty job \u963f\u300f<\/p>\n<p>\u6c92\u932f\uff0c\u5c31\u662f\u6709\u4eba\u60f3\u5230\u9019\u4e00\u9ede\uff0c\u4e7e\u8106\u9023 annotation \u4e5f\u81ea\u52d5\u5e6b\u4f60\u751f\u4e00\u751f\uff0c\u5c31\u662f <a href=\"https:\/\/github.com\/avast\/android-butterknife-zelezny\">ButterKnifeZelezny<\/a>\u00a0\uff0c\u9019\u500b android studio plug-in \u5b8c\u5168\u70ba\u4e86 butter knife \u800c\u751f\uff0c\u76f4\u63a5\u4e0a\u5f35\u5716\u5c31\u77e5\u9053\u591a\u65b9\u4fbf\u4e86\uff08\u8b93\u6211\u7cfe\u6b63\u4e00\u4e0b\u9019\u5f35\u5716\u7684 ButterKnife.inject(this) \uff0c\u73fe\u57288.0.0 \u7248\u672c\u4e4b\u5f8c\u5df2\u7d93\u4e0d\u7528 inject \uff0c\u800c\u662f ButterKnife.bind(this)\uff09<\/p>\n<p><a href=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/11\/zelezny_animated.gif\"><img decoding=\"async\" class=\"alignnone size-full wp-image-241\" src=\"https:\/\/boochlin.com\/wp-content\/uploads\/2016\/11\/zelezny_animated.gif\" alt=\"zelezny_animated\" width=\"712\" height=\"591\" \/><\/a><\/p>\n<ul>\n<li>\u5b89\u88dd\u65b9\u6cd5<\/li>\n<li>in Android Studio: go to <code>Preferences \u2192 Plugins \u2192 Browse repositories<\/code> and search for <code>ButterKnife Zelezny<\/code><\/li>\n<\/ul>\n<ul>\n<li><a href=\"http:\/\/plugins.jetbrains.com\/plugin\/7369\">download it<\/a> and install via <code>Preferences \u2192 Plugins \u2192 Install plugin from disk<\/code><\/li>\n<\/ul>\n<p>\u5beb\u5230\u9019\u88e1\u7b97\u662f\u544a\u4e00\u6bb5\u843d\uff0cButter Knife \u9084\u6709\u4e00\u4e9b\u6709\u8da3\u7684\u529f\u80fd\uff0c\u50cf\u662f\u4e0d\u5c11\u60c5\u6cc1\u4f7f\u7528 \u975c\u614b\u7de8\u8b6f inject view \u7684\u65b9\u5f0f\u4e26\u4e0d\u5408\u9069\uff0c\u5fc5\u9808\u5728 run-time \u968e\u6bb5\u7528 findViewById \u52d5\u614b\u53d6\u5f97 view \uff0c\u9019\u5c0d\u65bc Butter Knife \u80fd\u505a\u7684\u4e8b\u60c5\u5c31\u4e0d\u591a\u4e86\uff0c\u4e0d\u904e\u9084\u662f\u6709\u63d0\u4f9b\u7c21\u7565 class cast \u7684\u65b9\u6cd5\u53d6\u4ee3 findViewById<\/p>\n<pre>View view = LayoutInflater.from(context).inflate(R.layout.thing, null);\r\nTextView firstName = ButterKnife.findById(view, R.id.first_name);\r\nTextView lastName = ButterKnife.findById(view, R.id.last_name);\r\nImageView photo = ButterKnife.findById(view, R.id.photo);\r\n<\/pre>\n<p>reference:<\/p>\n<p><a href=\"http:\/\/jakewharton.github.io\/butterknife\/\">http:\/\/jakewharton.github.io\/butterknife\/<\/a><\/p>\n<p><a href=\"https:\/\/blog.ccjeng.com\/2015\/08\/Android-ButterKnife.html\">https:\/\/blog.ccjeng.com\/2015\/08\/Android-ButterKnife.html<\/a><\/p>\n<p><a href=\"http:\/\/www.jianshu.com\/p\/9ad21e548b69\">http:\/\/www.jianshu.com\/p\/9ad21e548b69<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android \u5beb\u4e45\u4e86\uff0c\u5927\u5bb6\u7e3d\u6703\u6709\u4e00\u4e9b\u5b9a\u756a\u7684\u8d77\u624b\u5f0f\uff0c\u50cf\u4e4b\u524d\u5c31\u4ecb\u7d39\u904e\u4e0d\u5c11\u65b9\u4fbf\u7684 lib \uff0c\u4f46\u4ed4\u7d30\u60f3\u60f3\u597d\u50cf\u5f9e\u6c92\u6709\u63d0\u904e\u5e38\u7528\u7684 android studio plug-in \uff0c\u56e0\u6b64\u9019\u6b21\u5c31\u4f86\u4ecb\u7d39\u500b\u5be6\u7528 plug&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[50,52,61],"tags":[62],"_links":{"self":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/238"}],"collection":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=238"}],"version-history":[{"count":1,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":239,"href":"https:\/\/boochlin.com\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions\/239"}],"wp:attachment":[{"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/boochlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}