0

我试图有一个带有一堆按钮的页面,每个按钮都会增加自己的变量。当我第一次单击按钮时,变量增加没问题。但是,第二次,该值不会增加。我将所有侦听器都包含在 oneonclicklistener handler中,这可能是问题的一部分,但如果这不能解决问题,或者有更简单的解决方法,我不想重写我的所有代码。代码:

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(这只是在一定时间后将按钮的颜色更改回默认值),这真的让我更加困惑。任何帮助将不胜感激

4

2 回答 2

1

移动小队 current = new Squad(BasketballGUIActivity.currentsquad); 进入班级成员级别。

final OnClickListener handler = new View.OnClickListener()
    {           
        public void onClick(View v)
        {
            Squad current = new Squad(BasketballGUIActivity.currentsquad);
------>
final OnClickListener handler = new View.OnClickListener()
    {   
        Squad current = new Squad(BasketballGUIActivity.currentsquad);        
        public void onClick(View v)
        {
于 2012-06-30T03:07:22.517 回答
0

您检查特定按钮单击的方式是错误的。它应该如下:

 if(v.getId() == R.id.BUTTONID) {

}

对所有其他按钮执行相同操作。我想这(以及将变量current从 中取出onClick())应该可以解决您的问题。

于 2012-06-30T04:04:38.643 回答