Java for android,代码重复

时间:2013-11-23 19:27:01

标签: java android

我目前有这段代码,但我需要重复它为红绿和蓝,有没有办法我可以做到没有复制和粘贴代码3次?

    yellow.setOnClickListener(new View.OnClickListener() {    
        public void onClick (View v)   {   

            switch (buttonCount) {
            case 1:  
                empty1.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess1= Colour.YELLOW;
                break;
            case 2:  
                empty2.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess2=Colour.YELLOW;
                break;
            case 3:  
                empty3.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess3=Colour.YELLOW;
                break;
            case 4:  
                empty4.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess4=Colour.YELLOW;
                break;
            case 5:  
                empty5.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess5=Colour.YELLOW;
                break;

            }
        }
    });

3 个答案:

答案 0 :(得分:1)

我不太了解android,但你可以定义一个私有的类

private class MyClickListener implements View.OnClickListener{

   private final String colour;
   private final String imageResource;

    public MyClickListener(String colour, String imageResource){
      this.colour = colour;
      this imageResource = imageResource;
    }   

   @Override
   public void onClick (View v)   {   

            switch (buttonCount) {
            case 1:  
                empty1.setImageResource(imageResource);
                buttonCount++;
                guess1= colour;
                break;
            case 2:  
                empty2.setImageResource(imageResource);
                buttonCount++;
                guess2=colour;
                break;
            case 3:  
                empty3.setImageResource(imageResource);
                buttonCount++;
                guess3=colour;
                break;
            case 4:  
                empty4.setImageResource(imageResource);
                buttonCount++;
                guess4=colour;
                break;
            case 5:  
                empty5.setImageResource(imageResource);
                buttonCount++;
                guess5=colour;
                break;

            }
        }
}

并在您的客户端代码中

yellow.setOnClickListener(new View.OnClickListener(new MyClickListener(Colour.YELLOW));

此外,您可以将它们放在像empty1首选empty2这样的集合中,而不是使用与List ArrayList相同类型的大量变量。然后在您的代码

中使用guess1 guessN
            list.get(buttonCount).setImageResource(imageResource);
            list.set(buttonCount,colour);
            buttonCount++;

答案 1 :(得分:1)

您可以通过检查单击哪个按钮然后设置相应的图像来轻松抽象出来,这是一个快速检查(尽管根据您的ID可能不会起作用的代码):

public void onClick (View v)   {   
    int drawable = 0;
    Colour guess = null;
    switch (v.getId())
    {
        case R.id.yellowButton:
            drawable = drawable;
            guess = Colour.YELLOW;
            break;
        case R.id.blueButton:
            drawable = R.drawable.blue;
            guess = Colour.BLUE
            break;
        case R.id.redButton:
            drawable = R.drawable.red;
            guess = Colour.RED
            break;
        case R.id.greenButton:
            drawable = R.drawable.green;
            guess = Colour.GREEN
            break;
    }
    switch (buttonCount) 
    {
    case 1:  
        empty1.setImageResource(drawable);
        buttonCount++;
        guess1= guess;
        break;
    case 2:  
        empty2.setImageResource(drawable);
        buttonCount++;
        guess2=guess;
        break;
    case 3:  
        empty3.setImageResource(drawable);
        buttonCount++;
        guess3=guess;
        break;
    case 4:  
        empty4.setImageResource(drawable);
        buttonCount++;
        guess4=guess;
        break;
    case 5:  
        empty5.setImageResource(drawable);
        buttonCount++;
        guess5=guess;
        break;

    }
}

然后您只需将按钮注册到此onClick方法(通过xml或以编程方式)

答案 2 :(得分:0)

您可以使用反射来做到这一点,但您应该意识到反射带来的危险。无论如何,你的代码看起来像这样:

  Button button;
  Class c = Class.forName("your.package.name.R$id");
  Field field = c.getDeclaredField("empty"+buttonCount);
  button = (Button) findViewById(field.getInt(null));
  button.setImageResource(R.drawable.yellow);

包围所有必要的例外情况。

编辑:在行中:c.getDeclaredField("empty"+buttonCount);您必须使用在布局xml中声明它们的实际ID。我认为它是“empty1”,“empty2”......“empty5”