Creating Imagebutton programmatically

陌路散爱 提交于 2019-11-27 06:10:46

问题


I want to create several ImageButtons programmatically. I am able to create them but the click event listener keeps receiving the same view (Button 2), whether I click on button 0 ,button1 or button 2.

    RelativeLayout gameBoard = (RelativeLayout) findViewById(R.id.RelGameboard);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);

    for(int i = 0 ; i <  3 ; i++)
    {

        ImageButton btnGreen = new ImageButton(this);
        btnGreen.setImageResource(R.drawable.bola_verde);
        btnGreen.setLayoutParams(lp);
        btnGreen.setOnClickListener(mGreenBallOnClickListener);
        btnGreen.setBackgroundColor(Color.TRANSPARENT); 
        btnGreen.setTag(i);
        btnGreen.setId(i);

                    gameBoard.addView(btnGreen);


     }'

Click event listener:

private View.OnClickListener mGreenBallOnClickListener = new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        //check which green ball was clicked
        ImageButton imgBtn = (ImageButton) v;

    Log.i("greeny","Clicked on green ball->"+imgBtn.getTag()+" v.ID->"+v.getId());
    }
};

For any imagebutton drawn, when I click I get : Clicked on green ball->2 v.ID->2

The buttons are in different positions ( i set a different padding for each one of them but in order to simplify the code I didn't put it here )


回答1:


Try adding a onClickListener -

private View.OnClickListener ClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int selected_item = (Integer) v.getTag();
    }
};

EDIT

Try creating a array of ImageButtons -

ImageButton[] btnGreen = new ImageButton[3];

for (int i = 0; i < 3; i++) {
    btnGreen[i] = new ImageButton(this);
    btnGreen[i].setImageResource(R.drawable.bola_verde);
    btnGreen[i].setLayoutParams(lp);
    btnGreen[i].setOnClickListener(ClickListener);
    btnGreen[i].setBackgroundColor(Color.TRANSPARENT); 
    btnGreen[i].setTag(i);
    btnGreen[i].setId(i);

    gameBoard.addView(btnGreen[i]);
}



回答2:


This happen because you do not set layout of button, All button in same place.

Use Aboveof,Belowof,left,right,center

Example

params.addRule(RelativeLayout.ALIGN_PARENT_ABOVE);  
    relativeLayout.addView(button, params);



回答3:


Try this code:

RelativeLayout gameBoard = (RelativeLayout)findViewById(R.id.RelGameboard);

ImageButton[] imageButtons;

private final int NUMBER_OF_IMAGE_BUTTONS = 5; //your number of image buttons

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

lp.addRule(RelativeLayout.CENTER_IN_PARENT,1);

imageButtons = new ImageButton[NUMBER_OF_IMAGE_BUTTONS];

for(int i = 0 ; i < NUMBER_OF_IMAGE_BUTTONS; i++){
   imageButtons[i] = new ImageButton(this);
   imageButtons[i].setImageResource(R.drawable.bola_verde);
   imageButtons[i].setLayoutParams(lp);
   imageButtons[i].setOnClickListener(mGreenBallOnClickListener);
   imageButtons[i].setBackgroundColor(Color.TRANSPARENT); 
   imageButtons[i].setTag(i);
   imageButtons[i].setId(i);
   gameBoard.addView(imageButtons[i]);
}


来源:https://stackoverflow.com/questions/16416990/creating-imagebutton-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!