Android OnClickListener用于许多imageview,仅更改最后一个imageview
我试图根据游戏难度创建动态数量的图像视图。我动态创建所需的图像视图数量,并使用Android OnClickListener用于许多imageview,仅更改最后一个imageview,android,android-imageview,Android,Android Imageview,我试图根据游戏难度创建动态数量的图像视图。我动态创建所需的图像视图数量,并使用onclick侦听器将其设置为可单击。我的问题是,无论我点击哪个图像视图,它只会改变最后一个图像。有没有办法让onclicklistener知道我点击的是什么图像视图,并让它改变那个特定的视图 public class MainActivity extends Activity { private static int ROWS; private static int COLUMNS; private int ga
onclick
侦听器将其设置为可单击。我的问题是,无论我点击哪个图像视图,它只会改变最后一个图像。有没有办法让onclick
listener知道我点击的是什么图像视图,并让它改变那个特定的视图
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;
}
}
公共类MainActivity扩展活动{
私有静态int行;
私有静态int列;
私密性;
私人可支取回退卡;
私人关系网;
私有图像视图iView;
私人线路布局;
private ClickListener ClickListener=新建ClickListener();
私有ArrayList卡=新ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
布局=新的线性布局(本);
布局。设置方向(线性布局。垂直);
LoadImages();
mainView=(RelativeLayout)findViewById(R.id.mainView);
按钮简单=(按钮)findViewById(R.id.button1);
easy.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
游戏难度=1;
StartGame();
}
});
按钮正常=(按钮)findViewById(R.id.button2);
normal.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
游戏难度=2;
StartGame();
}
});
按钮硬=(按钮)findViewById(R.id.button3);
setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v){
游戏难度=3;
StartGame();
}
});
}
公共void LoadImages()
{
backCard=getResources().getDrawable(R.drawable.star);
add(getResources().getDrawable(R.drawable.test1));
add(getResources().getDrawable(R.drawable.test2));
add(getResources().getDrawable(R.drawable.test3));
add(getResources().getDrawable(R.drawable.test4));
add(getResources().getDrawable(R.drawable.test5));
add(getResources().getDrawable(R.drawable.test6));
}
公共无效StartName()
{
mainView.removeView(findviewbyd(R.id.button1));
mainView.removeView(findviewbyd(R.id.button2));
mainView.removeView(findViewById(R.id.button3));
如果(游戏难度==1)
{
行数=4;
列=2;
创建卡片(行、列);
System.out.println(“创建卡”);
}
如果(游戏难度==2)
{
行数=4;
列=3;
创建卡片(行、列);
}
如果(游戏难度==3)
{
行数=4;
列=4;
创建卡片(行、列);
}
}
公共void CreateCard(int行、int列)
{
//下降的星星数量
对于(int x=0;x
使用传递给onClick()的变量。
:
问题是onClickListener中的代码是在单击视图时运行的,而不是在循环执行时运行的,因此iView
引用循环中的最后一个值…尝试在侦听器中使用v.getId()而不是iView.getId()
public void onClick(View v)
{
ImageView i = (ImageView) v;
System.out.println("Clicking card");
System.out.println(i.getId());
i.setBackgroundDrawable(cards.get(2));
}