Butterknife是一个懒人工具,大量减少了程序员的代码,尤其是又长又臭的findViewById之类,下面介绍一下它的使用。
1、导包,没有找到的哥们点击这里
2、定义声明控件@Bind(R.id.btn)
3、在main函数中Butterknife.Bind(this)--->对于Activity使用
4、直接定义方法使用
@Onclick(R.id.btn)
void onClick(){
//执行你所想
}
就可以成功了
MainActivity类
package com.zhansy.mytext;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*
* Bufferknife的使用
* 注意和7.0以前版本用法不一样
* */
public class MainActivity extends Activity {
@Bind(R.id.tv)
TextView textView;
@Bind(R.id.btn)
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.btn)
void onClick(){
textView.setText("你点击了btn按钮");
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:textSize="20sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮"
android:id="@+id/btn"
android:layout_below="@+id/tv"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp" />
</RelativeLayout>
如果我有多个ID需要点击怎么做呢?
控件声明定义,一样的做法,直接上代码:
package com.zhansy.mytext;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
/*
* Bufferknife的使用
* 注意和7.0以前版本用法不一样
* */
public class MainActivity extends Activity {
@Bind(R.id.tv)
TextView textView;
@Bind(R.id.btn)
Button button;
@Bind(R.id.btn1)
Button button1;
@Bind(R.id.btn2)
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick({R.id.btn,R.id.btn1,R.id.btn2})
void onClick(View v){
switch (v.getId()){
case R.id.btn:
textView.setText("你点击了btn按钮");
break;
case R.id.btn1:
textView.setText("你点击了btn1按钮");
break;
case R.id.btn2:
textView.setText("你点击了btn2按钮");
break;
}
}
}
xml只是添加多两个按钮就不上代码了
------------------------------------------------------------------------------------
如果不是Activity就不能直接用Bufferknife.Bind(this),例如Fragment就需要用Bufferknife.Bind(this,view),其中view是inflater视图后的view
View view = inflater.inflate(R.layout.fragment, container, false); ButterKnife.bind(this, view);
-----------------------------------------------------------------------------------
除了Onclick方法可以简便,还有很多,具体我们在监听器那里已经学习了就不多说
OnCheckedChanged |
Bind a method to an
OnCheckedChangeListener on the view for each ID specified.
|
OnClick |
Bind a method to an
OnClickListener on the view for each ID specified.
|
OnEditorAction |
Bind a method to an
OnEditorActionListener on the view for each ID specified.
|
OnFocusChange |
Bind a method to an
OnFocusChangeListener on the view for each ID specified.
|
OnItemClick |
Bind a method to an
OnItemClickListener on the view for each ID specified.
|
OnItemLongClick |
Bind a method to an
OnItemLongClickListener on the view for each ID specified.
|
OnItemSelected |
Bind a method to an
OnItemSelectedListener on the view for each ID specified.
|
OnLongClick |
Bind a method to an
OnLongClickListener on the view for each ID specified.
|
OnPageChange |
Bind a method to an
OnPageChangeListener on the view for each ID specified.
|
OnTextChanged |
Bind a method to an
TextWatcher on the view for each ID specified.
|
OnTouch |
Bind a method to an
OnTouchListener on the view for each ID specified.
|
参考文档,点这里