我想創建一個基於遊戲難度的動態數量的圖像視圖。我動態創建了所需的圖像視圖數量,並使用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;
}
}
解決了這個問題。謝謝。 – Karl 2013-03-26 17:25:30