Android注解式开发BufferKnife的使用

一、BufferKnife:什么是注解式开发?

  • JDK1.5后支持注解方式,想用注解式开发,就要自定义注解
  • 结构:@+注解名(也可以是类名)+传递的属性值,key和value,可设置目标 
  • 范围:方法(Method)、熟悉(Filed)、类(Type)
  • 自定义注解要用到@interface:用于定义注解;@T爱人跟帖:用于描述注解的使用范围;@Retention:注解的声明周期,一般RetentionPoikcy.RUNTIME
  • 解放代码,提高可读性,提升开发效率
二、ButterKnfe的用法:
  • 配置:下载jar包仿在libs里,配置eclips,鼠标对准需要注解的项目,单机右键poperties-》java complier -》Annotation Procession -》钩下E那边了 project specific settings 其他的就会自动勾上
  • View绑定:onCreate里注册:ButterKnife.inject(this); Activity声明绑定控件,例如:@injectView(R.id.title)TextView title;
三、BufferKnife的示例:
  • 效果
很简单的一个ListView列表,每个iten里对应一个Button,没有截成动态图;
  • 主要代码:Mainactivity:
package com.Evan.butterknifedemo;

import java.util.ArrayList;

import com.jike.butterknifedemo.R;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnItemClick;

/**
 * 注解式实现ListView展示数据
 * 
 * @author Administrator
 * 
 */

public class MainActivity extends Activity {
	@InjectView(R.id.lv)
	ListView lv;
	private MyAdapter adapter;
	private ArrayList<String> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ButterKnife.inject(this);
		list = new ArrayList<String>();
		list.add("测试1");
		list.add("测试2");
		list.add("测试3");
		list.add("测试4");
		list.add("测试5");
		list.add("测试10");
		list.add("测试11");
		list.add("测试12");
		list.add("测试13");
		list.add("测试14");
		adapter = new MyAdapter(this, list);
		lv.setAdapter(adapter);
	}

	@OnItemClick(R.id.lv)
	public void onMyItemClick(int position) {
		Toast.makeText(this, position + "", Toast.LENGTH_SHORT).show();
	}

}

MyAdapter':
package com.Evan.butterknifedemo;

import java.util.ArrayList;

import com.jike.butterknifedemo.R;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.ButterKnife;
import butterknife.InjectView;

public class MyAdapter extends BaseAdapter {
	private Context mContext;
	private ArrayList<String> lists;

	public MyAdapter(Context context, ArrayList<String> list) {
		mContext = context;
		lists = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return lists.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.item, null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		String s = lists.get(position);
		holder.tv_text.setText(s);
		holder.btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Toast.makeText(mContext, "点击", Toast.LENGTH_SHORT).show();
			}
		});
		return convertView;
	}

	static class ViewHolder {
		@InjectView(R.id.tv_text)
		TextView tv_text;
		@InjectView(R.id.btn)
		Button btn;

		public ViewHolder(View view) {
			ButterKnife.inject(this,view);
		}
	}

}

你可能感兴趣的:(BufferKnife,注解式开发)