我想用一堆按鈕創建一個頁面,每個按鈕都增加自己的變量。當我第一次點擊一個按鈕時,變量增加沒有問題。但是,第二次,價值不增加。我將所有聽衆包含在一個onclicklistener handler
之內,這可能是問題的一部分,但我不想重寫我的所有代碼,如果這樣做不能解決問題,或者修復更簡單。 代碼:OnClickListener只能增加一次
final OnClickListener handler = new View.OnClickListener()
{
public void onClick(View v)
{
Squad current = new Squad(BasketballGUIActivity.currentsquad);
if (v == us2)
{
current.usTotal2++;
us2.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us2);
Log.e("ustotal2", ""+current.usTotal2);
return;
}
if(v==us2made)
{
current.usTotal2made+=1;
us2made.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us2made);
}
if(v==them2)
{
current.themTotal2+=1;
them2.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them2);
}
if(v==them2made)
{
current.themTotal2made+=1;
them2made.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them2made);
}
if(v==us1)
{
current.usTotal1+=1;
us1.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us1);
}
if(v==us1made)
{
current.usTotal1made+=1;
us1made.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us1made);
}
if(v==them1)
{
current.themTotal1+=1;
them1.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them1);
}
if(v==them1made)
{
current.themTotal1made+=1;
them1made.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them1made);
}
if(v==us0reb)
{
current.usTotalOreb+=1;
us0reb.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us0reb);
}
if(v==usxreb)
{
current.usTotalXreb+=1;
usxreb.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(usxreb);
}
if(v==them0reb)
{
current.themTotalOreb+=1;
them0reb.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them0reb);
}
if(v==themxreb)
{
current.themTotalXreb+=1;
themxreb.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(themxreb);
}
if(v==us3)
{
current.usTotal3+=1;
us3.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us3);
}
if(v==us3made)
{
current.usTotal3made+=1;
us3made.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us3made);
}
if(v==them3)
{
current.themTotal3+=1;
them3.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them3);
}
if(v==them3made)
{
current.themTotal3made+=1;
them3made.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them3made);
}
if(v==us2foul)
{
current.usTotal2foul+=1;
us2foul.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us2foul);
}
if(v==us2foulmade)
{
current.usTotal2foulmade+=1;
us2foulmade.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us2foulmade);
}
if(v==us3foul)
{
current.usTotal3foul+=1;
us3foul.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us3foul);
}
if(v==us3foulmade)
{
current.usTotal3foulmade+=1;
us3foulmade.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(us3foulmade);
}
if(v==usTO)
{
current.usTotalTO+=1;
usTO.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(usTO);
}
if(v==them2foul)
{
current.themTotal2foul+=1;
them2foul.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them2foul);
}
if(v==them2foulmade)
{
current.themTotal2foulmade+=1;
them2foulmade.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them2foulmade);
}
if(v==them3foul)
{
current.themTotal3foul+=1;
them3foul.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them3foul);
}
if(v==them3foulmade)
{
current.themTotal3foulmade+=1;
them3foulmade.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(them3foulmade);
}
if(v==themTO)
{
current.themTotalTO+=1;
themTO.getBackground().setColorFilter(Color.parseColor("#00FF00"), PorterDuff.Mode.DARKEN);
new ButtonColorTask().execute(themTO);
}
}
而對於空間的緣故,是的,我初始化所有按鈕的監聽器(如果我沒有,他們甚至不會增加一個第一次)。我想知道發生了什麼問題,如果有一個簡單的方法來解決這個問題。我認爲問題可能是我沒有爲每個按鈕單獨監聽器,但我不確定。要回顧一下,問題是,當我單擊一個按鈕時,我的變量會增加一次,但不會超過一次。奇怪的是,每按一次按鈕實際上會運行ButtonColorTask
(這只是在一段時間後將按鈕的顏色改回默認值),這真的讓我更加困惑。任何幫助將不勝感激
每次你創建一個新的Squad current = new Squad(BasketballGUIActivity.currentsquad); –