Gam*_*oid 1 android button android-widget
for(i = 0; i <26; i ++){
btnAlpha[i] = new Button(this);
btnAlpha[i].setBackgroundColor(Color.TRANSPARENT);
btnAlpha[i].setTextColor(Color.GREEN);
btnAlpha[i].setText(Character.toString ((char)(j+i)));
btnAlpha[i].setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//Want to get the text from the current button.
btnAlpha[i].getText();
//But it gives error that "i" cannot be accessed.
}});
Run Code Online (Sandbox Code Playgroud)
我得到的错误是"不能引用在不同方法中定义的内部类中的非最终变量".但我需要在正确的时间得到文本.怎么做?还有其他办法吗?请帮忙!
Kyl*_*e P 14
Button是View的子类,因此onClick的参数v是被单击的Button.尝试
public void onClick(View v) {
((Button) v).getText();
}
Run Code Online (Sandbox Code Playgroud)