Android:View.setID(int id)以编程方式 - 如何避免ID冲突?

本文翻译自:Android: View.setID(int id) programmatically - how to avoid ID conflicts?

I'm adding TextViews programmatically in a for-loop and add them to an ArrayList. 我在for循环中以编程方式添加TextViews并将它们添加到ArrayList。

How do I use TextView.setId(int id) ? 我如何使用TextView.setId(int id) What Integer ID do I come up with so it doesn't conflict with other IDs? 我提出了什么整数ID,所以它不与其他ID冲突?


#1楼

参考:https://stackoom.com/question/7Bxx/Android-View-setID-int-id-以编程方式-如何避免ID冲突


#2楼

This works for me: 这对我有用:

static int id = 1;

// Returns a valid id that isn't in use
public int findId(){  
    View v = findViewById(id);  
    while (v != null){  
        v = findViewById(++id);  
    }  
    return id++;  
}

#3楼

(This was a comment to dilettante's answer but it got too long...hehe) (这是对dilettante的答案的评论,但它太长了......呵呵)

Of course a static is not needed here. 当然,这里不需要静电。 You could use SharedPreferences to save, instead of static. 您可以使用SharedPreferences来保存,而不是静态。 Either way, the reason is to save the current progress so that its not too slow for complicated layouts. 无论哪种方式,原因是保存当前进度,以便它对于复杂的布局而言不会太慢。 Because, in fact, after its used once, it will be rather fast later. 事实上,因为在使用一次后,它会在以后相当快。 However, I dont feel this is a good way to do it because if you have to rebuild your screen again (say onCreate gets called again), then you probably want to start over from the beginning anyhow, eliminating the need for static. 但是,我不觉得这是一个很好的方法,因为如果你必须再次重建你的屏幕(比如onCreate再次被调用),那么你可能想从头开始,无论如何,不​​需要静态。 Therefore, just make it an instance variable instead of static. 因此,只需将其设为实例变量而不是静态变量。

Here is a smaller version that runs a bit faster and might be easier to read: 这是一个较小的版本,运行速度更快,可能更容易阅读:

int fID = 0;

public int findUnusedId() {
    while( findViewById(++fID) != null );
    return fID;
}

This above function should be sufficient. 上述功能应该足够了。 Because, as far as I can tell, android-generated IDs are in the billions, so this will probably return 1 the first time and always be quite fast. 因为,据我所知,android生成的ID数十亿,所以这可能是第一次返回1并且总是非常快。 Because, it wont actually be looping past the used IDs to find an unused one. 因为它实际上不会循环使用已使用的ID来查找未使用的ID。 However, the loop is there should it actually find a used ID. 但是,循环是否应该实际找到使用的ID。

However, if you still want the progress saved between subsequent recreations of your app, and want to avoid using static. 但是,如果您仍希望在应用程序的后续重新创建之间保存进度,并且希望避免使用静态。 Here is the SharedPreferences version: 这是SharedPreferences版本:

SharedPreferences sp = getSharedPreferences("your_pref_name", MODE_PRIVATE);

public int findUnusedId() {
    int fID = sp.getInt("find_unused_id", 0);
    while( findViewById(++fID) != null );
    SharedPreferences.Editor spe = sp.edit();
    spe.putInt("find_unused_id", fID);
    spe.commit();
    return fID;
}

This answer to a similar question should tell you everything you need to know about IDs with android: https://stackoverflow.com/a/13241629/693927 这个类似问题的答案应告诉你需要知道的关于android的所有ID: https//stackoverflow.com/a/13241629/693927

EDIT/FIX: Just realized I totally goofed up the save. EDIT / FIX:刚刚意识到我完全搞砸了。 I must have been drunk. 我一定是喝醉了。


#4楼

Since API 17, the View class has a static method generateViewId() that will 从API 17开始, View类有一个静态方法 generateViewId()

generate a value suitable for use in setId(int) 生成一个适合在setId(int)中使用的值


#5楼

From API level 17 and above, you can call: View.generateViewId() 从API级别17及更高版本,您可以调用: View.generateViewId()

Then use View.setId(int) . 然后使用View.setId(int)

If your app is targeted lower than API level 17, use ViewCompat.generateViewId() 如果您的应用的目标低于API级别17,请使用ViewCompat.generateViewId()


#6楼

According to View documentation 根据View文档

The identifier does not have to be unique in this view's hierarchy. 标识符在此视图的层次结构中不必是唯一的。 The identifier should be a positive number. 标识符应为正数。

So you can use any positive integer you like, but in this case there can be some views with equivalent id's. 所以你可以使用你喜欢的任何正整数,但在这种情况下,可能会有一些具有相同id的视图。 If you want to search for some view in hierarchy calling to setTag with some key objects may be handy. 如果你想在层次结构中搜索一些视图,调用setTag ,一些关键对象可能会很方便。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值