来自for循环的几个按钮的setOnClickListener - Android

时间:2014-08-12 22:10:20

标签: android loops button for-loop tablelayout

我创建了一个带有两个for循环的TableLayout,一个用于行,一个用于列。 tablelayout内部是带有backgroundresources(图像)的按钮。 问题是我无法弄清楚如何为每个按钮设置onClickListener,因为 我没有给他们一个id,(他们在循环中)。

任何帮助都会非常友好。 这是代码:

public class CartoonActivity extends Activity implements OnClickListener {

private static final int NUM_ROWS = 3;
private static final int NUM_COL = 3;


int[] images = new int[] {R.drawable.cartoon1, R.drawable.cartoon2,
                R.drawable.cartoon3, R.drawable.cartoon4,
                R.drawable.cartoon5, R.drawable.cartoon6,
                R.drawable.cartoon7, R.drawable.cartoon8,
                R.drawable.cartoon9};



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags
        (WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.cartoons_menu);


    populateButton();       

}

private void populateButton() {

    TableLayout table = (TableLayout)findViewById(R.id.tableForCartoons);

    for (int row = 0; row < NUM_ROWS; row++) {
        TableRow tableRow = new TableRow(this);
        tableRow.setLayoutParams(new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.MATCH_PARENT,
                1.0f));
        table.addView(tableRow);

        for (int col = 0; col < NUM_COL; col++) {

            Button button = new Button(this);
            button.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT,
                    TableRow.LayoutParams.WRAP_CONTENT,
                    1.0f));

            tableRow.addView(button);
            button.setBackgroundResource(images[0]++);      
            button.setOnClickListener(this);

            }   

        }
    }


public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v.getBackground().equals(images[0]) == true) {

        Intent cartoon1Intent = new Intent(getApplicationContext(),
                Cartoon1Activity.class);
        startActivity(cartoon1Intent);


    }
}


}

1 个答案:

答案 0 :(得分:0)

int i=0;

private void populateButton() {

TableLayout table = (TableLayout)findViewById(R.id.tableForCartoons);

for (int row = 0; row < NUM_ROWS; row++) {
    TableRow tableRow = new TableRow(this);
    tableRow.setLayoutParams(new TableLayout.LayoutParams(
            TableLayout.LayoutParams.MATCH_PARENT,
            TableLayout.LayoutParams.MATCH_PARENT,
            1.0f));
    table.addView(tableRow);

    for (int col = 0; col < NUM_COL; col++) {

        Button button = new Button(this);
        button.setLayoutParams(new TableRow.LayoutParams(
                TableRow.LayoutParams.MATCH_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT,
                1.0f));

        tableRow.addView(button);
        button.setBackgroundResource(images[0]++);      
        button.setOnClickListener(this);
        button.setId(++i);//setId for button so that you can use it in onClick method

        }   

    }
 public void onClick(View v) {
  v.getId(); //will give give respective Id which is set.

 }

}