安卓:View.setID(INT ID)编程 - 如何避免ID冲突?冲突、setID、View、ID

2023-09-11 10:41:58 作者:各种姿势ゞ各种嗨°

我编程方式添加TextViews在一个for循环,并将它们添加到一个ArrayList

I'm adding TextViews programmatically in a for-loop and add them to an ArrayList.

我如何使用 TextView.setId(INT ID)?什么整数ID做我拿出那么它不与其它ID冲突?

How do I use TextView.setId(int id)? What Integer ID do I come up with so it doesn't conflict with other IDs?

推荐答案

查看文件

的标识符不必须是在该视图的层次结构是唯一的。标识符应当是一个正数。

The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.

所以,你可以使用任意正整数你喜欢的,但在这种情况下,可以用相当于身份证的一些看法。如果你想搜索的层级调用一些以 setTag 有一些关键的对象可能是得心应手。

So you can use any positive integer you like, but in this case there can be some views with equivalent id's. If you want to search for some view in hierarchy calling to setTag with some key objects may be handy.