我在for循环中以编程方式添加TextViews,并将它们添加到数组列表中。

我如何使用TextView。setId (int id) ?我要用什么样的整数ID才能不与其他ID冲突?


当前回答

public String TAG() {
    return this.getClass().getSimpleName();
}

private AtomicInteger lastFldId = null;

public int generateViewId(){

    if(lastFldId == null) {
        int maxFld = 0;
        String fldName = "";
        Field[] flds = R.id.class.getDeclaredFields();
        R.id inst = new R.id();

        for (int i = 0; i < flds.length; i++) {
            Field fld = flds[i];

            try {
                int value = fld.getInt(inst);

                if (value > maxFld) {
                    maxFld = value;
                    fldName = fld.getName();
                }
            } catch (IllegalAccessException e) {
                Log.e(TAG(), "error getting value for \'"+ fld.getName() + "\' " + e.toString());
            }
        }
        Log.d(TAG(), "maxId="+maxFld +"  name="+fldName);
        lastFldId = new AtomicInteger(maxFld);
    }

    return lastFldId.addAndGet(1);
}

其他回答

您可以使用xml资源文件定义稍后将在r.d ID类中使用的ID,并让Android SDK在编译时设置实际的惟一值。

 res/values/ids.xml
<item name="my_edit_text_1" type="id"/>
<item name="my_button_1" type="id"/>
<item name="my_time_picker_1" type="id"/>

在代码中使用它:

myEditTextView.setId(R.id.my_edit_text_1);

(这是对业余爱好者的回答的评论,但它太长了…呵呵)

Of course a static is not needed here. You could use SharedPreferences to save, instead of static. 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. Therefore, just make it an instance variable instead of static.

下面是一个更小的版本,运行速度更快,可能更容易阅读:

int fID = 0;

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

上述函数应该足够了。因为,据我所知,android生成的id有数十亿个,所以这可能会第一次返回1,而且总是很快。 因为,它实际上不会遍历已使用的id来查找未使用的id。但是,如果它确实找到了一个使用的ID,那么循环就在那里。

然而,如果你仍然希望在后续重新创建应用程序之间保存进度,并希望避免使用静态。这是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;
}

这个类似问题的答案应该告诉你你需要知道的关于android id的一切:https://stackoverflow.com/a/13241629/693927

编辑/修复:刚刚意识到我完全搞砸了保存。我一定是喝醉了。

Compat库现在还支持API级别之前的generateViewId()方法。

只要确保使用27.1.0以上版本的Compat库即可

例如,在您的构建中。Gradle文件,放入:

实现“com.android.support: appcompat-v7:27.1.1

然后你可以简单地使用来自ViewCompat类的generateViewId()而不是View类,如下所示:

//将分配唯一的ID myView。id = ViewCompat.generateViewId()

编码愉快!

这对我来说很管用:

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++;  
}
public String TAG() {
    return this.getClass().getSimpleName();
}

private AtomicInteger lastFldId = null;

public int generateViewId(){

    if(lastFldId == null) {
        int maxFld = 0;
        String fldName = "";
        Field[] flds = R.id.class.getDeclaredFields();
        R.id inst = new R.id();

        for (int i = 0; i < flds.length; i++) {
            Field fld = flds[i];

            try {
                int value = fld.getInt(inst);

                if (value > maxFld) {
                    maxFld = value;
                    fldName = fld.getName();
                }
            } catch (IllegalAccessException e) {
                Log.e(TAG(), "error getting value for \'"+ fld.getName() + "\' " + e.toString());
            }
        }
        Log.d(TAG(), "maxId="+maxFld +"  name="+fldName);
        lastFldId = new AtomicInteger(maxFld);
    }

    return lastFldId.addAndGet(1);
}