如何使用字符串作为Android的一个的ResourceID字符串、如何使用、ResourceID、Android

由网友(花落人散去)分享简介:我有几个的TextView s或其他组件,无所谓。而意见有重复的ID,如: textView1,textView2,textView3 等I've several TextViews or another component, doesn't matter. And the views have iteration...

我有几个的TextView s或其他组件,无所谓。而意见有重复的ID,如: textView1,textView2,textView3

I've several TextViews or another component, doesn't matter. And the views have iteration ids like: textView1, textView2, textView3 etc.

只要我想用的 pre-字符串值来迭代IDS。

Simply I want to iterate ids by using pre-string values.

的伪例如:

String pre_value = "textView";

for(int i = 0; i < size; i++) {

    String usable_resource_id = pre_value + Integer.toString(pre_value);
    // So how to use these id like R.id.textView1
    // Cast or something similar

}

有什么建议?

推荐答案

您可以使用 资源#则getIdentifier() ,以得到一个字符串的标识符。

You can use Resources#getIdentifier() to get the identifier from a string.

但是,如果你要遍历他们,那岂不是更容易保持的id在数组或列表?

But if you are going to iterate over them, wouldn't it be easier to keep the ids in an array or a list?

阅读全文

相关推荐

最新文章