代码之家  ›  专栏  ›  技术社区  ›  Iceflame007

当Android中有动态数量的按钮时,访问onClickListener中ImageButton的id

  •  0
  • Iceflame007  · 技术社区  · 9 年前

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

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

    当用户单击任何ImageButton时,我希望能够在onClickListener中访问其id。有办法吗?我看到的关于stackoverflow的所有答案都涉及固定数量的按钮,并使用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);
    }`
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Suhail Mehta    9 年前
    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);
        }`