我在按钮的循环中添加 OnClickListener,问题是 i 变量需要是最终的,但是不能增加他。
我的代码:
ArrayList<Button> buttonList = new ArrayList<Button>();
buttonList.add((Button)findViewById(R.id.mov1_btn));
buttonList.add((Button)findViewById(R.id.mov2_btn));
buttonList.add((Button)findViewById(R.id.mov3_btn));
buttonList.add((Button)findViewById(R.id.mov4_btn));
buttonList.add((Button)findViewById(R.id.mov5_btn));
buttonList.add((Button)findViewById(R.id.mov6_btn));
for(int i = 0;i<buttonList.size();i++)
{
buttonList.get(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonList.get(movCnt).setBackgroundResource(R.drawable.mov1n);
buttonList.get(i).setBackgroundResource(R.drawable.mov1o);
movCnt = i;
}
});
}