我从Android: The method OnClickListener() is undefined for the type View尝试了这个例子 但我在.getC()收到错误:
error at The method getC() is undefined for the type View.OnClickListener
希望有人能帮助我弄明白。或者是否有另一种可能在侦听器中生成值?
View.OnClickListener myListener = new View.OnClickListener() {
double c;
// in case you need to use C elsewhere
public double getC() {
return c;
}
@Override
public void onClick(View v) {
// get A and B values
// then use either
c = a - b;
// or I guess use
c = c - b;
}
};
myButton.setOnClickListener(myListener);
int x= myListener.getC();
答案 0 :(得分:0)
您可以在匿名类中声明变量和-even公共方法,但是您无法引用它们,因为您的类无法引用。
您应该将该匿名类更改为嵌套,以便访问在其中声明的成员和方法:
MyOnclickListener myListener = new MyOnclickListener();
private final class MyOnclickListener implements View.OnClickListener
{
double c;
// in case you need to use C elsewhere
public double getC()
{
return c;
}
@Override
public void onClick(View v)
{
// get A and B values
// then use either
c = a - b;
// or I guess use
c = c - b;
}
}