试图让这个代码在android中变得不那么混乱,任何人都可以帮忙吗?

时间:2010-12-28 04:37:27

标签: android

我知道必须有一个更简单的方法,我只是不明白java如何运作得很好,我正在努力。有人可能会指出我正确的方向吗?

ImageButton image = (ImageButton) findViewById(R.id.card1);
    ImageButton image2 = (ImageButton) findViewById(R.id.card2);
    ImageButton image3 = (ImageButton) findViewById(R.id.card3);
    ImageButton image4 = (ImageButton) findViewById(R.id.card4);
    ImageButton image5 = (ImageButton) findViewById(R.id.card5);
    ImageButton image6 = (ImageButton) findViewById(R.id.card6);
    ImageButton image7 = (ImageButton) findViewById(R.id.card7);
    ImageButton image8 = (ImageButton) findViewById(R.id.card8);
    ImageButton image9 = (ImageButton) findViewById(R.id.card9);
    ImageButton image10 = (ImageButton) findViewById(R.id.card10);
    ImageButton image11 = (ImageButton) findViewById(R.id.card11);
    ImageButton image12 = (ImageButton) findViewById(R.id.card12);
    ImageButton image13 = (ImageButton) findViewById(R.id.card13);
    ImageButton image14 = (ImageButton) findViewById(R.id.card14);
    ImageButton image15 = (ImageButton) findViewById(R.id.card15);
    //image.setImageResource(R.drawable.test2);
    Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled = Bitmap.createScaledBitmap(bMap, buttonSize, buttonSize, true);
    Bitmap bMap2 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled2 = Bitmap.createScaledBitmap(bMap2, buttonSize, buttonSize, true);
    Bitmap bMap3 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled3 = Bitmap.createScaledBitmap(bMap3, buttonSize, buttonSize, true);
    Bitmap bMap4 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled4 = Bitmap.createScaledBitmap(bMap4, buttonSize, buttonSize, true);
    Bitmap bMap5 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled5 = Bitmap.createScaledBitmap(bMap5, buttonSize, buttonSize, true);
    Bitmap bMap6 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled6 = Bitmap.createScaledBitmap(bMap6, buttonSize, buttonSize, true);
    Bitmap bMap7 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled7 = Bitmap.createScaledBitmap(bMap7, buttonSize, buttonSize, true);
    Bitmap bMap8 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled8 = Bitmap.createScaledBitmap(bMap8, buttonSize, buttonSize, true);
    Bitmap bMap9 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled9 = Bitmap.createScaledBitmap(bMap9, buttonSize, buttonSize, true);
    Bitmap bMap10 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled10 = Bitmap.createScaledBitmap(bMap10, buttonSize, buttonSize, true);
    Bitmap bMap11 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled11 = Bitmap.createScaledBitmap(bMap11, buttonSize, buttonSize, true);
    Bitmap bMap12 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled12 = Bitmap.createScaledBitmap(bMap12, buttonSize, buttonSize, true);
    Bitmap bMap13 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled13 = Bitmap.createScaledBitmap(bMap13, buttonSize, buttonSize, true);
    Bitmap bMap14 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled14 = Bitmap.createScaledBitmap(bMap14, buttonSize, buttonSize, true);
    Bitmap bMap15 = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
    Bitmap bMapScaled15 = Bitmap.createScaledBitmap(bMap15, buttonSize, buttonSize, true);

    image.setImageBitmap(bMapScaled);
    image2.setImageBitmap(bMapScaled2);
    image3.setImageBitmap(bMapScaled3);
    image4.setImageBitmap(bMapScaled4);
    image5.setImageBitmap(bMapScaled5);
    image6.setImageBitmap(bMapScaled6);
    image7.setImageBitmap(bMapScaled7);
    image8.setImageBitmap(bMapScaled8);
    image9.setImageBitmap(bMapScaled9);
    image10.setImageBitmap(bMapScaled10);
    image11.setImageBitmap(bMapScaled11);
    image12.setImageBitmap(bMapScaled12);
    image13.setImageBitmap(bMapScaled13);
    image14.setImageBitmap(bMapScaled14);
    image15.setImageBitmap(bMapScaled15);

2 个答案:

答案 0 :(得分:3)

我可能会使用数组来减少代码的混乱。尝试这样的事情:

// UPDATE: change from String to int
int[] imageIds = new int[15] {
  R.id.card1, R.id.card2, R.id.card3,
  R.id.card4, R.id.card5, R.id.card6,
  R.id.card7, R.id.card8, R.id.card9,
  R.id.card10, R.id.card11, R.id.card12,
  R.id.card13, R.id.card14, R.id.card15
};

// load the source image only once
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.vraag);
Bitmap bScaled = Bitmap.createScaledBitmap(bMap, buttonSize, buttonSize, true);

// render them all
ImageButton[] imageButtons = new ImageButton[15];
for(int i = 0; i < imageButtons.size(); i++) {
  ImageButton button = imageButtons[i] = (ImageButton) findViewById(imageIds[i]);
  button.setImageBitmap(bMapScaled);
}
祝你好运!

答案 1 :(得分:1)

私人ImageButton图像[] [] =新图像[4] [4];

Class c=Class.forName("com.test.R$id");
Integer i = new Integer(c.getField("ToolsbuttonR1C1").getInt(new R.id()));

//在这种情况下,让我们假设它是R1C1格式 - 行和列

       for(int a=0,p=i;a<4;a++)
          {
                for(int b=0;b<4;b++)
                {
                    image[a][b]=(Button)findViewById(p); 
                    image[a][b].setOnClickListener(this); // an example
                    p++;
                }
        }

如果id不连续,这可能会给你一个错误。比如。如果你已插入 在xml中的tablerow布局中逐行... R.id将不会是连续的..所以你必须在第一个for循环中添加另一个p ++ ...

这就是你可以减少代码的方式..