With @OnClick
and @OnLongClick
annotations you can use your methods as android.view.View.OnClickListener
and android.view.View.OnLongClickListener
respectively. Also, no need to implement any interfaces:
public class SampleActivity extends BaseActivity {
@OnClick
void onClickSaveButton() {
}
@OnLongClick
void onLongClickSaveButton() {
}
}
As well as in case of @FindView, Androjeta composes the IDs by default:
<lowercased activity name> + "_" + <method name without prefix>
Where prefix
is onClick or onLongClick respectively.
Note For sure there’s no reason to distrust this approach. In case of misspelling, the code won’t be compiled.
Clearly, for this example, both onClickSaveButton
and onLongClickSaveButton
are bound to a view by id R.id.sampleActivity_saveButton
.
Note OnLongClick allows you to set method’s return type to void
. In this case Androjeta will return true
by default. Change it to boolean
otherwise.
You can also define IDs explicitly:
@OnClick(R.id.sampleActivity_saveButton)
void onClickSaveButton() {
}
or
@OnClick(name = "sampleActivity_saveButton")
void onClickSaveButton() {
}
BaseActivity
:
class BaseActivity extends Activity {
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
MetaHelper.applyOnClicks(this);
}
}
The helper method would be:
public static void applyOnClicks(Activity activity) {
new OnClickController(metasitory, activity).addListeners();
}