简单一点,我需要从onClickListener中访问一个全局变量,但是实例不知道"这个"指针。有没有一种简单的方法可以做到这一点:
activityButton = (Button) findViewById(R.id.actbtn);
activityButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragmentCommunicator != null) {
// cannot really do this. I need to pass the "this pointer"
int counter = ((MyApplication) this.getApplication()).getGlobalCounter(); counter++;
fragmentCommunicator.passDataToFragment("Hi from activity!, global counter = " + counter);
}
}
我尝试过自定义OnClickListener
How to pass parameters to OnClickListener?
但是有一个紧凑的语法吗?在cpp ++中,它将等同于lambda capture [=]
THX!