ButterKnife使用简析

直接看下怎么写吧,用起来很方便喔,讨厌的事件绑定可以好看许多。

public class ButterknifeActivity extends Activity {
    @Bind({R.id.menu_1,R.id.menu_2,R.id.menu_3,R.id.menu_4,R.id.menu_5})   //这个做菜单绑定简单许多
    protected List<TextView> menus;
    @Bind(R.id.img)
    protected ImageView imageView;
    @Bind(R.id.spinner)
    protected Spinner spinner;

    private String[] items = new String[]{"item1","item2","item3","item4","item5"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_butterknife);
        ButterKnife.bind(this);

        imageView.setImageDrawable(getResources().getDrawable(R.drawable.obama));

        for (TextView textView:menus) {
            textView.setTextColor(getResources().getColor(android.R.color.holo_orange_light));
        }


        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, items);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

    @OnItemSelected(value = R.id.spinner, callback = OnItemSelected.Callback.ITEM_SELECTED)
    protected void itemClicked(int position){
        Toast.makeText(ButterknifeActivity.this,items[position],Toast.LENGTH_SHORT).show();
    }

    @OnItemSelected(value = R.id.spinner, callback = OnItemSelected.Callback.NOTHING_SELECTED)
    protected void nothingClicked(){
        Toast.makeText(this, "Nothing", Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.menu_1,R.id.menu_2,R.id.menu_3,R.id.menu_4,R.id.menu_5})
    protected void menuClicked(View v){
        String msg = "";
        switch (v.getId()) {
            case R.id.menu_1:
                msg = "menu 1";
                break;
            case R.id.menu_2:
                msg = "menu 2";
                break;
            case R.id.menu_3:
                msg = "menu 3";
                break;
            case R.id.menu_4:
                msg = "menu 4";
                break;
            case R.id.menu_5:
                msg = "menu 5";
                break;
        }
        Toast.makeText(ButterknifeActivity.this, msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        ButterKnife.unbind(this);
        super.onDestroy();
    }
}
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout

        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/menu_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Menu1"
            android:padding="5dp"
            />

        <TextView
            android:id="@+id/menu_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Menu2"
            android:padding="5dp"
            />

        <TextView
            android:id="@+id/menu_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Menu3"
            android:padding="5dp"
            />

        <TextView
            android:id="@+id/menu_4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Menu4"
            android:padding="5dp"
            />

        <TextView
            android:id="@+id/menu_5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Menu5"
            android:padding="5dp"
            />

        <ImageView
            android:id="@+id/img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>

</ScrollView>

butterknife绑定可以指定viewgroup绑定,所以在fragment中可以这样绑定:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.frg_about,container,false);
    ButterKnife.bind(this, view);
    return view;
}

另:AS上可安装butterknife zelezny插件,在相应的布局id上右键一键绑定。

你可能感兴趣的:(ButterKnife使用简析)