|
5 | 5 |
|
6 | 6 | <meta charset="UTF-8"> |
7 | 7 | <meta http-equiv="X-UA-Compatible" content="IE=11; IE=10; IE=9; IE=8; IE=7; IE=EDGE" /> |
8 | | - <title>ButterKnife | Android学习笔记</title> |
| 8 | + <title>ButterKnife | </title> |
9 | 9 | <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> |
10 | 10 | <meta name="description" content=""> |
11 | 11 | <meta name="generator" content="GitBook 1.4.1"> |
|
35 | 35 |
|
36 | 36 |
|
37 | 37 |
|
38 | | - <div class="book" data-level="10.1" data-basepath=".." data-revision="1422846108795"> |
| 38 | + <div class="book" data-level="10.1" data-basepath=".." data-revision="1425398533852"> |
39 | 39 |
|
40 | 40 |
|
41 | 41 | <div class="book-summary"> |
|
726 | 726 | <a href="#" class="btn pull-left toggle-summary" aria-label="Toggle summary"><i class="fa fa-align-justify"></i></a> |
727 | 727 | <a href="#" class="btn pull-left toggle-search" aria-label="Toggle search"><i class="fa fa-search"></i></a> |
728 | 728 |
|
729 | | - <a href="../GLOSSARY.html" class="btn pull-left" aria-label="Open Glossary"><i class="fa fa-sort-alpha-asc"></i></a> |
730 | | - |
731 | 729 | <div id="font-settings-wrapper" class="dropdown pull-left"> |
732 | 730 | <a href="#" class="btn toggle-dropdown" aria-label="Toggle font settings"><i class="fa fa-font"></i> |
733 | 731 | </a> |
|
791 | 789 | <!-- Title --> |
792 | 790 | <h1> |
793 | 791 | <i class="fa fa-circle-o-notch fa-spin"></i> |
794 | | - <a href="../" >Android学习笔记</a> |
| 792 | + <a href="../" ></a> |
795 | 793 | </h1> |
796 | 794 | </div> |
797 | 795 |
|
798 | 796 | <div class="page-wrapper" tabindex="-1"> |
799 | 797 | <div class="page-inner"> |
800 | 798 |
|
801 | 799 |
|
802 | | - <section class="normal" id="section-gitbook_4"> |
| 800 | + <section class="normal" id="section-gitbook_680"> |
803 | 801 |
|
804 | 802 | <h1 id="butterknife">ButterKnife</h1> |
| 803 | +<p><<<<<<< HEAD</p> |
| 804 | +<p>ButterKnife是一个Android View注入的库。</p> |
| 805 | +<h2 id="1-eclipse">1 配置Eclipse</h2> |
| 806 | +<p>在使用ButterKnife需要先配置一下Eclipse。</p> |
| 807 | +<p>项目右键-Properties-Java Complier-Annotation Processing 确保设置和下图一致</p> |
| 808 | +<p><img src="http://jakewharton.github.io/butterknife/static/ide-eclipse1.png" alt=""></p> |
| 809 | +<p>接着展开Annotation Processing选择Factory Path,选中Enable project specific settings。然后点击 Add JARs...,选中ButterKnife的jar包</p> |
| 810 | +<p><img src="http://jakewharton.github.io/butterknife/static/ide-eclipse2.png" alt=""></p> |
| 811 | +<p>然后点击ok保存设置,Eclipse将问你是否重新构建新项目,点击Yes。</p> |
| 812 | +<p>确保你项目的根目录里有一个.apt_generated的文件夹,文件夹中包含YOURACTIVITY$$ViewInjector.java这样的文件。</p> |
| 813 | +<h2 id="2-">2 使用注解</h2> |
| 814 | +<p>在Activity中使用注解</p> |
| 815 | +<pre><code class="lang-java"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ExampleActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> </span>{ |
| 816 | + <span class="hljs-annotation">@InjectView</span>(R.id.title) TextView title; |
| 817 | + <span class="hljs-annotation">@InjectView</span>(R.id.subtitle) TextView subtitle; |
| 818 | + <span class="hljs-annotation">@InjectView</span>(R.id.footer) TextView footer; |
| 819 | + |
| 820 | + <span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> </span>{ |
| 821 | + <span class="hljs-keyword">super</span>.onCreate(savedInstanceState); |
| 822 | + setContentView(R.layout.simple_activity); |
| 823 | + ButterKnife.inject(<span class="hljs-keyword">this</span>); |
| 824 | + <span class="hljs-comment">// TODO Use "injected" views...</span> |
| 825 | + } |
| 826 | +} |
| 827 | +</code></pre> |
| 828 | +<p>Fragment中使用注解</p> |
| 829 | +<pre><code class="lang-java"> |
| 830 | +<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FancyFragment</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Fragment</span> </span>{ |
| 831 | + <span class="hljs-annotation">@InjectView</span>(R.id.button1) Button button1; |
| 832 | + <span class="hljs-annotation">@InjectView</span>(R.id.button2) Button button2; |
| 833 | + |
| 834 | + <span class="hljs-annotation">@Override</span> <span class="hljs-function">View <span class="hljs-title">onCreateView</span><span class="hljs-params">(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)</span> </span>{ |
| 835 | + View view = inflater.inflate(R.layout.fancy_fragment, container, <span class="hljs-keyword">false</span>); |
| 836 | + ButterKnife.inject(<span class="hljs-keyword">this</span>, view); |
| 837 | + <span class="hljs-comment">// TODO Use "injected" views...</span> |
| 838 | + <span class="hljs-keyword">return</span> view; |
| 839 | + } |
| 840 | +} |
| 841 | +</code></pre> |
| 842 | +<p>Adapter中使用注解</p> |
| 843 | +<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAdapter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">BaseAdapter</span> </span>{ |
| 844 | + <span class="hljs-annotation">@Override</span> <span class="hljs-function"><span class="hljs-keyword">public</span> View <span class="hljs-title">getView</span><span class="hljs-params">(<span class="hljs-keyword">int</span> position, View view, ViewGroup parent)</span> </span>{ |
| 845 | + ViewHolder holder; |
| 846 | + <span class="hljs-keyword">if</span> (view != <span class="hljs-keyword">null</span>) { |
| 847 | + holder = (ViewHolder) view.getTag(); |
| 848 | + } <span class="hljs-keyword">else</span> { |
| 849 | + view = inflater.inflate(R.layout.whatever, parent, <span class="hljs-keyword">false</span>); |
| 850 | + holder = <span class="hljs-keyword">new</span> ViewHolder(view); |
| 851 | + view.setTag(holder); |
| 852 | + } |
| 853 | + |
| 854 | + holder.name.setText(<span class="hljs-string">"John Doe"</span>); |
| 855 | + <span class="hljs-comment">// etc...</span> |
| 856 | + |
| 857 | + <span class="hljs-keyword">return</span> convertView; |
| 858 | + } |
| 859 | + |
| 860 | + <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ViewHolder</span> </span>{ |
| 861 | + <span class="hljs-annotation">@InjectView</span>(R.id.title) TextView name; |
| 862 | + <span class="hljs-annotation">@InjectView</span>(R.id.job_title) TextView jobTitle; |
| 863 | + |
| 864 | + <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ViewHolder</span><span class="hljs-params">(View view)</span> </span>{ |
| 865 | + ButterKnife.inject(<span class="hljs-keyword">this</span>, view); |
| 866 | + } |
| 867 | + } |
| 868 | +} |
| 869 | +</code></pre> |
| 870 | +<h2 id="3">3.事件注入</h2> |
| 871 | +<p>点击事件注入</p> |
| 872 | +<pre><code class="lang-java"> |
| 873 | +<span class="hljs-annotation">@OnClick</span>(R.id.submit) |
| 874 | +<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sayHi</span><span class="hljs-params">(Button button)</span> </span>{ |
| 875 | + button.setText(<span class="hljs-string">"Hello!"</span>); |
| 876 | +} |
| 877 | +</code></pre> |
| 878 | +<p>多个控件具有相同的事件</p> |
| 879 | +<pre><code class="lang-java"><span class="hljs-annotation">@OnClick</span>({ R.id.door1, R.id.door2, R.id.door3 }) |
| 880 | +<span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">pickDoor</span><span class="hljs-params">(DoorView door)</span> </span>{ |
| 881 | + <span class="hljs-keyword">if</span> (door.hasPrizeBehind()) { |
| 882 | + Toast.makeText(<span class="hljs-keyword">this</span>, <span class="hljs-string">"You win!"</span>, LENGTH_SHORT).show(); |
| 883 | + } <span class="hljs-keyword">else</span> { |
| 884 | + Toast.makeText(<span class="hljs-keyword">this</span>, <span class="hljs-string">"Try again"</span>, LENGTH_SHORT).show(); |
| 885 | + } |
| 886 | +} |
| 887 | +</code></pre> |
| 888 | +<h2 id="4">4.重置注入</h2> |
| 889 | +<pre><code>public class FancyFragment extends Fragment { |
| 890 | + @InjectView(R.id.button1) Button button1; |
| 891 | + @InjectView(R.id.button2) Button button2; |
| 892 | + |
| 893 | + @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 894 | + View view = inflater.inflate(R.layout.fancy_fragment, container, false); |
| 895 | + ButterKnife.inject(this, view); |
| 896 | + // TODO Use "injected" views... |
| 897 | + return view; |
| 898 | + } |
| 899 | + |
| 900 | + @Override void onDestroyView() { |
| 901 | + super.onDestroyView(); |
| 902 | + ButterKnife.reset(this); |
| 903 | + } |
| 904 | +} |
| 905 | +</code></pre><h2 id="">可选注入</h2> |
| 906 | +<p>默认情况下@InjectView和@OnClick注入是必选的,如果view未找到将出现异常。为了避免出现异常,添加一个@Optional注解。</p> |
| 907 | +<h2 id="">其它</h2> |
| 908 | +<p>ButterKnife还包含了两个findById方法。</p> |
| 909 | +<pre><code> |
| 910 | +View view = LayoutInflater.from(context).inflate(R.layout.thing, null); |
| 911 | +TextView firstName = ButterKnife.findById(view, R.id.first_name); |
| 912 | +TextView lastName = ButterKnife.findById(view, R.id.last_name); |
| 913 | +ImageView photo = ButterKnife.findById(view, R.id.photo); |
| 914 | +</code></pre><h2 id="">混淆</h2> |
| 915 | +<p>为避免混淆的时代码被移除,所以要在proguard-project.txt中添加如下代码避免混淆</p> |
| 916 | +<pre><code>-dontwarn butterknife.internal.** |
| 917 | +-keep class **$$ViewInjector { *; } |
| 918 | +-keepnames class * { @butterknife.InjectView *;} |
| 919 | +</code></pre><h2 id="">阅读更多</h2> |
| 920 | +<p><a href="http://jakewharton.github.io/butterknife/" target="_blank">Butterknife</a></p> |
| 921 | +<p>=======</p> |
805 | 922 | <p>ButterKnife是一个Android View注入的库。</p> |
806 | 923 | <h2 id="1-eclipse">1 配置Eclipse</h2> |
807 | 924 | <p>在使用ButterKnife需要先配置一下Eclipse。</p> |
@@ -919,6 +1036,21 @@ <h2 id="">其它</h2> |
919 | 1036 | -keepnames class * { @butterknife.InjectView *;} |
920 | 1037 | </code></pre><h2 id="">阅读更多</h2> |
921 | 1038 | <p><a href="http://jakewharton.github.io/butterknife/" target="_blank">Butterknife</a></p> |
| 1039 | +<blockquote> |
| 1040 | +<blockquote> |
| 1041 | +<blockquote> |
| 1042 | +<blockquote> |
| 1043 | +<blockquote> |
| 1044 | +<blockquote> |
| 1045 | +<blockquote> |
| 1046 | +<p>43eb28dc379e3887bf933756ee10845587398c6c</p> |
| 1047 | +</blockquote> |
| 1048 | +</blockquote> |
| 1049 | +</blockquote> |
| 1050 | +</blockquote> |
| 1051 | +</blockquote> |
| 1052 | +</blockquote> |
| 1053 | +</blockquote> |
922 | 1054 |
|
923 | 1055 |
|
924 | 1056 | </section> |
|
0 commit comments