彼らが言ったように、私はバターナイフの指示に従います:「カスタムビューは、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");
// }
}
buttonPressed メソッドのコメントを外すと、「buttonPressed」が表示されます。コメントしても何も起こりません。私が期待しているのは、最初に「hello」が表示され、次に「buttonPressed」が表示されることです。
あなたはなにか考えはありますか?ありがとうございました。