动态没有时在onClickListener中访问一个ImageButton的id。 Android 中的按钮数量

Access id of an ImageButton in onClickListener when there are dynamic no. of buttons in Android

我在 Android 中有一个 table 布局,table 中的每一行都有一个 ImageButton、一个 TextView 和另一个 ImageButton。没有。行数是可变的,我在运行时以编程方式生成 table。

我使用 ImageButton.setId(i) 将按钮的 ID 设置为与行号相同。

当用户单击任何 ImageButton 时,我希望能够在 onClickListener 中访问它的 ID。有办法吗?我在 Whosebug 上看到的所有关于此的答案都涉及固定编号。按钮并使用 switch 语句找出 id。但自从没有。按钮的数量对我来说是动态的,这对我不起作用。

代码如下:

TableLayout tbl_layout = (TableLayout) findViewById(R.id.tbl_layout);
for (int i=0; i<num_rows; i++) {

    TableRow tbl_row= new TableRow(this);
    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    tbl_row.setLayoutParams(lp);
    tbl_row.setPadding(0, 5, 0, 5);
    //tbl_row.setBackground(R.drawable.border);

   if(i == 3)
        tbl_row.setBackgroundColor(Color.GREEN);
    else
        tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));

    tbl_row.setGravity(Gravity.CENTER);


    TextView tv = new TextView(this);
    tv.setGravity(Gravity.CENTER);
    tv.setText("Some text goes here\New line.");


    ImageButton infoBtn = new ImageButton(this);
    infoBtn.setImageResource(R.drawable.ic_menu_info);
    infoBtn.setAdjustViewBounds(true);
    infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
    infoBtn.setMaxHeight(120);
    infoBtn.setMaxWidth(120);
    infoBtn.setId(i);
    //infoBtn.setBackgroundColor(Color.BLUE);

    ImageButton cameraBtn = new ImageButton(this);
    cameraBtn.setImageResource(R.drawable.ic_menu_camera);
    cameraBtn.setAdjustViewBounds(true);
    cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
    cameraBtn.setMaxHeight(120);
    cameraBtn.setMaxWidth(120);
    cameraBtn.setId(i);

    tbl_row.addView(infoBtn);
    tbl_row.addView(tv);
    tbl_row.addView(cameraBtn);
    tbl_layout.addView(tbl_row, i);
}`
for (int i=0; i<num_rows; i++) {

        TableRow tbl_row= new TableRow(this);
        TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
        tbl_row.setLayoutParams(lp);
        tbl_row.setPadding(0, 5, 0, 5);
        //tbl_row.setBackground(R.drawable.border);

       if(i == 3)
            tbl_row.setBackgroundColor(Color.GREEN);
        else
            tbl_row.setBackgroundColor(Color.parseColor("#ffffd1"));

        tbl_row.setGravity(Gravity.CENTER);


        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER);
        tv.setText("Some text goes here\New line.");


        ImageButton infoBtn = new ImageButton(this);
        infoBtn.setImageResource(R.drawable.ic_menu_info);
        infoBtn.setAdjustViewBounds(true);
        infoBtn.setScaleType(ImageView.ScaleType.FIT_XY);
        infoBtn.setMaxHeight(120);
        infoBtn.setMaxWidth(120);
        //infoBtn.setId(i);
        //infoBtn.setBackgroundColor(Color.BLUE);
       infoBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });

        ImageButton cameraBtn = new ImageButton(this);
        cameraBtn.setImageResource(R.drawable.ic_menu_camera);
        cameraBtn.setAdjustViewBounds(true);
        cameraBtn.setScaleType(ImageView.ScaleType.FIT_XY);
        cameraBtn.setMaxHeight(120);
        cameraBtn.setMaxWidth(120);
        //cameraBtn.setId(i);
        cameraBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });

        tbl_row.addView(infoBtn);
        tbl_row.addView(tv);
        tbl_row.addView(cameraBtn);
        tbl_layout.addView(tbl_row, i);
    }`