SpyZip
SpyZip

Reputation: 5540

ButterKnife how to use @OnClick with parameters

I have a TextView and setting the text by calling setItem

public void setItem(String text){
    commentsTextView.setText(text);
}

With butterknife should be like this:

private String mText;

public void setItem(String text){
    mText = text;
}

@OnClick(R.id.commentsTextView)
protected void comment(){
    commentsTextView.setText(mText);
}

but this does not look right. What is the right way to use @OnClick, with parameters not just make a Toast like all the examples shows?

Upvotes: 3

Views: 2556

Answers (4)

Ethan Choi
Ethan Choi

Reputation: 2399

Annotate fields(BindView, OnClick, ...) in Butterknife is need called Butterknife.bind(...)

Make sure call Butterknife.bind function in your view.

Upvotes: 0

Salauddin Gazi
Salauddin Gazi

Reputation: 1517

in you android studio open (just click) @OnClick interface then you can see the method signature.

Upvotes: 0

Murat Karagöz
Murat Karagöz

Reputation: 37604

Besides the answer from Rikin, I think you are trying to misuse the @OnClick annotation. Normally you would pass the View and get/set the field from it e.g.

@OnClick(R.id.commentsTextView)
    protected void comment(TextView view){
        view.setText(mText);
    }

The field mText could be set beforehand somewhere.

Upvotes: 2

Rikin Prajapati
Rikin Prajapati

Reputation: 1943

Butter Knife's callbacks are methods on a class so if you want additional parameters those would need to be fields on that class.

Look into this.

Upvotes: 3

Related Questions