【问题标题】:setOnClickListener for several buttons from for loops - Android来自 for 循环的几个按钮的 setOnClickListener - Android
【发布时间】:2014-10-06 03:35:56
【问题描述】:

我制作了一个带有两个 for 循环的 TableLayout,一个用于行,一个用于列。 表格布局内是带有背景资源(图像)的按钮。 问题是我不知道如何为每个按钮设置一个 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);


    }
}


}

【问题讨论】:

    标签: android loops button for-loop tablelayout


    【解决方案1】:
    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.
    
     }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-20
      • 2014-11-07
      相关资源
      最近更新 更多