因为要使用开源 androidannotations 注解,发现eclipse没有Annotation Processin
解决办法:
需要配置插件:http://download.eclipse.org/releases/juno
Annotation Processin
http://blog.csdn.net/vurtne_ye/article/details/38850529
工程中添加butterknife.jar后, 在执行代码时,发现注解不成功. 查阅相关文档后, 发现需要在eclipse中对butterknife进行相关的配置. 再次运行后,成功解决问题. 记录配置流程如下: 配置流程 Java代码 收藏代码 工程项目 - 右键 - 选择 properties - 选中 java C
工程中添加butterknife.jar后, 在执行代码时,发现注解不成功.
查阅相关文档后, 发现需要在eclipse中对butterknife进行相关的配置.
再次运行后,成功解决问题.
记录配置流程如下:
配置流程
Java代码 收藏代码
工程项目
-> 右键
-> 选择 properties
-> 选中 java Complier
-> 选中 Annotation Processing
-> 勾选 Enable project specific settings (勾选后,其他选项自动被勾选)
-> 选中 Factory Path
-> 勾选 Enable project specific settings
-> 点击 Add JARs
-> 添加 butterknife.jar
butterknife
让我们从繁琐的 findViewById
中解救出来。下面直接是使用方法
Activity
class ExampleActivity extends Activity { @InjectView(R.id.title) TextView title; @InjectView(R.id.subtitle) TextView subtitle; @InjectView(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } }
Fragment 销毁的时候掉用 ButterKnife . reset ( this );
public class FancyFragment extends Fragment { @InjectView(R.id.button1) Button button1; @InjectView(R.id.button2) Button button2; @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fancy_fragment, container, false); ButterKnife.inject(this, view); // TODO Use "injected" views... return view; } }
adapter
public class MyAdapter extends BaseAdapter { @Override
public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view != null) { holder = (ViewHolder) view.getTag(); } else { view = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); view.setTag(holder); } holder.name.setText("John Doe"); // etc... return view; } static class ViewHolder { @InjectView(R.id.title) TextView name; @InjectView(R.id.job_title) TextView jobTitle; public ViewHolder(View view) { ButterKnife.inject(this, view); } } }
VIEW LISTS
@InjectViews({ R.id.first_name, R.id.middle_name, R.id.last_name }) List<EditText> nameViews;
CLICK LISTENER INJECTION
@OnClick(R.id.submit) public void submit() { // TODO submit data to server... }
@OnClick(R.id.submit) public void sayHi(Button button) { button.setText("Hello!"); }
还可以绑定多控件id
@OnClick({ R.id.door1, R.id.door2, R.id.door3 }) public void pickDoor(DoorView door) { if (door.hasPrizeBehind()) { Toast.makeText(this, "You win!", LENGTH_SHORT).show(); } else { Toast.makeText(this, "Try again", LENGTH_SHORT).show(); } }
INJECTION RESET 销毁
ButterKnife.reset(this);
默认情况下 @InjectView
and @OnClick
找到控件都是不允许为空的,否则会抛异常。
可以添加 @Optional
允许为空
@Optional @InjectView(R.id.might_not_be_there) TextView mightNotBeThere; @Optional @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() { // TODO ... } http://www.tuicool.com/articles/VRruAfAhttp://www.it165.net/pro/html/201404/12375.html