2
我跟着butterknife instruction,因为他们说:“自定义视图可以通过不指定ID绑定到自己的监听器。Android - 自定义视图可以通过不指定ID绑定到自己的监听器 - 不起作用
级
Gradle
compile 'com.jakewharton:butterknife:7.0.0'
我创建的BaseButton:
public class BaseButton extends Button {
public BaseButton(Context context) {
super(context);
}
public BaseButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OnClick
public void onClick() {
System.out.println("Hello");
}}
activity_main
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<com.example.BaseButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test"/>
而且MainActivity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
// @OnClick(R.id.button)
// public void buttonPressed(){
// System.out.println("buttonPressed");
// }
}
如果我un-comment buttonPressed方法,“buttonPressed”显示出来。当我评论时,没有任何反应。我期望的是首先看到“hello”,然后看到“buttonPressed”。
你有什么想法?谢谢。
谢谢杰克,如果我想在做特定任务之前为所有按钮添加一个动作。像往常一样,我可以重写并打电话超级,是否可以用黄油刀做成。 – thanhbinh84
覆盖点击侦听器设置器,并在调用完成之前使用您的动作包装它。 –
你能否解释一下如何重载click监听器?谢谢 – thanhbinh84