0

我正在尝试根据游戏难度创建动态数量的图像视图。我动态创建所需的图像视图数量,并使用onclick侦听器将它们设置为可点击。我的问题是,无论我点击什么图像视图,它只会改变最后一张图像。有什么方法可以让onclick听众知道我正在点击什么图像视图并让它改变那个特定的视图?

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;
}
}
4

2 回答 2

0

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

于 2013-03-26T17:05:46.073 回答
0

使用传递给的变量onClick()

public void onClick(View v) 
{
    ImageView i = (ImageView) v;
    System.out.println("Clicking card");
    System.out.println(i.getId());
    i.setBackgroundDrawable(cards.get(2));          
}

问题是 onClickListener 内的代码在单击视图时运行,而不是在循环执行时运行,因此iView引用循环中的最后一个值......

于 2013-03-26T17:05:24.723 回答