Android, setonclicklistner to dynamically generated array of textviews

后端 未结 2 396
梦如初夏
梦如初夏 2021-01-15 19:11

I have a dynamically generated array of textviews

for(int i = 0; i < blog_link_counter; i++) {
textViewArray[i] = new TextView(this);
textViewArray[i]         


        
相关标签:
2条回答
  • 2021-01-15 19:52

    don't have to use R.id.xxx, simply use the same numbers that you used in your loop:

    switch(v.getId())
    {
    case 0:
    case 1:
    //etc
    }
    
    0 讨论(0)
  • 2021-01-15 19:56

    Just call setOnClickListener(OnClickListener) then on each of your TextViews.

    for(int i = 0; i < blog_link_counter; i++) {
        textViewArray[i] = new TextView(this);
        textViewArray[i].setText(Html.fromHtml(array_blog_text[i]+"<br>"));
        textViewArray[i].setId(i);
        textViewArray[i].setOnClickListener(listener);
        ((LinearLayout) linearLayout).addView(textViewArray[i]);
    }
    
    0 讨论(0)
提交回复
热议问题