2013-03-26 55 views
0

我想创建一个基于游戏难度的动态数量的图像视图。我动态创建了所需的图像视图数量,并使用onclick侦听器将其设置为可点击。我的问题是,无论我点击什么图像视图,只会改变最后的图像。有没有什么办法可以让我的onclick听众知道我点击的图像视图并让它改变那个特定的图像?许多Imageviews的OnClickListener只改变最后的图像视图

public class MainActivity extends Activity { 

private static int ROWS; 
private static int COLUMNS; 

private int gameDifficulty; 
private Drawable backCard; 
private RelativeLayout mainView; 
private ImageView iView; 
private LinearLayout layout; 

private ClickListener clickListener = new ClickListener(); 

private ArrayList<Drawable> cards = new ArrayList<Drawable>(); 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 

    LoadImages(); 

    mainView = (RelativeLayout)findViewById(R.id.mainView); 


    Button easy = (Button) findViewById(R.id.button1); 
    easy.setOnClickListener(new View.OnClickListener() 
    { 

     public void onClick(View v) { 
      gameDifficulty = 1; 
      StartGame(); 

     } 
    }); 


    Button normal = (Button) findViewById(R.id.button2); 
    normal.setOnClickListener(new View.OnClickListener() 
    { 

     public void onClick(View v) { 
      gameDifficulty = 2; 
      StartGame(); 

     } 
    }); 

    Button hard = (Button) findViewById(R.id.button3); 
    hard.setOnClickListener(new View.OnClickListener() 
    { 

     public void onClick(View v) { 
      gameDifficulty = 3; 
      StartGame(); 

     } 
    });   
} 

public void LoadImages() 
{ 
    backCard = getResources().getDrawable(R.drawable.star); 

    cards.add(getResources().getDrawable(R.drawable.test1)); 
    cards.add(getResources().getDrawable(R.drawable.test2)); 
    cards.add(getResources().getDrawable(R.drawable.test3)); 
    cards.add(getResources().getDrawable(R.drawable.test4)); 
    cards.add(getResources().getDrawable(R.drawable.test5)); 
    cards.add(getResources().getDrawable(R.drawable.test6)); 
} 

public void StartGame() 
{ 
    mainView.removeView(findViewById(R.id.button1)); 
    mainView.removeView(findViewById(R.id.button2)); 
    mainView.removeView(findViewById(R.id.button3)); 



    if (gameDifficulty == 1) 
    { 
     ROWS = 4; 
     COLUMNS = 2; 
     CreateCard(ROWS, COLUMNS); 
     System.out.println("Creating card"); 

    } 


    if (gameDifficulty == 2) 
    { 
     ROWS = 4; 
     COLUMNS = 3; 
     CreateCard(ROWS, COLUMNS); 

    } 

    if (gameDifficulty == 3) 
    { 
     ROWS = 4; 
     COLUMNS = 4; 
     CreateCard(ROWS, COLUMNS); 

    } 


} 

public void CreateCard(int rows, int columns) 
{ 


    //Amount of stars going down 
    for (int x = 0; x < columns; x++) 
    {   
     LinearLayout row = new LinearLayout(this); 
     row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 


     //Number of rows 
     for (int y = 0; y < rows; y++) 
     { 
      iView = new ImageView(this); 
      iView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      iView.setBackgroundDrawable(backCard); 
      iView.setId(y + 1 + (x * 4)); 
      iView.setClickable(true); 
      iView.setOnClickListener(clickListener);    
      row.addView(iView);    
     } 

     layout.addView(row); 
     setContentView(layout); 
    }  


} 

class ClickListener implements OnClickListener 
{ 

    @Override 
    public void onClick(View v) 
    { 
     System.out.println("Clicking card"); 
     System.out.println(iView.getId()); 
     iView.setBackgroundDrawable(cards.get(2));   
    } 

} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 
} 

回答

0

使用传递给onClick()变量:

​​

的问题是,onClickListener内运行代码点击查看时,而不是在循环执行,所以iView引用的最后一个值从循环...

+0

解决了这个问题。谢谢。 – Karl 2013-03-26 17:25:30

0

尝试v.getId()而不是侦听器中的iView.getId()。