开发者

dynamically add array of buttons and reference those buttons by id in android

i found this helpful post for adding buttons dynamically to a layout开发者_如何转开发, however i am can't understand how to reference those buttons by id (or some other way) to use them in the program. can anyone help me?


In code why don't you just declare a class level variable? Another common technique is to save references as tags or save whole bunch of references in the holder object and save that as a tag


I had the same situation.Just use Tag, and assign them an id that you can use in a loop. See example below for some images and assignment of tags and touch listener, but you can you it for buttons or anything you want. Now you can use loops to change things about each button:

for (int i = 0; i < 8; i++)
        {
        String bid = "WLButton"+i;
        int resID = getResources().getIdentifier(bid, "id", "com.head");
        wlbutt[i] = (ImageView) findViewById(resID);
        wlbutt[i].setTag(i);
        wlbutt[i].setOnTouchListener((OnTouchListener) WLListener);   
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜