开发者

How do I to findViewByTag?

开发者 https://www.devze.com 2023-04-05 08:21 出处:网络
Say I have dynamically generated some amount of LinearLayout, all with different tags. for (int i = 0; i <= 5; i++)

Say I have dynamically generated some amount of LinearLayout, all with different tags.

for (int i = 0; i <= 5; i++)
{
        final LinearLayout LinLayBtn = new LinearLayout(this);
        LinLayBtn.setTag( "id开发者_开发技巧"+String.valueOf(i) );
         ...

And now I need to somehow access this layout by they tag number from a different method.

LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("1");

What would be the best way to do that?

Thanks!


Have you tried it this way and found a problem with it?

You'll need to make this line

LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("1");

match the naming scheme you used when you set the tag. So you'd want something like this in your example:

LinearLayout LinLayBtn = (LinearLayout)findViewWithTag("id1");

If you need to do many of these lookups though it would probably be a better approach to store the view references in an array while you are creating them so you don't have to have all of the findView calls. Or do like @Muhammad suggested and use parent.getChild(index i);

0

精彩评论

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