Custom View ButterKnife onClick

I created a custom button and tried to use annotation ButterKnife @onClickbut it does not work.

Can I use ButterKnifefor custom views like this, or will I have to use the default onClickListener?

Custombutton

public class CustomButton extends RelativeLayout {

public CustomButton(Context context) {
    super(context);
}

public CustomButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

private void init() {
    inflate(getContext(), R.layout.button_layout, this);
}

Menu

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
    ButterKnife.bind(this, rootView);
    return rootView;
}

@OnClick(R.id.button_play)
public void onButtonPlayClicked() {
    // NOT CALLED
}

Menu Layout

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_pattern"
android:padding="15dp">

<com.example.ui.custom.CustomButton
    android:id="@+id/button_play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"/>
</RelativeLayout>
+4
source share
5 answers

I am currently facing the same problem and have found a pretty nice solution. I override setOnClickListener (OnClickListener l) inside my view and assign a listener to my button. After that, @OnClick works as expected.

:

@Override
public void setOnClickListener(OnClickLister l) {
   ButterKnife.findById(this.getView(), R.id.button_play).setOnClickLister(l);
}
+3

http://jakewharton.imtqy.com/butterknife/

, .

public class FancyButton extends Button {
    @OnClick
    public void onClick() {
        // TODO do something!
    }
}

, !

+2

. , .

:

public class CustomButton extends RelativeLayout {

    // ...

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP) {
            if(hasOnClickListeners()) {
                callOnClick();
            }
        }
        return super.dispatchTouchEvent(event);
    }

    // ...

}

, .

+2

, !

XML (R.layout.button_layout) <merge>. ButterKnife:

, ButterKnife.bind(). <merge> , . , XML, onFinishInflate().

:

<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:background="@drawable/bg_button_default"
android:clickable="true"
android:focusable="true"
android:padding="@dimen/normal_padding"
tools:background="@color/white"
tools:layout_height="80dp">

:

<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

CustomView. OnClick OnLongClick !

, !

+2
In your activity try to add..

 ButterKnife.inject(this);
check this code

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
}

@OnClick(R.id.buttonAlert)
public void alertClicked(View v){
new AlertDialog.Builder(v.getContext())
    .setMessage(getFormattedMessge())
    .setNeutralButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        }
    })
    .show();
 }
-1

Source: https://habr.com/ru/post/1627390/


All Articles