创建编程的ImageButton(Creating Imagebutton programmatic

2019-09-02 03:32发布

我想通过程序生成几个ImageButtons。 我能够创建他们,但click事件侦听器不断地接受了同样的观点(按钮2),我是否点击按钮0,按钮1或按钮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);


     }'

点击事件监听器:

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());
    }
};

对于任何的ImageButton绘制,当我点击我得到:点击绿色的球 - > 2 v.ID-> 2

这些按钮在不同的位置(我设置了不同的填充对于他们中的每一个,但是为了简化代码,我没有把它放在这里)

Answer 1:

尝试添加onClickListener -

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

编辑

尝试创建的阵列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]);
}


Answer 2:

出现这种情况是因为你没有设置按键的布局,在同一个地方的所有按钮。

Use Aboveof,Belowof,left,right,center

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


Answer 3:

试试这个代码:

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]);
}


文章来源: Creating Imagebutton programmatically